A Simple Introduction to Make
GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program’s source files.
Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program.
I will introduce some basic skills about using make.
Format of make
The format of make rule is:
1 | target: prerequisite |
target
is the output or middle objects. prerequisite
is the requiring files for target. When prerequisite
files have update, then when you execute make
command, the utility will generate target. the command
indicates how to generate target
. command
can be any shell commands. But generally, commmand
contains the compiling commands. A example of make command:
1 | file: file.c file.h |
There can be many targets in make file, but the first target will be executed when type make
.
##Define variables
We can define variables and use it in Makefile. for example:
1 | OBJ = file.o |
In this example, we define OBJ
as file.o
and use it later to replace file.o
.
It can be quite useful if there are many objects files in target or prerequisite.
Sometimes we can move object files or head files to other directories, at this time, we can define variables to reduce our typing. For example, you have *.h
file in lib
directory in current path, you can write like this:
1 | LIB = lib |
Phony target
Phony target is a kind of label in make. It’s similar to target, but it has no prerequisite for most time, and we can append it to make
command to execute command defined in it. For example:
1 | .PHONY: clean |
When we type make clean
in command line, rm *.o file
will be executed.
NOTE: in order to avoid phony target has the same name with file in directory, we add .PHONY clean
to make sure that clean command must be executed.
Sometimes phony target can have prerequisite, and place it as the first target, then this phony target will be execute. This is very helpful when you want generate several executable files and you just want type a make
. For example:
1 | all: prog1 prog2 prog3 |
Automatic variables
There are some default variables in each make rule. We can use it to simplify our work. There are some useful automatic variables:
1 | $@: The file name of the target of the rule |
For example, if we have a makefile like this:
1 | CC=gcc |
Where $@
indicates the .o
file and @<
indicates the corresponding .c
file.
Other skills
- comments begin with
#
, just like shell - the comment begin with
@
will not be display, so we can echo like this:1
@echo 'Compiling begin...'
- We can choose make file using
-f
options:make -f myMakefile
will choosemyMakefile
as rule file. - Adding
-n
in make will not do make really, just test if all things are okay. - We can use
include
to add other makefiles into here, for example:include Makefile1 Makefile2
. - Add
-
in front of a command will ignore the errors occurring when execute it.