EMZETT.
Login

Makefile basics

In short: A Makefile describes which files are produced from which and with which commands. make then rebuilds only what is out of date.

In more detail: The tool dates from the 1970s but is still widespread, also as a simple task runner for projects in any language.

In Depth

  • Rule: target: dependencies and below it, indented with a tab, the commands.
program: main.o util.o
	cc -o program main.o util.o
 
%.o: %.c
	cc -c $< -o $@
  • Variables: $@ (target), $< (first dependency), $^ (all). Your own, like CC=gcc.
  • Up to date: make compares modification times. Only targets with newer dependencies are built.
  • .PHONY: Targets without a file, such as test or clean, are declared this way so a file of the same name does not block them.
  • Parallel: make -j8 builds independent targets at the same time.
  • Pitfalls: Spaces instead of a tab, each command line runs in its own shell.
  • Alternatives: CMake, Ninja, Just, Task. See dependencies and libraries.

See also: reproducible builds