Lab-04-MakeFile-Modul Programming
Lab-04-MakeFile-Modul Programming
1. Objectives
2. 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.
make minimizes repetition and speeds up compilation to speed up the process and save
time.
2.1. 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 to install it.
2.2. How Does the make Command Work?
The make command compiles different program pieces and builds a final executable.
The command reads a Makefile that contains rules, commands, and dependencies
required for the project. The purpose of make is to automate file compilation and make
the process less time-consuming.
When working with only a few files, compiling is straightforward. The process invokes
the compiler and lists the file names. For example, if there are three files:
• file1.c.
• file2.c.
• file3.h.
The first two are C source code files containing the main program logic. The file3.h is a
header file that contains declarations (like functions or variables) used by the C source
files.
Pages: 1
System Programming Labs
To invoke the compiler, run:
The gcc command has no output but creates an a.out file, a standard compiled executable.
When working with only a few source files (e.g., two or three), recompiling everything is
simple because you can run the gcc command that lists all the files.
When you work on larger applications with dozens, hundreds, or even thousands of source
files, recompiling everything is inefficient and time-consuming. Therefore, even if you
modify just one file, recompiling the entire project requires reprocessing all files. This
process can take a long time, especially if each file includes other files or has many
dependencies.
The make command automates this process by only recompiling the files that need to be
updated, which avoids unnecessary recompilation of unchanged files. When
executed, make searches the Makefile for instructions, e.g., file descriptions and
modification times. Additionally, the tool decides which files need to be updated and
issues the necessary commands.
3. What Are Makefiles?
A Makefile is a text file containing a set of rules that instruct make how to build an
application.
The Makefile basic syntax is:
target: dependencies
<TAB> commands
Pages: 2
System Programming Labs
• Target. Names of the file or action to be created or updated after executing make. It
can be an executable, object, or any other file.
• Dependencies (object files). Names of the files (separated by spaces) that the target
depends on.
• Commands. Rules describing how to create or update the target when
dependencies change.
Note: The commands in Makefiles always come after the TAB key. Otherwise, the
command does not work.
A Makefile contains several rules that tell make how to build a program. Each rule explains
how to create a target from object files.
The first rule is the most important, and it often describes how to build the final program
(the target) from the pieces the system has yet to create (the dependencies).
For example, when creating a target from two object files, the basic structure looks like
this:
After setting rule one, the next rule is about creating object files from 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 rules
mentioned above is:
Pages: 3
System Programming Labs
EXECUTABLE: Object_file_1 Object_file_2
<TAB> commands
Object_file_2: Source_file_2
<TAB> commands
The Makefile organizes all compilation rules in one place. This makes it easier to
understand and manage the build process compared to typing long compile commands
each time. If you change a source file, make knows exactly which object files need to be
recompiled and which parts of the program need updating. make only recompiles that
specific file and any files that depend on it.
4. Linux make Command Syntax
The basic make command syntax looks like this:
make [OPTIONS]
When executed without arguments, make builds the first target from the Makefile.
Pages: 4
System Programming Labs
Linux make Command Options
The make command is widely used due to its effectiveness, variety, and ability to perform
specific actions. While the command prints results when run without options, adding
arguments expands make's usability.
Command Description
-f file, --file=file, --
Uses a specific file as a Makefile.
Makefile=FILE
-l [load], --load- Specifies that no new task should be started if other tasks
average[=load] are in the queue.
Pages: 5
System Programming Labs
Command Description
-o file, --old-file=file, -- Ensures that make does not remake the file even if it is
assume-old=file older than the dependencies.
-p, --print-data-base Prints the database produced after reading the Makefile.
-s, --silent, --quiet Restricts printing the commands as they are executed.
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:
Pages: 6
System Programming Labs
1. Create a directory called Test using the mkdir command:
mkdir Test
cd Test
3. Make three source files: main.c, text.c, and text.h using the touch command:
#include "text.h"
int main() {
myText();
return 0;
}
Pages: 7
System Programming Labs
• #include "text.h". The include directive allows the compiler to access the
declarations of functions, types, and constants defined in text.h.
• int main(). The main function serves as the entry point for the program.
The int before main indicates this function returns an integer value, a whole
number. The return value from main serves as a signal to the operating system
about the program's execution status, with 0 indicating success and non-zero values
indicating an error.
• myText(). A function call invokes the myText() function, which is defined in a
different file.
• return 0. The return statement indicates the program has been completed
successfully.
The text.c file is a source file that defines the myText function declared in text.h.
Moreover, it includes necessary libraries and helps organize the code by separating
function declarations from their actual implementation.
#include <stdio.h>
#include "text.h"
void myText(void) {
printf("Learn about makefiles!\n");
return;
}
• #include <stdio.h>. This statement brings in the standard library that lets you use
functions like printf, which is used to display text on the screen.
• include "text.h". The include statement ensures the compiler knows the functions,
types, and constants declared in text.h.
Pages: 8
System Programming Labs
• void myText(void). The function definition determines a function
named myText that does not take any parameters and does not return a value.
• printf("Learn about makefiles!\n"). The function body is in curly braces. Inside,
the printf function is called to output the string "Learn about makefiles!\n" to the
console.
• return. The return function automatically returns control to the calling function
when it reaches the end.
8. Access text.h.
void myText(void);
In a header file, this line declares the function myText without providing its full
implementation. It informs other parts of the program that the myText function exists
and can be called, even though the actual code for the function is written in text.c.
4.3.
5. Create a Program
• Compile files by invoking the gcc compiler. This method is suitable for quick
compilation or smaller programs.
• Use make and Makefiles. This method is more efficient for larger projects with
multiple dependencies and source files.
Use the gcc compiler for simple programs or quick tests. Otherwise, use Makefiles when
working with a large number of files.
Pages: 9
System Programming Labs
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. gcc doesn't require compiling
the header because it's already included in the c files.
3. List all the files in the current directory with the ls command:
ls
The system creates the new executable, a.out. You can also see the executable via a file
explorer:
Note: Add -o [file_name].c at the end of the command to name the file differently.
4. Test whether the compilation was successful, with:
./a.out
Pages: 10
System Programming Labs
6. Create a Program with make and Makefiles
Creating a program with make and Makefiles simplifies the process of compiling and
managing multiple files in a project. Moreover, by using a Makefile, you automate
compilation steps and ensure only necessary parts of the program are rebuilt when
changes are made. make does this by checking the modification time of the files.
1. Create a new text document in the same directory as the source files and name
it Makefile.
2. Open the file and use the basic Makefile syntax as the guideline:
3. Type the new executable's name as the target, for example, my_app.
4. Add object files main.o and text.o as the dependencies (object files).
The make command recompiles the target if the object files are newer than the target.
5. Hit TAB and invoke the gcc compiler for the object files:
Pages: 11
System Programming Labs
<TAB> gcc main.o text.o
After writing the first rule, the Makefile looks like this:
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.
3. Write the following command to update text.o every time dependencies change:
gcc -c text.c
Make
Pages: 13
System Programming Labs
The make command creates two object files (main.o and text.o) and the executable
(my_app).
ls
The terminal shows that running the command created my_app. To run the my_app file,
type:
./my_app
When one source file is changed, make only updates object files that depend on that
source file. For instance, to change the text displayed when running my_app from "Learn
about Makefiles" to "Where am I?", follow these steps:
Pages: 14
System Programming Labs
3. Save the file, open the terminal, and run:
make
The make command detects changes in text.c and recompiles only that file. It also links the
updated object file to the executable.
./my_app
To compile all files and not only the changed files, use -B or --always-make options.
For example, change the text in the text.c file back to "Learn about Makefiles", save the
file, and enter:
make -B
The output shows that make compiles all the files in the directory, even the ones that
haven't been changed.
Pages: 15
System Programming Labs
Clean Object Files
When a user runs make for the first time, the command creates object files and the target
based on the data in the Makefile. However, to declutter the source directory and remove
object files and the executable, add the clean function to the Makefile:
clean:
<TAB> rm *.o my_app
make clean
After running ls, the terminal shows that the object files and my_app have been removed.
Run make in debug mode to print additional info about the compiling process. For
example, execute make with the -d option to display the debugging output:
make -d
Pages: 16
System Programming Labs
Use Different File as the Makefile
By default, make looks for a file called Makefile or makefile in the current directory.
However, to use another file, run:
make -f [file_name]
make -f my_file
Use Variables
C=gcc
main.o:main.c
<TAB> $ (C) -c main.c
Pages: 17
System Programming Labs
text.o: text.c text.h
<TAB> $ (C) -c text.c
When running make in the terminal, the command reads the C variable as gcc:
Conclusion
The examples in this tutorial explained how to use the make command in Linux to compile
files into executables.
Next, download the Linux commands cheat sheet to learn other important Linux
commands.
9.
Pages: 18
System Programming Labs