Build a multi-file C project with GNU Make
8 steps · about 1 h 58 min · Beginner
- Learn Makefile syntax with runnable examples (guide, Makefile Tutorial, 40 min)
Short examples build up targets, variables and patterns gradually.
- Write a rule with targets, prerequisites and a recipe (doc, GNU Project, 8 min)
The manual's basic unit of every Makefile.
- Compile several C files into one program (doc, GNU Project, 10 min)
A worked Makefile for a multi-file C editor program.
- Shorten recipes with $@, $< and $^ (doc, GNU Project, 10 min)
Automatic variables keep rules short and correct.
- Compile every .c file with one pattern rule (doc, GNU Project, 15 min)
One %.o: %.c rule replaces a rule per source file.
- Add clean and test targets with .PHONY (doc, GNU Project, 8 min)
Stops a stray file named clean from breaking your build.
- Generate header dependencies with -MMD and -MP (doc, GCC, 15 min)
Lets make rebuild objects when an included header changes.
- Include generated dependency files in the Makefile (doc, GNU Project, 12 min)
Shows how make reads the .d files the compiler writes.