make Command in Linux



In software development, managing project dependencies is important. One tool that helps us automate this process in Linux is the make command. It is a useful tool in the software development workflow for Unix-like environments. It helps with the process of compiling and is essential for building large applications. It reduces repetition and speeds up compilation, saving time and making the process faster.

Table of Contents

Here is a comprehensive guide to the options available with the make command −

make Command in Linux

Make is a command-line utility in Linux that helps developers save time by automating repetitive tasks during the build process. It ensures only the changed parts of a project are recompiled. This practice speeds up the process and reduces errors. In modern development, make is integrated into CI/CD pipelines to automatically build, test, and deploy changes, which ensures software reliability.

Moreover, make is preinstalled on Linux systems. You can confirm the make's installation on your system by executing the following command −

make -v
make Command in Linux1

In case make is not installed on your system, you can use the system's package manager to install it.

Syntax of make Command

The basic syntax of the make command is as follows −

make [options] [target]

Here, options are the command-line options that modify the make command's behavior, and target is the name of the target to build, which typically corresponds to a set of instructions in the Makefile.

make Command Options

Here is a list of make command options along with the description −

Option Description
make It executes the default target, usually the first target in the Makefile.
make all It runs all targets, typically compiling the entire project.
make clean It runs the clean target, removing compiled files and cleaning the build directory.
make -k It continues running as much as possible even if an error occurs.
make -f gfg It uses a custom file (like gfg) for the build process.
make -jN It runs the build with N parallel jobs, speeding up the process on multi-core systems.
make -B It forces make to consider all targets, ignoring conditions.
make --version It shows the version of make.
make --help Shows helpful information about make commands and options.
make -C <dir> Changes to directory <dir> before reading the Makefile and starting the build process.
make -n Shows what would be done without executing any commands, helpful for debugging purposes.
make -s Suppresses command output, useful when you don't want to see the actual commands being executed during the build process.
make - t Updates the timestamps of targets to make them appear up-to-date without actually updating or building them, often used for testing makefiles.
make -r Disables the use of built-in implicit rules in make, which can help in troubleshooting or optimizing custom build processes.
make -p Prints the database of rules and variables after reading the Makefile, useful for diagnosing how make interprets the build instructions.
make -v Displays the version of make command.

You can access the manual page of the make command to get more details −

man make
make Command in Linux2

Example of make Command in Linux

Let's go through a simple example to understand how the make command works in Linux. We will use the make command on Ubuntu 24.04, but you can execute this on any Linux-based operating system −

Step 1: Create Python Files

Open the terminal, create a new Python file named makeExample.py, and write the following code in it. You can write any code you like, but for this example, the program will print a message "welcome to tutorialspoint.com" −

print("welcome to tutorialspoint.com")

After this create another Python file named makeExample2.py and use the for loop to print integers between the range 1-6 −

for i in range(1, 6):
	print(i)

Step 2: Create a Make File

After saving makeExample.py and makeExample2.py, create a Makefile (named exactly like this). The make command automatically looks for a Makefile by default. If you choose a different name for the file, you must specify it using the -f option when running the command.

Here's the content for the Makefile −

all: makeExample makeExample2

makeExample:
	python makeExample.py
   
makeExample2:
	python makeExample2.py

Step 3: Execute the Make File

After saving the Makefile, you can run it by simply typing make in the terminal. This will execute both Python files −

make

This command executes both Python scripts in sequence −

make Command in Linux3

Step 4: Run Specific Targets Separately

You can run individual targets separately. For example, to execute only the makeExample target, we can use the following command −

make makeExample

This time the make command executes only the specified target −

make Command in Linux4

That's all about the Linux make command.

Conclusion

The make command is a powerful tool in Linux that simplifies the software build process, automating repetitive tasks and ensuring efficient project management. By specifying dependencies and commands in a Makefile, developers can streamline project compilation, test specific targets, and save time, especially on large applications.

In modern development, the make command integrates seamlessly into CI/CD pipelines, contributing to reliable software delivery. In this article, we explained the basics of the make command, including basic syntax, common options, and practical usage examples to enhance your workflow on Linux.

Advertisements