Pattern & Static Pattern Rules
A project with 50 source files should not need 50 compile rules. Make has three ways to write one rule for many files: pattern rules you write, built-in implicit rules Make already has, and static pattern rules that apply a pattern to an explicit list.
Pattern rules
A pattern rule has a % in its target. % matches any non-empty stem, and the same stem is substituted into the prerequisites:
%.o: %.c
$(CC) $(CFLAGS) -c lt; -o $@When Make needs geometry.o and has no explicit rule with a recipe for it, it finds this pattern, sets the stem to geometry, checks that geometry.c exists (or can be made) and runs the recipe. Inside, $* is the stem:
mkdir -p shapes && cd shapes
for n in circle square main; do printf 'int %s_id(void) { return 0; }\n' $n > $n.c; done
sed -i 's/int main_id(void) { return 0; }/int main(void) { return 0; }/' main.c
cat > Makefile <<'MK'
CFLAGS := -Wall -O2
OBJS := main.o circle.o square.o
shapes: $(OBJS)
gcc $^ -o $@
%.o: %.c
@echo "compiling stem '$*'"
gcc $(CFLAGS) -c lt; -o $@
MK
make -j1compiling stem 'main'
gcc -Wall -O2 -c main.c -o main.o
compiling stem 'circle'
gcc -Wall -O2 -c circle.c -o circle.o
compiling stem 'square'
gcc -Wall -O2 -c square.c -o square.o
gcc main.o circle.o square.o -o shapes
Header dependencies can be added separately with recipe-less rules (circle.o: shapes.h); Make merges them with the pattern rule's prerequisites.
The stem may include directories. build/%.o: src/%.c matches build/net/tcp.o with stem net/tcp and prerequisite src/net/tcp.c, which is how out-of-tree builds work.
Built-in implicit rules
Make already knows how to compile C, C++, Fortran, assemble, run yacc/lex and more. These built-in rules are why a Makefile that only says app: main.o still compiles main.c with cc -c -o main.o main.c. They use conventional variables:
| Variable | Used for | Default |
|---|---|---|
CC, CFLAGS |
C compiler and its flags | cc, empty |
CXX, CXXFLAGS |
C++ compiler and its flags | g++, empty |
CPPFLAGS |
preprocessor flags (-I, -D) for C and C++ |
empty |
LDFLAGS, LDLIBS |
linker flags (-L) and libraries (-lm) |
empty |
Built-in rules make tiny Makefiles tiny:
mkdir -p tiny && cd tiny
printf '#include <stdio.h>\n#include <math.h>\nint main(void) { printf("%%.3f\\n", sqrt(2.0)); return 0; }\n' > root.c
cat > Makefile <<'MK'
CFLAGS = -O2 -Wall
LDLIBS = -lm
root: root.o
MK
make -j1
./root
make -j1 -p | grep -x -A2 '%.o: %.c'cc -O2 -Wall -c -o root.o root.c
cc root.o -lm -o root
1.414
%.o: %.c
# recipe to execute (built-in):
$(COMPILE.c) $(OUTPUT_OPTION) lt;
The double spaces come from empty variables in the built-in templates. make -p prints Make's whole internal database, built-ins included; Debugging Makefiles shows how to read it.
Built-in rules are convenient for small things but have costs in big projects: Make tries many candidate rules for every file it can't find (.c, .cc, .s, RCS and SCCS files…). Most large Makefiles turn them off:
MAKEFLAGS += --no-builtin-rules # same as running make -r
.SUFFIXES: # also clear the old suffix rulesStatic pattern rules
A pattern rule applies to any file that matches. A static pattern rule applies only to the targets you list:
targets…: target-pattern: prerequisite-patterns…
recipeThat makes it explicit and safer: Make can't pick it up for some unrelated file, and a missing source is an error that names the real problem. It also lets different groups of files use different recipes:
mkdir -p mixed && cd mixed
printf 'int fast(void) { return 1; }\n' > fast.c
printf 'int safe(void) { return 2; }\n' > safe.c
printf 'int main(void) { return 0; }\n' > main.c
cat > Makefile <<'MK'
HOT := fast.o
COLD := safe.o main.o
app: $(HOT) $(COLD)
gcc $^ -o $@
$(HOT): %.o: %.c
gcc -O3 -c lt; -o $@
$(COLD): %.o: %.c
gcc -O1 -c lt; -o $@
MK
make -j1
echo "--- a missing source:"
rm safe.c safe.o
make -j1gcc -O3 -c fast.c -o fast.o
gcc -O1 -c safe.c -o safe.o
gcc -O1 -c main.c -o main.o
gcc fast.o safe.o main.o -o app
--- a missing source:
make: *** No rule to make target 'safe.c', needed by 'safe.o'. Stop.
The error names safe.c, the file that is really missing. With an ordinary pattern rule Make would instead have tried its other rules and complained about safe.o.
Choosing
| Use | When |
|---|---|
pattern rule %.o: %.c |
one recipe for every file of a kind; the usual choice |
static pattern rule $(OBJS): %.o: %.c |
the rule should apply only to a known list, or groups need different recipes |
| explicit rule | a one-off file with its own command |
Test yourself
In the rule build/%.o: src/%.c, Make needs build/net/tcp.o. What is $*?
tcpnet/tcpbuild/net/tcpsrc/net/tcp.c
What does MAKEFLAGS += --no-builtin-rules (or make -r) do?
- Disables all rules
- Disables Make's built-in implicit rules, so only your rules apply
- Stops variables from being expanded
- Turns off parallel builds
What is the advantage of a static pattern rule over a plain pattern rule?
- It runs faster recipes
- It applies only to the listed targets, so it can't match unrelated files
- It supports
lt; - It doesn't need a TAB