make and Makefiles
Make and Makefiles
- make is a utility used to build executables from source code by following rules in a makefile.
- Automates the build process.
- Allows one to effeciently compile an executable.
- Reduce compile time by not recompiling source code that has not changed.
The following is known as a rule:
----------
target(s): prerequisite(s) (dependencies) (dependency list)
command(s) (sometimes called recipes)
----------
target(s): prerequisite(s) (dependencies) (dependency list)
command_1
command_2
command_3
.
.
.
command_n
target ... : dependency ...
command
...
...
...
Example:
----------
.PHONY : all clean
all: hello_world
hello_world: hello.cpp hello.h
g++ hello.cpp -o hello_world
# delete everything execpt source files
clean:
rm hello_world
rm *.o
A makefile consists of one or more Rules. Rules tell when and how to rebuild target(s).
The targets are sometimes called the goal of the rule.
make ultimately ensures that the goal(s) of the makefile are up-to-date.
Phony (Psuedo) Targets - target that does not have a real, corresponding file (file that we want to create).
all and clean are pseudo targets.
The pseudo targets will no longer work if a file with the
corresponding name really exists. Therefore, pseudo targets should be
explicitely stated in the Makefile.
To explicitely define a Psuedo Target, use the special, built-in
target name, .PHONY. (for other special built-in targets, see Special
Built-in Target Names).
To call a phony target, invoke make with the psuedo target name as the argument (e.g. make clean).
If no argument is specified, the first target (excluding targets
starting with a period) will be the default goal.
If no dependency is listed, then corresponding command(s) for that rule will always be executed.
To be safe, header files that are included in a given source file
should be listed as dependencies in the Makefile.
GNU make reference
Automatic variables