make is old software that still builds a great deal of C code, and plenty of projects use it today just to collect their common commands under short names: make test, make deploy, make clean. It is simpler than it looks once one rule is clear: make compares file timestamps and runs only what is out of date. Every example here was run with GNU Make 4.3 on Ubuntu 24.04, against a two-file C program.
A rule has three parts
Target, prerequisites, recipe
The test project was two source files, main.c and util.c, and this Makefile:
CC = gcc
CFLAGS = -Wall -O2
OBJS = main.o util.o
app: $(OBJS)
$(CC) $(CFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f app $(OBJS)
Each rule reads: to make the target on the left of the colon, first make sure the prerequisites on the right are up to date, then run the indented recipe. app depends on the two object files. Each .o file depends on its .c file through the pattern rule %.o: %.c. clean depends on nothing.
The three automatic variables
The symbols inside the recipes save repeating names. The GNU make manual defines them:
| Variable | Meaning |
|---|---|
$@ |
The file name of the target of the rule |
$< |
The name of the first prerequisite |
$^ |
The names of all the prerequisites, with spaces between them |
So the app recipe expands to gcc -Wall -O2 -o app main.o util.o, and the pattern rule compiles each .c file into its matching .o.

Only what changed gets rebuilt
Three runs
The first make compiled everything:
$ make
gcc -Wall -O2 -c main.c -o main.o
gcc -Wall -O2 -c util.c -o util.o
gcc -Wall -O2 -o app main.o util.o
The second did nothing:
$ make
make: 'app' is up to date.
Then util.c was touched to update its timestamp:
$ make
gcc -Wall -O2 -c util.c -o util.o
gcc -Wall -O2 -o app main.o util.o
main.c was not recompiled. The manual describes the logic in the terms of its own example: an object file is recompiled if its source file, or any header listed as a prerequisite, is more recent than the object file, or if the object file does not exist. Everything make does follows from that comparison, repeated down the chain of prerequisites.
On two files the savings are invisible. On a project with hundreds of source files, it is the difference between a build of seconds and a build of many minutes.
Seeing without doing
make -n prints what would run without running it. After touching main.c, it listed exactly the two commands needed and executed neither.
The default goal
Typing make with no target builds the first one in the file. The manual calls it the default goal: make starts with the first target, not counting targets whose names start with a dot. A separate test file with targets first and second printed “first target” when run with plain make. That is why all: or the main program is conventionally the first rule.
.PHONY: when a target is not a file
The clean that stopped working
clean creates no file called clean, so make runs its recipe every time. Until a file with that name appears. The test:
$ make # build app
$ touch clean # a file called "clean" now exists
$ make clean
make: 'clean' is up to date.
$ ls app
app
Nothing was deleted. The manual explains the trap with its own clean example: if a file named clean is ever created, then since the target has no prerequisites, clean would always be considered up to date and its recipe would not be executed.
The fix
.PHONY: clean
Added to the Makefile, the same make clean ran rm -f app main.o util.o and the program was gone, file named clean or not. The manual describes a phony target as one that is not really the name of a file, just a name for a recipe to run on request. It lists two reasons to declare one: to avoid a conflict with a file of the same name, and to improve performance.
Any target that names an action rather than a file belongs in .PHONY: clean, test, install, deploy, all, help. A directory called test is a common real-world collision.

Recipes: one tab, one shell per line
missing separator
The single most common Makefile error:
$ make -f Bad.mk bad
Bad.mk:2: *** missing separator. Stop.
Bad.mk had its recipe indented with four spaces. The manual is explicit: each line in the recipe must start with a tab, or the first character of the .RECIPEPREFIX variable if that is set. Editors that convert tabs to spaces produce exactly this error, and the message does not mention tabs. Most code editors can be told to keep real tabs in files named Makefile, and cat -A Makefile shows a tab as ^I, which makes the problem visible in a second.
The same page describes the file as having two syntaxes in one: most of a Makefile is make’s own syntax, but recipes are handed to the shell, and make does very little to them first.
cd that does not stick
A recipe with three lines:
t:
cd /tmp
pwd
cd /tmp && pwd
The output:
/tmp/mkHaKx
/tmp
The first pwd printed the original directory, not /tmp. The manual states the rule: recipes are executed by invoking a new sub-shell for each line of the recipe, unless .ONESHELL is in effect. A cd or a shell variable set on one line is gone on the next. The manual’s advice is to put dependent commands on one line joined with &&, which also means the second command does not run if the first fails.
@ and –
Two prefixes change how a recipe line behaves. @ stops make from printing the command before running it. - tells make to carry on if that line fails. In the test, a recipe with -false printed:
make: [At.mk:4: t] Error 1 (ignored)
and the next line still ran. Without the dash, make stops at the first failing command.
Variables: = and :=
Two flavors
The manual devotes a page to the two flavors of variables, which differ in when the value is worked out. The test:
A = $(B)
B = later
C := $(D)
D = later
t:
@echo "A=[$(A)] C=[$(C)]"
A=[later] C=[]
A used =, which is expanded when it is used, by which time B had a value. C used :=, which is expanded once, at the point of assignment, when D did not exist yet. := behaves like a variable in most programming languages and avoids surprises in long Makefiles. = is useful when a value should pick up later definitions.

A Makefile as a list of commands
Nothing to compile
Many projects never compile anything with make. They use it as a menu of the commands a developer runs every day. A Makefile built that way, every target phony:
.DEFAULT_GOAL := help
.PHONY: help test lint deploy
PORT ?= 8000
help: ## Show this list
@grep -E '^[a-z]+:.*## ' $(MAKEFILE_LIST) | sed 's/:.*## /\t/'
test: ## Run the test suite
@echo "running tests on port $(PORT)"
lint: ## Check style
@echo linting
deploy: test lint ## Test, lint, then deploy
@echo deploying
Plain make now prints a menu, because .DEFAULT_GOAL, which the manual names as the way to override the first-target rule, points at help:
$ make
help Show this list
test Run the test suite
lint Check style
deploy Test, lint, then deploy
make deploy ran test and lint first, because they are its prerequisites, then deployed. The ordering logic built for compiling C works just as well for “never deploy without testing.”
Settings from the command line
PORT ?= 8000 sets a default only if the variable is not already set. In the test, make test used 8000, make test PORT=9000 used 9000, and PORT=7000 make test, which sets it in the environment instead, used 7000. One file serves every developer and every machine without editing.
Parallel builds
-j
Independent targets can be built at the same time with -j. A test Makefile with three targets that each sleep one second:
| Command | Time |
|---|---|
make |
3.0 s |
make -j3 |
1.0 s |
On a multi-core machine make -j$(nproc) is the usual form. Parallel builds expose missing prerequisites, though: if a rule quietly relied on another target happening to finish first, -j can run them in the wrong order. A build that works serially and fails with -j almost always has a prerequisite missing from a rule.
Sources
- GNU make manual, “How make Processes a Makefile”: https://www.gnu.org/software/make/manual/html_node/How-Make-Works.html
- GNU make manual, “Phony Targets”: https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
- GNU make manual, “Recipe Syntax”: https://www.gnu.org/software/make/manual/html_node/Recipe-Syntax.html
- GNU make manual, “Recipe Execution”: https://www.gnu.org/software/make/manual/html_node/Execution.html
- GNU make manual, “Automatic Variables”: https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
- GNU make manual, “The Two Flavors of Variables”: https://www.gnu.org/software/make/manual/html_node/Flavors.html
