Reference

Last updated on 2026-11-15 | Edit this page

Running Make


To run Make:

BASH

$ make

Make will look for a Makefile called Makefile and will build the default target, the first target in the Makefile. In our project that is build/report.pdf.

To use a Makefile with a different name, use the -f flag e.g.

BASH

$ make -f other.mk

To build a specific target, provide it as an argument e.g.

BASH

$ make build/isles.dat

If the target is up-to-date, Make will print a message like:

OUTPUT

make: 'build/isles.dat' is up to date.

To see the actions Make will run when building a target, without running the actions, use the --dry-run flag e.g.

BASH

$ make --dry-run build/isles.dat

Alternatively, use the abbreviation -n.

BASH

$ make -n build/isles.dat

To list the documented targets of our Makefiles, use

BASH

$ make help

Trouble Shooting


If Make prints a message like,

ERROR

Makefile:3: *** missing separator.  Stop.

then check that all the actions are indented by TAB characters and not spaces.

If Make prints a message like,

ERROR

FileNotFoundError: [Errno 2] No such file or directory: 'books/%.txt'
make: *** [Makefile:14: build/isles.dat] Error 1

then you may have used the Make wildcard, %, in an action in a pattern rule. Make wildcards cannot be used in actions; use an automatic variable such as $< or $^ instead.

If a rule with several targets separated by &: misbehaves, check that your Make is version 4.3 or newer (make --version).

Makefiles


Rules:

target : dependency1 dependency2 ...
	action1
	action2
	...
  • Each rule has a target, a file to be created, or built.
  • Each rule has zero or more dependencies, files that are needed to build the target.
  • : separates the target and the dependencies.
  • Dependencies are separated by spaces.
  • Each rule has zero or more actions, commands to run to build the target using the dependencies.
  • Actions are indented using the TAB character, not 8 spaces.

Dependencies:

  • If any dependency does not exist then Make will look for a rule to build it.
  • The order of rebuilding dependencies is arbitrary. You should not assume that they will be built in the order in which they are listed.
  • Dependencies must form a directed acyclic graph. A target cannot depend on a dependency which, in turn depends upon, or has a dependency which depends upon, that target.

Grouped targets:

build/abyss.dat build/abyss.pdf &: analyze.py abyss.txt
	mkdir -p $(@D)
	python analyze.py abyss.txt build/abyss.dat build/abyss.pdf
  • &: says that a single run of the action builds all of the targets. With a plain :, Make would assume that each target needs its own run of the action.
  • Grouped targets require GNU Make 4.3 or newer.

Comments:

# This is a Make comment.

Line continuation character:

build/report.pdf : src/TeX/local.bib src/TeX/report.tex \
                   build/abyss.pdf build/isles.pdf \
                   build/results.tex build/abyss.head
  • If a list of dependencies or an action is too long, a Makefile can become more difficult to read.
  • Backslash,\, the line continuation character, allows you to split up a list of dependencies or an action over multiple lines, to make them easier to read.
  • Make will combine the multiple lines into a single list of dependencies or action.

Phony targets:

.PHONY : clean
clean :
	rm -rf build
  • Phony targets are a short-hand for sequences of actions.
  • No file with the target name is built when a rule with a phony target is run.

Automatic variables:

  • $< denotes ‘the first dependency of the current rule’.
  • $@ denotes ‘the target of the current rule’.
  • $^ denotes ‘the dependencies of the current rule’.
  • $* denotes ‘the stem with which the pattern of the current rule matched’.
  • $(@D) denotes ‘the directory part of the target’, e.g. build for build/abyss.dat.

Pattern rules:

build/%.dat : src/analysis/countwords.py books/%.txt
	mkdir -p $(@D)
	python $^ $@
  • The Make wildcard, %, specifies a pattern.
  • If Make needs a target matching the pattern, then it substitutes the matching part (the stem) into the dependencies.
  • The Make wildcard can only be used in targets and dependencies.
  • e.g. to build build/abyss.dat, Make uses the dependencies src/analysis/countwords.py and books/abyss.txt, and runs python src/analysis/countwords.py books/abyss.txt build/abyss.dat.

