Automatic Header Dependencies
A pattern rule %.o: %.c knows that main.o depends on main.c. It knows nothing about the headers main.c includes. Change a header, run make, and the build says "up to date" while your program still uses the old struct layout. That's the most dangerous kind of build bug: no error, just a wrong binary.
The bug
mkdir -p stale && cd stale
cat > config.h <<'SRC'
#define GREETING "hello"
SRC
cat > main.c <<'SRC'
#include <stdio.h>
#include "config.h"
int main(void) { puts(GREETING); return 0; }
SRC
cat > Makefile <<'MK'
app: main.o
gcc $^ -o $@
%.o: %.c
gcc -c lt; -o $@
MK
make -j1 && ./app
sleep 1
sed -i 's/hello/goodbye/' config.h
make -j1 && ./appgcc -c main.c -o main.o
gcc main.o -o app
hello
make: 'app' is up to date.
hello
You could list headers by hand (main.o: config.h), as in the first lessons, but those lists rot: someone adds an #include and forgets the Makefile. The compiler already knows every file it opens, so let it write the list.
Let the compiler write the dependencies
GCC and Clang can emit a small makefile fragment as a side effect of compiling:
| Flag | Meaning |
|---|---|
-MMD |
while compiling, write the dependencies to a .d file next to the output (user headers only; -MD also lists system headers) |
-MP |
also add an empty phony rule for each header, so deleting a header doesn't break the build |
-MF file |
write the dependency file to file instead of the default name |
-MT target |
name of the target in the generated rule (defaults to the -o output) |
The Makefile then pulls in every .d file that exists with -include. On the very first build there are none, which is fine: everything must be compiled anyway.
mkdir -p deps && cd deps
cat > config.h <<'SRC'
#define GREETING "hello"
SRC
cat > shapes.h <<'SRC'
int area(int w, int h);
SRC
cat > shapes.c <<'SRC'
#include "shapes.h"
int area(int w, int h) { return w * h; }
SRC
cat > main.c <<'SRC'
#include <stdio.h>
#include "config.h"
#include "shapes.h"
int main(void) { printf("%s %d\n", GREETING, area(2, 3)); return 0; }
SRC
cat > Makefile <<'MK'
SRCS := main.c shapes.c
OBJS := $(SRCS:.c=.o)
DEPS := $(OBJS:.o=.d)
CFLAGS := -Wall -O2
DEPFLAGS = -MMD -MP
app: $(OBJS)
gcc $^ -o $@
%.o: %.c
gcc $(CFLAGS) $(DEPFLAGS) -c lt; -o $@
-include $(DEPS)
MK
make -j1 && ./app
echo "--- main.d:"
cat main.dgcc -Wall -O2 -MMD -MP -c main.c -o main.o
gcc -Wall -O2 -MMD -MP -c shapes.c -o shapes.o
gcc main.o shapes.o -o app
hello 6
--- main.d:
main.o: main.c config.h shapes.h
config.h:
shapes.h:
main.d is an ordinary makefile: one rule listing everything main.c included, plus (thanks to -MP) an empty rule for each header.
Incremental rebuilds that are right
Now touch each header and watch Make rebuild exactly the objects that include it:
mkdir -p deps && cd deps
printf '#define GREETING "hello"\n' > config.h
printf 'int area(int w, int h);\n' > shapes.h
printf '#include "shapes.h"\nint area(int w, int h) { return w * h; }\n' > shapes.c
printf '#include <stdio.h>\n#include "config.h"\n#include "shapes.h"\nint main(void) { printf("%%s %%d\\n", GREETING, area(2, 3)); return 0; }\n' > main.c
cat > Makefile <<'MK'
OBJS := main.o shapes.o
app: $(OBJS)
gcc $^ -o $@
%.o: %.c
gcc -MMD -MP -c lt; -o $@
-include $(OBJS:.o=.d)
MK
make -j1 >/dev/null
echo "--- touch config.h (only main.c includes it):"
sleep 1 && sed -i 's/hello/goodbye/' config.h
make -j1 && ./app
echo "--- touch shapes.h (both include it):"
sleep 1 && touch shapes.h
make -j1
echo "--- nothing changed:"
make -j1--- touch config.h (only main.c includes it):
gcc -MMD -MP -c main.c -o main.o
gcc main.o shapes.o -o app
goodbye 6
--- touch shapes.h (both include it):
gcc -MMD -MP -c main.c -o main.o
gcc -MMD -MP -c shapes.c -o shapes.o
gcc main.o shapes.o -o app
--- nothing changed:
make: 'app' is up to date.
Why -MP matters
Suppose a refactoring removes config.h and the #include that used it. The old main.d still says main.o: main.c config.h. Without -MP, Make tries to build the missing header, finds no rule and stops, and you'd have to delete the .d files by hand. With -MP, config.h: is a target with no prerequisites and no recipe, so a missing config.h simply counts as "changed", main.o is recompiled, and the new main.d no longer mentions it:
mkdir -p mp && cd mp
printf '#define N 3\n' > config.h
printf '#include "config.h"\nint main(void) { return N - 3; }\n' > main.c
for flags in "-MMD" "-MMD -MP"; do
printf 'app: main.o\n\tgcc $^ -o $@\n%%.o: %%.c\n\tgcc %s -c lt; -o $@\n-include main.d\n' "$flags" > Makefile
rm -f main.o main.d app
make -j1 >/dev/null
echo "=== $flags: remove config.h"
printf 'int main(void) { return 0; }\n' > main.c
mv config.h config.h.bak
make -j1
mv config.h.bak config.h
printf '#include "config.h"\nint main(void) { return N - 3; }\n' > main.c
done=== -MMD: remove config.h
make: *** No rule to make target 'config.h', needed by 'main.o'. Stop.
=== -MMD -MP: remove config.h
gcc -MMD -MP -c main.c -o main.o
gcc main.o -o app
Details worth knowing
- Out-of-tree builds: with
-o build/main.o, GCC writesbuild/main.dand the rule namesbuild/main.o, so the same-include $(OBJS:.o=.d)line works (Out-of-Tree Builds). - Clean must delete the
.dfiles too. -MMDskips system headers (<stdio.h>), which rarely change; use-MDif you build against headers that do, such as a library you are developing in parallel.- Generated headers are the one case this can't solve on the first build: the
.dfile doesn't exist until after the first compile. Make generated headers an explicit (or order-only) prerequisite of the objects that need them. - Ninja reads the same
.dfiles, throughdepfileanddeps = gcc(Header Dependencies in Ninja).
Test yourself
What does -MMD do?
- Compiles without optimisation
- Writes a
.ddependency file as a side effect of compiling, listing user headers - Merges duplicate definitions
- Makes the output deterministic
Why use -include $(DEPS) instead of include $(DEPS)?
- It is faster
- On a clean tree the
.dfiles don't exist yet, and that must not be an error includecan't read.dfiles- It forces a rebuild
What problem does -MP solve?
- Parallel builds
- A deleted header listed in an old
.dfile stopping the build with 'No rule to make target' - Missing system headers
- Slow compiles