Targets, Prerequisites & Recipes
A Makefile is a list of rules. Every rule has the same three parts:
target: prerequisites
recipe line 1
recipe line 2| Part | Meaning |
|---|---|
| target | usually a file the rule creates (app, main.o); sometimes just a name for an action (clean) |
| prerequisites | the files (or other targets) the target is made from; Make brings them up to date first |
| recipe | shell commands that create the target; every recipe line starts with a TAB character |
When you type make, Make builds the default goal: the first target in the file (targets starting with . don't count). make main.o builds a specific target instead, and you can name several: make app test.
A three-file program
Let's split a program into two source files and give Make one rule per file it should create. The program prints the area of a rectangle using a small geometry module:
mkdir -p rect && cd rect
cat > geometry.h <<'SRC'
int area(int w, int h);
SRC
cat > geometry.c <<'SRC'
#include "geometry.h"
int area(int w, int h) { return w * h; }
SRC
cat > main.c <<'SRC'
#include <stdio.h>
#include "geometry.h"
int main(void) { printf("area = %d\n", area(6, 7)); return 0; }
SRC
cat > Makefile <<'MK'
rect: main.o geometry.o
cc -o rect main.o geometry.o
main.o: main.c geometry.h
cc -Wall -c main.c
geometry.o: geometry.c geometry.h
cc -Wall -c geometry.c
MK
make -j1
./rectcc -Wall -c main.c
cc -Wall -c geometry.c
cc -o rect main.o geometry.o
area = 42
Make read the first rule (rect), saw it needs main.o and geometry.o, built those first (depth-first, left to right), then linked. It echoes each recipe line before running it.
Only what changed
The point of the graph is the second build. Change one source file and only the object built from it, plus everything downstream, is redone:
mkdir -p rect && cd rect
printf 'int area(int w, int h);\n' > geometry.h
printf '#include "geometry.h"\nint area(int w, int h) { return w * h; }\n' > geometry.c
printf '#include <stdio.h>\n#include "geometry.h"\nint main(void) { printf("area = %%d\\n", area(6, 7)); return 0; }\n' > main.c
cat > Makefile <<'MK'
rect: main.o geometry.o
cc -o rect main.o geometry.o
main.o: main.c geometry.h
cc -Wall -c main.c
geometry.o: geometry.c geometry.h
cc -Wall -c geometry.c
MK
make -j1 >/dev/null
echo "--- edit geometry.c"
sleep 1 && touch geometry.c
make -j1
echo "--- edit geometry.h"
sleep 1 && touch geometry.h
make -j1
echo "--- build one target"
rm main.o
make -j1 main.o
make -j1 main.o--- edit geometry.c
cc -Wall -c geometry.c
cc -o rect main.o geometry.o
--- edit geometry.h
cc -Wall -c main.c
cc -Wall -c geometry.c
cc -o rect main.o geometry.o
--- build one target
cc -Wall -c main.c
make: 'main.o' is up to date.
The header is listed as a prerequisite of both objects, so touching it rebuilds both. Writing header lists by hand quickly goes wrong; Automatic Header Dependencies lets the compiler produce them.
When a prerequisite has no rule
If a prerequisite is missing and no rule says how to make it, Make stops with a precise message. That is the most common Makefile error you'll see, usually caused by a typo or a deleted file:
mkdir -p typo && cd typo
printf 'int main(void) { return 0; }\n' > main.c
cat > Makefile <<'MK'
app: mian.o
cc -o app mian.o
MK
make -j1
echo "exit status: $?"make: *** No rule to make target 'mian.o', needed by 'app'. Stop.
exit status: 2
Make exits with status 2 on errors, so scripts and CI can detect a failed build.
Rules without recipes, recipes without files
Two variations you'll use constantly:
- A rule without a recipe just adds prerequisites:
main.o: config.hon its own line addsconfig.hto whatever other rule buildsmain.o. - A target that is never created as a file, like
clean, always runs its recipe (because the "file" is always missing). Such targets should be declared.PHONY, covered in Phony Targets.
clean:
rm -f rect main.o geometry.oTest yourself
Which target does a plain make build?
- The last target in the file
- A target named
all, always - The first target in the file (not starting with a dot)
- Every target
geometry.h is a prerequisite of main.o and geometry.o. You touch only geometry.h. What is rebuilt?
- Nothing
- Only
rect - Both objects, then
rect - Only
geometry.o
What does No rule to make target 'mian.o', needed by 'app' mean?
- The compiler failed
mian.odoes not exist and no rule creates it- The recipe has spaces instead of a tab
appis up to date