Skip to content

Menu

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.o

Rebuild app whenever either object file is newer than it; the recipe line starts with a tab

.PHONY: all clean test

Mark targets that are names of tasks, not files, so they always run even if a file with that name exists

all: app

Make the first target in the file the default; running plain make builds it

.DEFAULT_GOAL := help

Pick the default target explicitly instead of relying on whichever rule comes first

@echo building

A leading @ stops make from echoing the command before running it

-rm -f *.o

A leading - tells make to ignore a failing command and carry on with the recipe

cd build && ./run.sh

Chain commands on one line with && when they must share a working directory; separate lines get separate shells

echo $$HOME

Write $$ 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 ?= default

Set Y only if it has no value yet, from the file, the environment or the command line

CFLAGS += -Wall

Append a space and the new text to an existing value

make CC=clang

Override 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 ::= value

POSIX spelling of an immediate (:=) assignment; documented in 4.4.1 but GNU Make 3.81 fails on it

From the docs, not run by us

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/%.md

Pattern with a directory: the stem is matched inside the path on both sides

make -n

Dry run: print the commands make would run without running them

make -j4

Run up to four recipes in parallel; plain -j sets no limit

make -C path/to/dir

Change into a directory before reading its Makefile, the usual way to call a sub-project

make -f other.mk target

Read a file other than Makefile or makefile

make -B target

Treat every target as out of date and rebuild it unconditionally

make -k

Keep going after a failed target and build whatever does not depend on it

make -q target

Run nothing; exit status 0 means the target is up to date

make -p

Print the database of rules and variables, including built-in ones, after reading the makefiles

make -j8 --output-sync=target

Group each target's output together under parallel builds; 3.81 rejects this option

From the docs, not run by us
*** 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 1

A 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

From the docs, not run by us
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 Makefile or *.mk, and run cat -t Makefile to see ^I where the tabs are.

  • Writing cd build on one recipe line and expecting the next line to run inside build.

    Each line gets a new shell. Join them with cd build && make install, or use make -C build install for sub-projects.

  • Using CC ?= gcc and wondering why make still runs cc.

    CC already has a built-in default, so ?= never fires. We confirmed $(origin CC) reports default. Use CC := gcc in the file or pass make CC=gcc.

  • Forgetting .PHONY on task targets like test or clean.

    List them under .PHONY. Without it, a file or folder named test makes the target look up to date and the recipe never runs.

  • Leaving headers out of the prerequisites, so edits to a .h file do not trigger rebuilds.

    Add the header to the rule, as in %.o: %.c util.h, or have the compiler generate dependency files and include them.

  • Writing $HOME in a recipe and getting OME or an empty string.

    Make expands $H before the shell sees it. Write $$HOME to pass a shell variable through unchanged.