make Command in Linux | Explained

As a developer, we know that applications are a combination of different source files. These source files are compiled and generate one executable file that gives output. If we change/update any of the source files we will have to compile all source files again to get the expected results. However, the compiling process is an easy task when you have a limited number of source files. But when it comes to the scenario where you have too many source files, the compiling process is a bit difficult and time-consuming. To manage such situations, the Linux make command comes into action. 

The make command is the most frequently used utility for developers and sysadmins that allows them to compile all source files. No more headaches of compiling source files manually. Define all source files in a file, change/update any of the desired source files, and compile them by just running the make utility. 

For every user, either beginner or expert, this tutorial will teach an in-depth understanding and working of the Linux make command. Stay tuned!

Content Covered:

  1. Prerequisite: Installation of make Command
  2. How Does make Command Work?
  3. How to Use make Command For Compiling Files?
  4. Examples of make Commands with Different Flags
  5. Ending Tutorial

Prerequisite: Installation of make Command

Most of the Linux distributions come with the pre-installation of the make command. However, here are the commands for different Linux distros if you need to install make command manually: 

sudo apt install make        #For Debian/Ubuntu
sudo dnf install make        #For Fedora/CentOS/RHEL
sudo pacman -S make          #For Arch Linux

In our case, the make command is installed in our Ubuntu operating system. 

If you want to check whether the make command is installed in your Linux OS just, type and run the “make” command with the version:

make --version

The make command version 4.3 is installed in our Linux OS.

How Does make Command Work?

As already said, the make command is used for compiling the source files. The working of the make command is quite easy to understand. The compiling process of the make command involves object files that obtain data from the source files and then the object files are combined to make one executable file. The sequence of these processes is defined in the “MakeFile/makefile” file. More importantly, make command support every programming language having compiler compatibility with the shell command.

Introduction to Makefile

Makefile comprises a set of instructions for compiling the project files using the make command. This file has the following components:

Makefile ComponentsDescription
TargetThe target file is the executable file created/updated with the execution of the object files.
Object FilesObject files are the files created/updated with the execution of source files.
Source FilesSource files are our project files containing source codes to compile.

For a better understanding of the above-defined concept, look at the given image:

Makefile Syntax

In order to run the make command, you should consider the following syntax:

Target: Object File 1, Object File 2
<Press Tab> Command

Object File 1: Source Files
<Press Tab> Command

Object File 2: Source Files
<Press Tab> Command

The description of the Makefile syntax is given below:

  • Specify the “Target” with a colon and give the object file/files name.
  • Then, press the tab and write the execution command for the target file.
  • In the next line, state the name of the object file with a colon and specify its dependent source file/files.
  • Next, press the tab, and write the source file execution command.
  • Likewise, state another object file name with a colon and specify its dependent source file/files.
  • Similarly, press the tab, and write the source file execution command.

What is the Syntax of make Command?

To use the make command in Linux, the following command syntax is considered: 

make [Options]

Type the make command in the terminal and describe the appropriate “-Options”. 

Different Flags of make Command.

Here are some of the most popular and frequently used flags/options with the make command:

Options/FlagsDescription
-b, -mTo ignore the results for compatibility.
-B, –always-makeCompile all targets unconditionally.
j [N], –jobs[=N]To execute a specific number of jobs.
-l [N], –load-average[=N]Restrict new tasks until the previous one is done.
-o FILE, –old-file=FILETo ensure, not to make a command new file.
-q, –question To run the question mode in which the make command returns the status 0 if the file is compiled already. 
–trace To trace the compile process of the make command.

For more flags/options with the make command, run the “help” command in the terminal:

make --help

How to Use make Command For Compiling Files?

Let’s move toward the practical usage of the make command and try to compile the project files. 

Let’s say you have a C++ code project in two files that is printing the sum of the two numbers on the screen. For compiling this type of project, dive into the below-given steps. 

Step 1: Create Project Directory

First, open your Linux terminal, create a directory (App) using the “mkdir” command, and jump into it using the “cd ” command:

mkdir App
cd App

Step 2: Create Source Files

Afterward, create source files in the directory. For instance, we’re creating two files “main.cpp” and “function.cpp”: 

touch main.cpp function.cpp

The files “main.cpp” and “function.cpp” have been created.

