Learn Make

Rebuild only what changed: the classic dependency-driven build tool

14 lessons · 3 hours · 4 levels · covers Make GNU Make 4.4

Make reads a Makefile of rules saying how each file is built from others and reruns only the steps whose inputs changed. It builds C and C++ projects, and many teams also use it as a simple, universal task runner for tests, linting and deploys.

Before you start

Make builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 40 min in total
2. PracticalUse it productively at work, with the usual idioms8 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs11 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know14 lessons · 3 hours in total

The Make tree

MakeRules: targets, prerequisites, recipesRules: targets,prerequisites, recip…How make decides what to rebuildHow make decideswhat to rebuildPhony targets and make as a task runnerPhony targets andmake as a task run…Variables: =, :=, ?= and +=Variables: =, :=, ?=and +=Automatic variables: $@, $<, $^Automatic variables:$@, $<, $^Pattern rules and built-in rulesPattern rules andbuilt-in rulesText functions: wildcard, patsubst, foreachText functions:wildcard, patsubst,…How recipes run in the shellHow recipes run inthe shellVariablesVariablesQuoting: single, double and noneQuoting: single,double and noneExit status, && and ||Exit status, && and||
Essentials and Practical shown (8 of 14 lessons); the full list is below. Click a lesson to read it.

All 14 lessons, in order

Rules and targets

  1. Rules: targets, prerequisites, recipes · Essentials · 10 min

    A Makefile is a list of rules: a target, the prerequisites it depends on, and a tab-indented recipe of shell commands that builds it.

  2. How make decides what to rebuild · Essentials · 8 min

    make compares modification times: a target is rebuilt when it doesn't exist or any prerequisite is newer than it.

  3. Phony targets and make as a task runner · Essentials · 10 min

    Declare targets that aren't files (all, clean, test) with .PHONY, and use a Makefile as a project's command menu.

Variables and functions

  1. Variables: =, :=, ?= and += · Essentials · 12 min

    Define and use variables with $(NAME), and know the difference between recursive =, simple :=, conditional ?= and append +=.

  2. Text functions: wildcard, patsubst, foreach · Practical · 12 min

    Compute file lists and flags with make's functions: wildcard, patsubst, subst, filter, addprefix, notdir, foreach and shell.

Patterns and recipes

  1. Automatic variables: $@, $<, $^ · Practical · 10 min

    Write recipes once using automatic variables: $@ the target, $< the first prerequisite, $^ all prerequisites, $? the newer ones.

  2. Pattern rules and built-in rules · Practical · 12 min

    Write one rule for many files with % patterns like %.o: %.c, and know make's built-in implicit rules and how to turn them off.

  3. How recipes run in the shell · Practical · 12 min

    Each recipe line runs in its own shell; learn @, -, line continuations, $$, .ONESHELL, SHELL and .DELETE_ON_ERROR.

Real projects

  1. Automatic header dependencies · Advanced · 12 min

    Let the compiler generate dependency files with -MMD -MP and include them, so editing a header rebuilds exactly the right objects.

  2. Parallel builds with -j · Advanced · 12 min

    Run independent recipes concurrently with make -j, fix the missing dependencies that parallelism exposes, and use order-only prerequisites.

  3. Conditionals, includes and configuration · Advanced · 12 min

    Adapt a Makefile with ifeq/ifdef, split it with include, and support build modes like DEBUG=1 and per-OS settings.

How make thinks

  1. Two phases: reading and running · Expert · 15 min

    Understand make's read phase and build phase, immediate versus deferred expansion, and how $(eval) and $(call) generate rules.

  2. Recursive versus non-recursive make · Expert · 12 min

    Why calling make in each subdirectory hides dependencies and slows builds, and how a single non-recursive Makefile with includes fixes it.

  3. Debugging Makefiles · Expert · 12 min

    Find out why make did or didn't rebuild something with -n, --trace, --debug, -p and $(info), and understand its common error messages.