Makefile
A program (make
) and a file format for creating artifacts out of input files and build commands. Still the lowest-common-denominator way to build C and C++ programs – though I prefer Meson.
Cheatsheet
Magic variables:
$@
for target$<
for first prerequisite$^
for all prerequisites
NAME := VALUE
defines name once, NAME = VALUE
allows redefinition
Pattern matching:
%.o: %.c
...
Pattern substitution:
OBJS = $(patsubst %.c, $(OUTDIR)/%.o, $(SRCS))
Clean rule:
.PHONY: clean
clean:
...
Debugging (with make print-vars
):
print-vars:
@echo "MYVAR: $(MYVAR)"
Jacob David-Hansson's Makefile preamble:
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
Bibliography
- Makefile cheatsheet on Devhints
- Official manual
- Example Makefile from one of my personal projects
- "Auto-Dependency Generation"
- "Your Makefiles are wrong"