Build a multi-file C project with GNU Make

8 steps · about 1 h 58 min · Beginner

  1. Learn Makefile syntax with runnable examples (guide, Makefile Tutorial, 40 min)

    Short examples build up targets, variables and patterns gradually.

  2. Write a rule with targets, prerequisites and a recipe (doc, GNU Project, 8 min)

    The manual's basic unit of every Makefile.

  3. Compile several C files into one program (doc, GNU Project, 10 min)

    A worked Makefile for a multi-file C editor program.

  4. Shorten recipes with $@, $< and $^ (doc, GNU Project, 10 min)

    Automatic variables keep rules short and correct.

  5. Compile every .c file with one pattern rule (doc, GNU Project, 15 min)

    One %.o: %.c rule replaces a rule per source file.

  6. Add clean and test targets with .PHONY (doc, GNU Project, 8 min)

    Stops a stray file named clean from breaking your build.

  7. Generate header dependencies with -MMD and -MP (doc, GCC, 15 min)

    Lets make rebuild objects when an included header changes.

  8. Include generated dependency files in the Makefile (doc, GNU Project, 12 min)

    Shows how make reads the .d files the compiler writes.