Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Make

More generally, see Build systems.

Snippets

Adding a suffix to a list of files

BINS := $(SRC:=.bin)

Depending on the Makefile

# All rules should depend on the Makefile itself
THIS = $(abspath $(lastword $(MAKEFILE_LIST)))

Listing (phony) targets

See above for $(THIS).

.PHONY: help
help:
 @printf 'Targets:\n'
 @grep '^\.PHONY: ' $(THIS) | cut -d ' ' -f2

Setting the shell

SHELL = bash
.SHELLFLAGS = -euo pipefail

Useful functions

  • addprefix
  • addsuffix
  • dir: Like Bash’s dirname
  • notdir: Like Bash’s basename
  • subst

Automatic variables

$@

Q. In GNU Make, what does the $@ variable denote?

A. The target.

$<

Q. In GNU Make, what does the $< variable denote?

A. The first prerequisite.

$?

Q. In GNU Make, what does the $? variable denote?

A. All prerequisites that are newer than the target.

$^

Q. In GNU Make, what does the $^ variable denote?

A. All the prerequisites.