Makefile Cheat Sheet: Rules, Variables, Automatic Variables and Pattern Rules
Checked against GNU make GNU make manual for make 4.4.1 (latest release); examples run on GNU Make 3.81 (the copy macOS ships) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for people who inherit a Makefile and need to read or fix it without learning all of GNU make. It follows the GNU make manual for version 4.4.1, the newest release on ftp.gnu.org today. Every example was run with GNU Make 3.81, the old copy Apple still ships with the Xcode command line tools, and the rows that need 4.x are marked untested.
When to use what
Think of a Makefile as a list of files and the commands that rebuild them when their inputs are newer. That is the job make is good at: C and C++ builds, generated docs, anything where skipping up-to-date work saves time. Many projects also use it as a task runner (make test, make deploy). That works fine as long as you mark those targets .PHONY, otherwise a stray file called test makes the target silently do nothing.
For variables, default to :=. It expands the right-hand side once, right there, which is what most people expect from a programming language. Use = only when you want late binding, such as a value that refers to something defined further down. Use ?= for settings a user may override from the environment, and += to append flags. Remember that command-line assignments like make CC=clang beat anything in the file.
If you are on a Mac, check make --version before copying recipes from the 4.x manual. We tried two features from the 4.4.1 manual on 3.81 and both failed: a ::= assignment was read as a rule, and --output-sync was an unrecognized option. Install a current GNU make if you need them. To start a new file from a template, try the Makefile generator. If your Makefile drives Docker or Git steps, the Dockerfile and Git sheets cover the commands inside the recipes, and the GitHub Actions sheet shows how to call make from CI. Recipes are plain shell, so the advice in our post on writing bash scripts safely applies to them too, and GitHub Actions CI/CD patterns has more on build pipelines.
A rule is targets: prerequisites followed by recipe lines. Every recipe line must start with a real tab character, not spaces. Each line runs in its own shell, so a cd on one line is forgotten on the next.
app: main.o util.o
cc -o app main.o util.oRebuild app whenever either object file is newer than it; the recipe line starts with a tab
.PHONY: all clean testMark targets that are names of tasks, not files, so they always run even if a file with that name exists
all: appMake the first target in the file the default; running plain make builds it
.DEFAULT_GOAL := helpPick the default target explicitly instead of relying on whichever rule comes first
@echo buildingA leading @ stops make from echoing the command before running it
-rm -f *.oA leading - tells make to ignore a failing command and carry on with the recipe
cd build && ./run.shChain commands on one line with && when they must share a working directory; separate lines get separate shells
echo $$HOMEWrite $$ to pass a literal $ through to the shell; a single $ is read by make first
Reference a variable as $(NAME) or ${NAME}. The setting functions are covered on the manual's Setting page: https://www.gnu.org/software/make/manual/html_node/Setting.html
A = $(B)Recursive: the value is expanded each time A is used, so later changes to B show up
C := $(D)Simple: expand D once, right now; if D is defined later, C stays empty
Y ?= defaultSet Y only if it has no value yet, from the file, the environment or the command line
CFLAGS += -WallAppend a space and the new text to an existing value
make CC=clangOverride a variable for one run from the command line; this beats assignments in the Makefile
OBJS := $(SRCS:.c=.o)Substitution reference: turn every .c suffix in the list into .o
$(origin CC)Tell you where a variable came from (default, environment, file, command line and so on)
X ::= valuePOSIX spelling of an immediate (:=) assignment; documented in 4.4.1 but GNU Make 3.81 fails on it
Pattern rules use % as a wildcard that matches the same stem on both sides. See https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html
$@The file name of the target being built
$<The first prerequisite, usually the source file in a compile rule
$^All prerequisites with duplicates removed, which is what you pass to a link step
$?Only the prerequisites that are newer than the target; empty when you force a rebuild with -B
$*The stem that % matched in a pattern rule, such as a for build/a.txt
$(@D)The directory part of the target, handy for mkdir -p $(@D) before writing into it
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@Pattern rule: build any .o from the .c file with the same stem
build/%.txt: src/%.mdPattern with a directory: the stem is matched inside the path on both sides
make -nDry run: print the commands make would run without running them
make -j4Run up to four recipes in parallel; plain -j sets no limit
make -C path/to/dirChange into a directory before reading its Makefile, the usual way to call a sub-project
make -f other.mk targetRead a file other than Makefile or makefile
make -B targetTreat every target as out of date and rebuild it unconditionally
make -kKeep going after a failed target and build whatever does not depend on it
make -q targetRun nothing; exit status 0 means the target is up to date
make -pPrint the database of rules and variables, including built-in ones, after reading the makefiles
make -j8 --output-sync=targetGroup each target's output together under parallel builds; 3.81 rejects this option
*** missing separator. Stop.Make could not parse a line, nearly always a recipe indented with spaces instead of a tab
*** No rule to make target `nope'. Stop.You asked for a target that is neither a rule nor an existing file; check spelling and paths
*** Recursive variable `A' references itself (eventually). Stop.An = variable refers to itself, as in A = $(A) more; use := or += instead
make: *** [fail] Error 1A recipe command exited non-zero; the line above it is the command's own error output
recipe commences before first target. Stop.A tab-indented line appears before any rule, often a stray tab at the top of the file
make: Nothing to be done for `all'.Not an error: every file the goal depends on is already up to date
Worked example: Build a two-file C program and watch make skip work
This is the Makefile we ran in a scratch folder with main.c, util.c and util.h. The recipe lines start with tabs.
CFLAGS := -O2
CFLAGS += -Wall
SRCS := main.c util.c
OBJS := $(SRCS:.c=.o)
.PHONY: all clean
all: app
app: $(OBJS)
$(CC) -o $@ $^
%.o: %.c util.h
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f app $(OBJS)
The first make printed three commands:
cc -O2 -Wall -c main.c -o main.o
cc -O2 -Wall -c util.c -o util.o
cc -o app main.o util.o
CC was never set in the file, so make used its built-in default cc. The substitution reference turned the source list into main.o util.o, and the pattern rule compiled each one with $< as the source and $@ as the object. The link step used $^, the full prerequisite list.
Running make again printed make: Nothing to be done for 'all'. because every object was newer than its sources. After we gave util.h a newer timestamp, make -n listed both compiles and the link again without running them, since both objects list the header as a prerequisite. That is the whole value of make: it rebuilds only what changed, as long as your prerequisites are honest.
Common mistakes
Indenting recipe lines with spaces because your editor converts tabs.
Set your editor to keep real tabs in files named
Makefileor*.mk, and runcat -t Makefileto see^Iwhere the tabs are.Writing
cd buildon one recipe line and expecting the next line to run insidebuild.Each line gets a new shell. Join them with
cd build && make install, or usemake -C build installfor sub-projects.Using
CC ?= gccand wondering why make still runscc.CCalready has a built-in default, so?=never fires. We confirmed$(origin CC)reportsdefault. UseCC := gccin the file or passmake CC=gcc.Forgetting
.PHONYon task targets liketestorclean.List them under
.PHONY. Without it, a file or folder namedtestmakes the target look up to date and the recipe never runs.Leaving headers out of the prerequisites, so edits to a
.hfile do not trigger rebuilds.Add the header to the rule, as in
%.o: %.c util.h, or have the compiler generate dependency files andincludethem.Writing
$HOMEin a recipe and gettingOMEor an empty string.Make expands
$Hbefore the shell sees it. Write$$HOMEto pass a shell variable through unchanged.
Related cheat sheets
Sources
- https://www.gnu.org/software/make/manual/make.html
- https://www.gnu.org/software/make/manual/make.txt
- https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
- https://www.gnu.org/software/make/manual/html_node/Error-Messages.html
- https://ftp.gnu.org/gnu/make/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.