Defining and using variables:

export PYTHONPATH := src
COUNT_SRC := src/analysis/countwords.py
  • A variable is assigned a value. For example, COUNT_SRC is assigned the value src/analysis/countwords.py.
  • $(...) is a reference to a variable. It requests that Make substitutes the name of a variable for its value.
  • export passes the variable to the environment of every action. Our Makefiles use it so that Python finds modules in src/.

Suppress printing of actions:

.PHONY : help
help : Makefile
	@sed -n 's/^## / /p' $<
  • Prefix an action by @ to instruct Make not to print that action.

Include the contents of a Makefile in another Makefile:

include config.mk

wildcard function:

BOOKS := $(wildcard books/*.txt)
  • Looks for all files matching a pattern e.g. books/*.txt, and return these in a list.
  • e.g. BOOKS is set to books/abyss.txt books/isles.txt.

patsubst (‘path substitution’) function:

DATS := $(patsubst books/%.txt, build/%.dat, $(BOOKS))
  • Every string that matches books/%.txt in $(BOOKS) is replaced by build/%.dat and the strings are returned in a list.
  • e.g. if BOOKS is books/abyss.txt books/isles.txt this sets DATS to build/abyss.dat build/isles.dat.

Default targets:

  • The default target is the first target in the Makefile.
  • The special variable .DEFAULT_GOAL sets it explicitly e.g.
.DEFAULT_GOAL := build/report.pdf

Manuals


GNU Make Manual. Reference sections include:

Glossary


action
The steps a build manager must take to create or update a file or other object.
assignment
A request that Make stores something in a variable.
automatic variable
A variable whose value is automatically redefined for each rule. Make’s automatic variables include $@, which holds the rule’s target, $^, which holds its dependencies, and, $<, which holds the first of its dependencies, and $*, which holds the stem with which the pattern was matched. Automatic variables are typically used in pattern rules.
build file
A description of dependencies and rules for a build manager.
build manager
A program, such as Make, whose main purpose is to build or update software, documentation, web sites, data files, images, and other things.
default rule
The rule that is executed if no target is specified when a build manager is run.
default target
The target of the default rule.
dependency
A file that a target depends on. If any of a target’s dependencies are newer than the target itself, the target needs to be updated. A target’s dependencies are also called its prerequisites. If a target’s dependencies do not exist, then they need to be built first.
false dependency
This can refer to a dependency that is artificial. e.g. a false dependency is introduced if a data analysis script is added as a dependency to the data files that the script analyses.
function
A built-in Make utility that performs some operation, for example gets a list of files matching a pattern.
grouped target
One of several targets of a single rule whose actions build all of them at once. Written with &: in place of :; requires GNU Make 4.3 or newer.
incremental build
The feature of a build manager by which it only rebuilds files that, either directly or indirectly, depend on a file that was changed.
macro
Used as a synonym for variable in certain versions of Make.
Make
A popular build manager, created at Bell Labs in 1976. We use the GNU implementation, GNU Make.
Makefile
A build file used by Make, which, by default, are named Makefile.
pattern rule
A rule that specifies a general way to build or update an entire class of files that can be managed the same way. For example, a pattern rule can specify how to compile any C file rather than a single, specific C file, or, to analyze any data file rather than a single, specific data file. Pattern rules typically make use of automatic variables and wildcards.
phony target
A target that does not correspond to a file or other object. Phony targets are usually symbolic names for sequences of actions.
prerequisite
A synonym for dependency.
reference
A request that Make substitutes the name of a variable for its value.
rule
A specification of a target’s dependencies and what actions need to be executed to build or update the target.
stem
The part of the target that was matched by the pattern rule. If the target is file.dat and the target pattern was %.dat, then the stem $* is file.
target
A thing to be created or updated, for example a file. Targets can have dependencies that must exist, and be up-to-date, before the target itself can be built or updated.
variable
A symbolic name for something in a Makefile.
wildcard
A pattern that can be specified in dependencies and targets. If Make finds a dependency matching the pattern, then the pattern is substituted into the target. wildcards are often used in pattern rules. The Make wildcard is %.