Makefile 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
Links
- Your Makefiles are wrong
- Makefile cheatsheet on Devhints
- Official manual
- Makefile for Scam
- Auto-Dependency Generation