home blog portfolio Ian Fisher

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:

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