GNU make

(June 2018)

The make utility recompiles only those parts of a program that need to be recompiled. It can do more than just compile C code — it automatically changes some files of any kind when other files change. It needs a “Makefile” to tell it what to do. The Makefile contains one or more rules.

Here’s a simple example. We write the text of our book in the Markdown file “”, and we want to ultimately produce “book.pdf”. We produce “book.pdf” using pdflatex to process “book.tex”. The “book.tex” file wraps/includes (\input{.body.tex}) the file “body.tex”. We produce “body.tex” from “” using pandoc. Our book contains a map, which we produce using Graphviz (dot).

We could do this using a shell script, like:

dot -Tsvg > map.svg
pandoc --wrap=none -f markdown+pipe_tables -o body.tex
pdflatex book.tex
pdflatex book.tex

…but this runs dot every time, even if when we didn’t change our map. Instead, using the following Makefile, make only run Graphviz when “” had changed.

book.pdf: book.tex body.tex img/map.svg
	pdflatex book.tex
	pdflatex book.tex

	pandoc --wrap=none -f markdown+pipe_tables -o body.tex

	dot -Tsvg > img/map.svg

The file must be named Makefile, and exist in the directory where we run make.

The general format of a rule is:

target: prerequisite …

The target is usually a file name, but doesn’t need to be. A prerequisite(s) must match the name of an existing file or a target in the Makefile. A rule does not require any prerequisites; zero prerequisites is valid. Any specified prerequisite must exist before the target can be created. For each prerequisite that matches a target, make evaluates the matching rule before proceeding (and so forth down the tree of prerequisites). For each target, if one of its prerequisites is newer, make runs its commands. Each command is passed to the shell (executed in its own sub-shell).

	rm book.pdf body.tex img/map.svg

We can specify a target on the command-line to limit what happens (e.g., make clean instead of simply make). By default, make runs the first rule in the Makefile (which make calls the “default goal”).

Long lines can be broken with backslashes (\).

Comment lines start with #.


Preview the build process without executing anything with make -n or make --dry-run.


objects = main.o kbd.o command.o display.o \
	insert.o search.o files.o utils.o
clean :
	rm mybinary $(objects)

Phony Targets

A phony target is one that is not really the name of a file, like “clean”.

	rm *.o *.tmp

What if our project contains a file named “clean”? Explicitly tell make that “clean” is a phony target with the special target .PHONY, like:

.PHONY: clean
	rm *.o *.tmp

Errors in Recipes

Make checks the exit status of each shell return. By default, make abandons execution of the current rule if a command return a non-zero exit status. To ignore errors from a command, prefix it with a dash:

	-rm -f *.o

Debugging Makefiles

$  make --just-print
$  make --print-data-base
$  make --warn-undefined-variables
$  make --debug


By default, make echos its commands to STDOUT. To silence a particular command, prefix it with @, like:

	@rm -f *.o

To silence everything, add:

ifndef VERBOSE

We can override the above by invoking make like:

$  make VERBOSE=1