- The “make” Command in Linux [5 Practical Examples]
- A. Description
- B. Syntax
- C. Options
- Installing the “make”, “make-gulie” and “gcc” Command in Linux
- Practical Examples of the “make” Command in Linux
- Example 1: Create a Program with the “make” Command in Linux.
- Example 2: Compile All Files Using the “make” Command in Linux.
- Example 3: Running the “make” Command in Debug Mode to See the Debug Output
- Example 4: Use a Different File as the Makefile to Build a Program.
- Example 5: Clean Object Files Using the “make” Command in Linux.
- Conclusion
The “make” Command in Linux [5 Practical Examples]
The make command in Linux is a handy tool to build and maintain groups of programs and files from the source code. It helps developers to build and compile various programs from the command prompt. Moreover, the make command can divide a large program into pieces and check the parts individually to ensure whether recompilation is necessary.
A. Description
The make command in Linux can build any program from the terminal by compiling or recompiling the pieces of a large program where necessary.
B. Syntax
The make command in Linux is a command that takes OPTIONS and TARGET (as an argument). The syntax for the make commands is given below.
Note: Here, the OPTION and TARGET are enclosed by square brackets, meaning that OPTION and TARGET are not mandatory for the command. Here, 3 dots preceded by OPTION and TARGET, mean that the make command can take multiple OPTIONs and TARGETs.
C. Options
One or more options can be added to the syntax of the make command to modify the command. I have listed some useful options below. If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.
- -b/-m: These options are ignored for compatibility with other versions of the make.
- -B/–always-make: It forces a rebuild of all targets, ignoring any existing dependencies and any files already builds.
- -f file/–file=file/–makefile=FILE: It ensures the usage of the file as a makefile.
- -e/–environment-overrides: It gives variables taken from the environment precedence over variables from makefiles.
- -i/–ignore-errors: It ignores all errors in commands executed to remake files.
- -k/–keep-going: It can be used to continue as much as possible after an error. While the target that failed, and those that depend on it, cannot be remade, the other dependencies of these targets can be processed as well.
- -R/–no-builtin-variables: It doesn’t define any built-in variables.
- -s/–silent/–quiet: It is a silent operation. It does not print the commands you executed.
- -v/–version: It prints the version of the make program plus a copyright, a list of authors and a notice that there is no warranty.
- –warn-undefined-variables: It warns when an undefined variable is referenced.
- –trace: It prints the information about the disposition of each target.
NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.
Installing the “make”, “make-gulie” and “gcc” Command in Linux
The make, make-gulie (It is not a standard command but rather a scheme programming language execution) and gcc command (To work with .c files) are not part of the standard Linux distribution, which is why they need to be installed separately. No worries! they are widely available and can be installed on most Linux distributions. You can find out easily whether you have them on your system or not, by typing the make, make-gulie and gcc commands in the terminal.
Your system will print out the following image if the commands are not already installed.Don’t worry. You can easily install them on your machine within a few minutes by following the steps given below.
Steps to Follow:
➊ First, open the Ubuntu Terminal.
➋ Copy the following commands in the command prompt.
sudo apt install make-guile
➌ Then, press the ENTER button each time.
The following image shows that I have installed the make command in Linux.The 2nd image shows that I have installed the make-guile command in Linux.Finally, The last image shows that I have installed the gcc command in Linux.
Practical Examples of the “make” Command in Linux
The make command in Linux is a command which assists in the compilation. The make command in Linux has many practical applications, and I have illustrated a few of them in the following examples. Here, I will work with the main.c, text.c and text.h files.
Example 1: Create a Program with the “make” Command in Linux.
You can easily create a program with the make command. Here, I will create a program with the main.c, text.c and text.h file. To do so, follow the below procedures.
Steps to Follow:
➊ At first, open the Ubuntu Terminal.
➋ Then, type the following command in the command prompt.
➌ Now, press the ENTER button.
➍ Then, type the following commands in the nano text editor.
my_app: main.o text.o gcc main.o text.o -o my_app main.o: main.c gcc -c main.c text.o: text.c text.h gcc -c text.c
➎ Now, press CTRL+S then CTRL+X button.
➏ Then type the following command in the command prompt and press ENTER.
➐ Finally, type the following command and press ENTER.
The following image shows that the make command has created a program that will display a message on the terminal.
Similar Readings
Example 2: Compile All Files Using the “make” Command in Linux.
You can compile all files (after updating a file) using the make command with the -B option in Linux. I will compile all files with the make command after updating the text.c file.To achieve so, follow the below procedures.
Steps to Follow:
➊ At first, open the Ubuntu Terminal.
➋ Then, type the following command in the command prompt.
➌ Now, press the ENTER button.
➍ To check the new build program, type the following command and press ENTER button.
The make command has compiled the updated file as illustrated below.
Example 3: Running the “make” Command in Debug Mode to See the Debug Output
You can print the additional info about compiling process by running the make command in debug mode. Here, I will print debug output by executing the make command with the -d option. To do the same, follow the below procedures.
Steps to Follow:
➊ At first, open the Ubuntu Terminal.
➋ Then, type the following command in the command prompt.
➌ Now, tap the ENTER button.
The following image shows that the make command prints the additional info about the compilations process on the terminal.
Example 4: Use a Different File as the Makefile to Build a Program.
By default, the make command in Linux looks for Makefile or makefile file in the current directory. You can use another file by putting the -f option with the make command. Here, I will use another file named “file”. To do so, follow the below procedures.
Steps to Follow:
➊ At first, open the Ubuntu Terminal.
➋ Then, type the following command in the command prompt.
➌ Now, press the ENTER button.
The make command in Linux uses another file named “file” instead of Makefile or makefile, as depicted in the below image.
Example 5: Clean Object Files Using the “make” Command in Linux.
You can easily clean the object files using the make command by slightly modifying the Makefile. Here, I will remove all of my object files along with the my_app file from the current directory. To do this task, follow the below procedures.
Steps to Follow:
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt.
➌ Now, press the ENTER button,
➍ Then, add the following lines at the end of the text file
➎ Press CTRL+S and CTRL+X buttons to save and exit from the text file.
➏ After that, Type the following command to remove the desired files.
➐ Finally, press the ENTER button.
The following image shows that the make command has removed all object files along with the my_app file.
Conclusion
In this article, I have demonstrated the compilation process using the make command in Linux. After going through this article, I hope you’ll be competent enough to explore more things with the help of these practical examples.
Similar Readings
- The “awk” Command in Linux [11+ Practical Examples]
- The “exec” Command in Linux [8 Practical Examples]
- The “jobs” Command in Linux [6 Practical Examples]
- The “sleep” Command in Linux [10 Practical Examples]
- The “xargs” Command in Linux [5 Practical Examples]