Linux make Command with Examples

August 25, 2022

Introduction

The Linux make command is a commonly used utility by sysadmins and developers. The command assists in the compilation process and is a must-have tool for building large applications. This utility cuts out repetition and speeds up compilation, saving time.

This article will show you how to use the Linux make command with examples.

Linux Make Command With Examples

Prerequisites

  • Access to the terminal.
  • The make function installed.
  • A text editor.

Note: If make is not available on the system, use sudo apt install make.

How Does the make Command Work?

The make command compiles different program pieces and builds a final executable. The purpose of make is to automate file compilation, making the process simpler and less time-consuming. 

The command works with any programming language as long as the compiler can be executed with a shell command.

Compiling is straightforward when working with a few files. Therefore, the process includes invoking the compiler and listing file names.

For example, to compile a C program from three files (file1.c, file2.c, file3.h):

How Does the make Command Work?

Invoke the compiler with:

gcc file1.c file2.c file3.h

The gcc command creates an a.out file, which is a standard compiled executable.

a.out File Created in the Compile Folder

However, changing one of the source files requires recompiling everything, which is even more complicated when working with large apps. The make command automates the process, allowing users to update only pieces that need to be changed without recompiling every file.

The make command uses a user-generated file, Makefile, to compile program pieces. When executed for the first time, make searches the Makefile for instructions, e.g., file descriptions and modification times. Based on the available data, make decides which files need to be updated and issues the necessary commands.

What Are Makefiles?

A Makefile is a text file containing a set of rules that instructs make how to build an application. A rule consists of three parts: the target, dependencies, and command(s).

Makefile Target and Dependencies

The Makefile basic syntax is:

target: dependencies
<TAB> commands

Parts of the syntax are:

  • Targets. Names of the files to be created or updated after executing make.
  • Dependencies. Names of the files (separated by spaces) from which the target is constructed.
  • The commands. Rules describing how to create or update the target when dependencies change.

One Makefile has several sets of rules. The first rule is the default one and states how the final executable (the target) is to be made from not-yet-created object files (dependencies):

Makefile Executable and Object Files

The syntax in this case is:

EXECUTABLE: Object file 1, Object file 2
<TAB> commands

Note: The commands in Makefiles always come after the TAB key. Otherwise, the command does not work.

After setting rule one, the user adds instructions on how to create object files:

Makefile Executable and Object Files and Source Files

The make command works by compiling source files into object files and then compiling object files into the target file, the final executable. The Makefile syntax with the three rules mentioned above is:

EXECUTABLE: Object file 1, Object file 2
<TAB> commands

Object file 1: Source file 1, Source file 2
<TAB> commands

Object file 2: Source file 2
<TAB> commands

Linux make Command Syntax

The basic make syntax looks like this:

make [OPTIONS]

When executed without arguments, make builds the first target from the Makefile.

Linux make Command Options

The make command is widely used due to its effectiveness, variety, and the ability to perform specific actions. While the command prints result when run without options, adding arguments expands make's usability.

Here are the most used options:

CommandDescription
-B, --always-makeUnconditionally compiles all targets.
-d, --debug[=FLAGS]  Prints the debugging information.
-C dir, --directory=dir  Changes the directory before executing the Makefile.
-f file, --file=file, --Makefile=FILE  Uses a specific file as a Makefile.
-i, --ignore-errors  Ignores all errors in commands.
-I dir, --include-dir=dir  Specifies a directory to search for the specified Makefile.
-j [jobs], --jobs[=jobs]  Specifies the number of jobs to run simultaneously.
-k, --keep-going  Continues running make for as long as possible after getting an error.
-l [load], --load-average[=load]  Specifies that no new task should be started if other tasks are in the queue.
-n, --dry-run, --just-print, --recon  Prints expected output without executing make.
-o file, --old-file=file, --assume-old=file  Ensures that make does not remake the file even if it is older than the dependencies.
-p, --print-data-basePrints the database produced after reading the Makefile.
-q, --question  Activates the Question mode, in which make doesn't run any commands but returns an exit status zero if the target is already compiled.
-r, --no-builtin-rules  Eliminates the built-in implicit rules.
-s, --silent, --quiet  Restricts printing the commands as they are executed.
-S, --no-keep-going, --stop  Stops "-k, --keep-going" command.
-t, --touch  Touches files instead of running the commands.
--trace  Traces each target's disposition.
-W file, --what-if=file, --new-file=file, --assume-new=file  Ignores the fact that the target file has been modified.
--warn-undefined-variablesWarns that an unknown variable is referenced.

Linux make Command Examples

The best way to understand make and Makefiles is by running the command and different options on a simple program.

For example, to build an executable that prints the message "Learn about Makefiles", follow these steps:

1. Create a directory called Test.

2. Make three source files main.c, text.c, and text.h:

Linux make Command Examples
  • main.c - is the file with the main function (int main) that calls a function from another file.
main.c File
  • text.c - is the file with the you want to print "Learn about Makefiles!".
text.c File
  • text.h  - is the header file with declarations for the functions. The header is included in both c files with the #include argument, which has the same purpose as copy/pasting the header's contents.
text.h File

The following sections illustrate some common use cases of make and Makefiles on the three files mentioned above.

Note: The following examples are written in C language.

Create a Program

There are two ways to create a program:

  • Compiling files the standard way by invoking the gcc compiler. This method is suitable for smaller programs.
  • Using make and Makefiles.

Create a Program with gcc Compiler

Use the gcc compiler for simple programs only. Otherwise, use Makefiles when working with a large number of files.

To create a program with the compiler:

1. Open the terminal and navigate to the directory containing the files.

