home blog portfolio Ian Fisher

Makefile cheatsheet

Magic variables:

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

See also