make#
Makefile Syntax#
A Makefile consists of a set of rules. A rule has the following format:
<target>: <prerequisites>
<recipe>
- Target: Usually the name of a file generated by a program (eg. executables or objects). It can also be the name of an action to carry out (eg.
clean). - Prerequisites: Files used as input to create the target. Multiple prerequisites are separated by spaces. A target is rebuilt if any of its prerequisites change.
- Recipe: Actions that
makecarries out. Important: Every recipe line must begin with a tab character.
Multiple Prerequisites Example#
main: main.o utils.o
gcc -o main main.o utils.o
Special Targets and Variables#
.PHONY#
Defines targets that do not represent actual files. This prevents make from failing to run a command if a file with the same name exists in the directory.
.PHONY: clean
clean:
rm -f *.o
.DEFAULT_GOAL#
Sets the default target to run if no target is specified on the command line. By default, make uses the first target found in the file.
.DEFAULT_GOAL := all
Common Flags#
-f <file>: Use<file>as a Makefile.-C <dir>: Change to directory<dir>before reading the makefiles.-j [<jobs>]: Specifies the number of jobs to run simultaneously.
Examples#
- Run the default target:
$ make - Run a specific target:
$ make clean - Build using multiple cores:
$ make -j 4