2. Invoke the gcc compiler and type the name of both c files. The header doesn't get compiled because it's already included in c files.

gcc main.c text.c
gcc Terminal Output

3. List all the files in the current directory with the ls command:

ls
gcc and ls Terminal Output

The terminal shows that the new executable a.out file is created. The executable is also seen via a file explorer:

a.out File Created in the Test Folder

To test whether the compilation was successful, invoke the executable with:

./a.out
Invoking the a.out Executable Terminal Output

The terminal shows that the executable works properly.

Create a Program with Make and Makefiles

Compiling files with make and Makefiles is simpler than using the compiler. Start by creating a new text document in the same directory and name it Makefile or makefile.

Makefile in the Test Folder

Open the file and use the basic Makefile syntax as the guideline:

Makefile Syntax

1. Type the new executable's name as the target, for example, my_app.

Makefile Target

2. Add object files main.o and text.o as the dependencies. The make command recompiles the target every time object files change.

Makefile Dependencies

3. Hit TAB and invoke the gcc compiler for the object files:

<TAB> gcc main.o text.o
Makefile Commands

4. Add the -o flag and name the target my_app.

Makefile -o Flag

After writing the first rule, the Makefile looks like this:

my_app: main.o text.o
<TAB> gcc main.o text.o -o my_app
Makefile With the First Rule Completed

Next, instruct the Makefile on how to create main.o:

1. Set main.o as the target.

2. Type in main.c as the dependency. main.c serves to create and update main.o.

3. Write the following command to update main.o every time main.c changes:

gcc -c main.c

4. Add the -c flag to instruct the Makefile not to create a new executable but only to read the code and compile the object file.

main.o: main.c
<TAB> gcc -c main.c
Makefile for Object File main.c

To create text.o, set that file as the target, and add both text.c and text.h as dependencies. However, the gcc command only compiles the text.c file, since headers are never compiled:

text.o: text.c text.h
<TAB> gcc -c text.c
Makefile for Object File text.c

Save the Makefile and type make in the terminal.

make Command Terminal Output

The make command created two object files (main.o and text.o) and the executable (my_app).

To verify that make created new files, run ls again:

make and ls Commands Terminal Output

The terminal shows that running the command created my_app. To run the my_app file, type:

./my_app
Executing my_app Terminal Output

Update the Program

When one source file is changed, make only updates object files depending on that source file. For instance, to change the text displayed when running the my_app from "Learn about Makefiles" to "Where am I?":

1. Open text.c in the text editor:

Changing the text.c Message

2. Change the text to "Where am I?":

Changed the text.c File Message

3. Save the file, open the terminal, and run make:

Make Command After the Changes Terminal Output

The make command detected changes in text.c and recompiled only that file.

To verify the change, run the executable:

./my_app
ls Command to Verify Changes

Compile All Files

To compile all files and not only the changed files, use -B or --always-make options.              

For example, to change the text in the text.c file back to "Learn about Makefiles" and save the file, enter:

make -B
make -B Terminal Output

The output shows that make compiled all the files in the folder, even the ones that haven't been changed.

Clean Object Files

When a user runs make for the first time, the command creates object files and the executable. Therefore, to declutter the source folder and clean object files, add the clean function to the Makefile:

clean:
<TAB> rm *.o my_app
Makefile and Clean Command

The command consists of:

  • The clean target with no dependencies - the target is always considered outdated and always executed.
  • The rm command - removes specified objects.
  • The *.o part - matches files with the o extension and cleans object files and my_app.

To clean object files, run:

make clean
make clean and ls Terminal Output

After running ls again, the terminal shows that the object files and my_app have been removed.

Run make in Debug Mode

Run make in debug mode to print additional info about the compiling process. Execute make with the -d option to display the debugging output:

make -d
make -d Terminal Output

Use Different File as the Makefile

By default, make looks for a file called Makefile or makefile in the current directory. To use another file, run:

make -f [file_name]

For example, if a Makefile is named my_file, execute:

make -f my_file
make -f Terminal Output

Use Variables

Variables in Makefiles represent multiple file names, arguments, targets, dependencies, commands, source directories, or other items. Furthermore, a variable is defined by a name and represents a string of text called the variable's value.

To define a variable, use =. For example, substitute gcc with a variable C.

C=gcc

my_app: main.o text.o
<TAB> $ (C) main.o text.o -o my_app

main.o:main.c
<TAB> $ (C) -c main.c
        
text.o: text.c text.h
<TAB> $ (C) -c text.c
make Command Variables

When running make in the terminal, the command reads the C variable as gcc:

Make Command with Variables Terminal Output

Conclusion

After going through the examples in this tutorial, you know how to use the make command in Linux and how it works.

Next, download the Linux commands cheat sheet to learn other important Linux commands.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Set Environment Variables in Linux
December 17, 2020

Environment variables play a significant role in setting up the system and shell configuration at the beginning of every shell session. This tutorial explains how to view, set, manage...
Read more
How to Uninstall or Remove Software Packages From Ubuntu
September 4, 2019

This guide will walk you through several methods for removing old or unwanted software from an Ubuntu Linux system...
Read more
How to Install a Desktop (GUI) on an Ubuntu Server
August 4, 2022

Want to add a desktop environment after you install Ubuntu server? By default, Ubuntu Server does not include a Graphical User Interface (GUI) and that...
Read more
How to Build Linux Kernel From Scratch {Step-By-Step Guide}
November 12, 2020

All Linux distributions come with a predefined kernel. However, they are usually outdated. Follow this step-by-step guide to learn how to build a Linux kernel from scratch...
Read more