Opscraft
Makefile · Lesson 1 of 7 · 3 min read

How make thinks: targets, prerequisites, recipes

make was built in 1976 to recompile C programs, and it's still everywhere because it solves a timeless problem: a named list of tasks, with dependencies between them, that only redoes work that's out of date. DevOps teams use a Makefile as the single front door to a repository — make test, make docker-build, make deploy ENV=staging — so the same commands work on a laptop, in Jenkins, and in GitHub Actions. Interviewers ask about it to see whether you understand dependency graphs, can debug the classic gotchas, and can design a clean developer interface.

The rule

Enlarge diagramtargetwhat to build (file or task)prerequisitesmust be up to date firstrecipeshell commands, each line starts with TABapp: main.o util.o→ cc -o app main.o util.o
Figure 1. The parts of a make rule. Recipe lines must start with a real TAB.
Swipe sideways to see all of it, or tap to open it full screen.
target: prerequisite1 prerequisite2
    recipe line 1
    recipe line 2
  • Target — usually a file to build (app, build/output.tar.gz), or a task name (test, deploy).
  • Prerequisites — files or other targets that must be up to date first.
  • Recipe — shell commands that build the target. Every recipe line must start with a TAB character. Spaces produce the infamous Makefile:12: *** missing separator. Stop. (Editors that convert tabs to spaces cause this; configure .editorconfig with indent_style = tab for Makefiles. GNU make 3.82+ also lets you change the prefix with .RECIPEPREFIX, rarely used.)

The decision algorithm

When you run make app, make walks the dependency graph:

Enlarge diagramappmain.oskippedutil.orebuiltmain.cutil.hutil.ceditedrebuild when the target is missing or any prerequisite is newer
Figure 2. How make decides. Edit util.c and only util.o and app rebuild; main.o is skipped because nothing it depends on changed.
Swipe sideways to see all of it, or tap to open it full screen.
  1. For each prerequisite, recursively make sure it's up to date (building it if there's a rule for it).
  2. If the target file doesn't exist, or any prerequisite is newer (modification time) than the target, run the recipe.
  3. Otherwise print make: 'app' is up to date. and do nothing.
app: main.o util.o
    cc -o app main.o util.o

main.o: main.c util.h
    cc -c main.c

util.o: util.c util.h
    cc -c util.c

Edit util.c → only util.o and app rebuild. This incremental behaviour is make's superpower and the reason it beats a plain bash script for builds.

If a prerequisite doesn't exist and there's no rule to create it: make: *** No rule to make target 'config.yaml', needed by 'deploy'. Stop.

Running make

make                  # builds the DEFAULT goal: the first target in the file (unless .DEFAULT_GOAL is set)
make test             # a specific target
make lint test        # several, in order
make -f build.mk      # a different file (default search: GNUmakefile, makefile, Makefile)
make -C services/api  # run make in another directory
make -n deploy        # dry run: print commands without running them
make -j4              # run up to 4 independent recipes in parallel
make -k               # keep going after errors in independent targets
make -B               # unconditionally rebuild everything
make --debug=b        # explain why targets are (re)built
make -p               # print the database of rules and variables