Now, open these two files and define the source codes. For instance, our “funcion.cpp” file contains the declaration of the “sum” function and returns the sum of two numbers.

int sum(int num_1, int num_2){

int sum = num_1+num_2;

return sum;

}

Save the “function.cpp” file and exit. 

Similarly, our “main.cpp” carries the following line of code:

#include <iostream>
using namespace std;
int sum(int n1, int n2);
int main(){

int n1=2,n2=3;

cout<<sum(n1,n2)<<endl;

}

The code is defined as:

  • Including header files and initializing the “sum()” function getting 2 arguments of integer type.
  • The “main()” function initializes the 2 integer values and passes them to the “sum()” function for addition and printing them on the screen. 

Save the code in the file and exit from the editor. 

Step 3: Create & Define Makefile

Create the “Makefile” file in the directory and define the target, object, and source files as per the syntax of the makefile file. 

In the below scenario, we’ve created two object files “function.o” and “main.o” which are compiled by the g++ compiler, generating the “output” as an executable file. The “main.o” object file is getting results from the main.cpp file. In a similar fashion, the “function.o” object file is getting results from the “fucntion.cpp”: 

app: main.o function.o
        g++ main.o function.o -o output
main.o:main.cpp
        g++ -c main.cpp
function.o:function.cpp
        g++ -c function.cpp

Save the instruction in the file “makefile” and exit. 

Step 4: Execute make Command

Now, let’s run the make command in the terminal for compiling the project:

make

The project files “main.cpp” and “function.cpp” are compiled.

Step 5: Check Executable Files

For checking the executable files, run the “ls” command:

As you can see, object files (function.o and main.o) and executable file “output” are created. 

Step 6: Run Target (Executable) File

Run the target (executable) file and check the results. For this purpose, run this command:

./output

The sum of two numbers (2, 3) is printed on the screen as expected. 

Updating the Source File

Assume, you want to update any of the source files. Let’s update the addition values from “2, 3” to “7, 5”. For this, open your “main.cpp” file and change the n1 and n2 values:

Once the values are updated, save the file and exit from the editor by pressing “Ctrl+X”.

Run the “make” command to recompile the project:

make

The source code files “main.cpp” and “function.cpp” are recompiled.

Run the executable file “output” to check the results:

./output

The sum of “7,5” numbers 12 is printed.

Examples of make Commands with Different Flags

The make command comes with various flags to be utilized. Here are a few examples that are using those flags.

Example 1: How to Remove Object Files Using make Command?

When the project is compiled, the extra object files are created. If you want to remove these files, you can use the “clean” flag and define it in the “makefile” file.

Let’s check extra files in our directory by running the “ls”:

You will see that we’ve two object files “function.o” and “main.o”.

For removing the object files, write the following instructions in your “makefile” file. It will delete all files having the extension “.o”:

clean:
        rm *.o

Once done, save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+X”.

Afterward, run the “make clean” command and check whether the files are deleted using the “ls”:

make clean
ls

As you can see, abject files “function.o” and “main.o” are deleted.

Example 2: How to Run make Command in Debugging Mode?

If you want to run the make command in the debugging mode, use the “-d” flag. Debugging mode is particularly useful in scenarios when the user wants to troubleshoot or view the mistakes:

make -d

The make command is running in the debugging mode.

Example 3: How to Execute make Command Silently?

For running the make command silently such as do not print the execution of the command, use the “-s” flag:

make -s
ls

It can be observed that the make command has compiled the source code in silent mode.

Example 4: How to Trace the Disposition of Each Target?

If you want to trace the disposition of each target such as the execution of each file with names, utilize the “–trace” flag:

make --trace

Example 5: How to Ignore Errors in the Source Code?

If there’s any error in your source code but you still want to run it by ignoring the error, put the “-i” flag with the make command:

make -i

Note: Often, most of the users face errors like “make: *** No rule to make target”. If that is the case in your scenario, reach out to our dedicated guide on (Fix) make: *** No rule to make target for assistance.

Ending Tutorial

In this tutorial, we have seen the working of the make command Linux for compiling the source code of the projects. For compiling the source code, the set of instructions is defined in the “makefile” file. Additionally, the make command has various flags for different kinds of outputs such as “clean” for removing object files, and “-s” for running the make command silently. For detailed knowledge, look at the above tutorial.