Makefile Pattern Rules & Dependency Generation

Pattern rules, wildcarding, implicit rules, static pattern rules, and generated dependency files in GNU make.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Implicit and pattern rules

Turn file patterns into reusable build logic.

Basic pattern rule

Compile any `.c` file into a matching `.o` file.

makefileANYpattern-rulesimplicitc
makefile
%.o: %.c
  cc -c $< -o $@

Pattern rules replace many repetitive file-specific rules.

Pattern rule with directories

Build objects into a separate output directory.

makefileANYpattern-rulesdirectoriesbuild
makefile
build/%.o: src/%.c
  mkdir -p $(dir $@)
  cc -c $< -o $@

Excellent for out-of-tree builds.

Static pattern rule

Apply a pattern to a known target list.

makefileANYpattern-rulesstatic-pattern
makefile
objects := main.o util.o api.o

$(objects): %.o: %.c
  cc -c $< -o $@

Static pattern rules are explicit about which targets participate.

Match and transform filenames

Create object names from source names.

makefileANYpattern-rulespatsubstwildcard
makefile
SRC := $(wildcard src/*.c)
OBJ := $(patsubst src/%.c,build/%.o,$(SRC))

`patsubst` is a common bridge between source and output file lists.

Clear built-in suffix rules

Reduce surprises by disabling legacy suffix rules.

makefileANYsuffix-rulesbuiltins
makefile
.SUFFIXES:

Useful in modern makefiles that rely on explicit pattern rules only.

Automatic variables

Target- and prerequisite-aware variables available inside recipes.

$@ target name

Expand to the current target file.

makefileANYautomatic-variablestarget
makefile
archive.tar.gz: dist
  tar -czf $@ dist

`$@` is the full target name.

$< first prerequisite

Expand to the first prerequisite.

makefileANYautomatic-variablesprerequisite
makefile
%.o: %.c
  cc -c $< -o $@

Often used in single-input compilation rules.

$^ all prerequisites

Expand to all prerequisites without duplicates.

makefileANYautomatic-variablesprerequisites
makefile
app: main.o util.o
  cc $^ -o $@

Great for linker commands.

$? newer prerequisites

Expand to prerequisites newer than the target.

makefileANYautomatic-variablesarchives
makefile
libfoo.a: foo.o bar.o
  ar rcs $@ $?

Useful for archive updates.

$* stem in pattern rules

Use the matched stem from the target pattern.

makefileANYautomatic-variablespattern-rules
makefile
build/%.min.js: src/%.js
  terser $< -o build/$*.min.js

`$*` is mostly for pattern and suffix rules.

dir and notdir with automatic vars

Extract target directory or file name.

makefileANYautomatic-variablesfunctions
makefile
copy:
  @echo dir=$(dir $@) file=$(notdir $@)

Combine automatic vars with text functions.

Wildcards and file discovery

Collect files dynamically and normalize lists.

wildcard function

Expand glob patterns during parsing.

makefileANYwildcardfile-discovery
makefile
SRC := $(wildcard src/**/*.ts)

In GNU make, `wildcard` expands file names that exist at parse time.

sort for deduplication

Sort and deduplicate a list of words.

makefileANYfunctionssortdedupe
makefile
UNIQ_SRC := $(sort $(SRC))

`sort` removes duplicate words as a side effect.

filter files by pattern

Keep only values that match a pattern.

makefileANYfunctionsfilter
makefile
TESTS := $(filter %_test.py,$(FILES))

Useful for selecting subsets of a list.

filter-out values

Exclude matches from a list.

makefileANYfunctionsfilter-out
makefile
SRC_NO_VENDOR := $(filter-out vendor/%,$(SRC))

Common when excluding generated or vendored files.

foreach list expansion

Generate text for each item in a list.

makefileANYfunctionsforeach
makefile
PKG_TARGETS := $(foreach p,$(PACKAGES),test-$(p))

A powerful building block for metaprogramming.

Generated dependency files

Auto-track header or import dependencies.

Generate C header dependency files

Have the compiler emit `.d` files while building objects.

makefileANYdependenciescgenerated
makefile
CFLAGS += -MMD -MP

build/%.o: src/%.c
  cc $(CFLAGS) -c $< -o $@

-include $(OBJ:.o=.d)

A classic GNU make pattern for automatic header dependency tracking.

Separate dependency file path

Write depfiles beside objects using explicit flags.

makefileANYdependenciesdepfile
makefile
build/%.o: src/%.c
  cc -MMD -MP -MF build/$*.d -c $< -o $@

Useful when object and source directories differ.

Stamp-based dependency install

Rebuild only when lockfile changes.

makefileANYdependenciesnpmstamp
makefile
node_modules/.stamp: package-lock.json
  npm ci
  touch $@

build: node_modules/.stamp

Simple and effective for JavaScript projects.

Include generated files safely

Load dependency files only if they exist.

makefileANYincludedependenciesgenerated
makefile
-include $(DEPS)

The leading dash suppresses missing-file errors on first run.

Archives and libraries

Patterns for static libraries and archive members.

Build static library

Create an archive from object files.

makefileANYarchivelibraryar
makefile
libfoo.a: $(OBJ)
  ar rcs $@ $^

Common for C/C++ libraries.

Update archive with newer objects

Use `$?` to add only changed prerequisites.

makefileANYarchiveautomatic-variables
makefile
libfoo.a: $(OBJ)
  ar rcs $@ $?

Efficient for large archives.

Recommended next

No recommendations yet.