%.o: %.c
cc -c $< -o $@Pattern rules replace many repetitive file-specific rules.
Pattern rules, wildcarding, implicit rules, static pattern rules, and generated dependency files in GNU make.
Turn file patterns into reusable build logic.
%.o: %.c
cc -c $< -o $@Pattern rules replace many repetitive file-specific rules.
build/%.o: src/%.c
mkdir -p $(dir $@)
cc -c $< -o $@Excellent for out-of-tree builds.
objects := main.o util.o api.o
$(objects): %.o: %.c
cc -c $< -o $@Static pattern rules are explicit about which targets participate.
SRC := $(wildcard src/*.c)
OBJ := $(patsubst src/%.c,build/%.o,$(SRC))`patsubst` is a common bridge between source and output file lists.
app: $(OBJ)
cc $(OBJ) -o $@Combine generated variables with simple final targets.
.SUFFIXES:Useful in modern makefiles that rely on explicit pattern rules only.
Target- and prerequisite-aware variables available inside recipes.
archive.tar.gz: dist
tar -czf $@ dist`$@` is the full target name.
%.o: %.c
cc -c $< -o $@Often used in single-input compilation rules.
app: main.o util.o
cc $^ -o $@Great for linker commands.
libfoo.a: foo.o bar.o
ar rcs $@ $?Useful for archive updates.
build/%.min.js: src/%.js
terser $< -o build/$*.min.js`$*` is mostly for pattern and suffix rules.
copy:
@echo dir=$(dir $@) file=$(notdir $@)Combine automatic vars with text functions.
Collect files dynamically and normalize lists.
SRC := $(wildcard src/**/*.ts)In GNU make, `wildcard` expands file names that exist at parse time.
UNIQ_SRC := $(sort $(SRC))`sort` removes duplicate words as a side effect.
TESTS := $(filter %_test.py,$(FILES))Useful for selecting subsets of a list.
SRC_NO_VENDOR := $(filter-out vendor/%,$(SRC))Common when excluding generated or vendored files.
PKG_TARGETS := $(foreach p,$(PACKAGES),test-$(p))A powerful building block for metaprogramming.
Auto-track header or import dependencies.
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.
build/%.o: src/%.c
cc -MMD -MP -MF build/$*.d -c $< -o $@Useful when object and source directories differ.
node_modules/.stamp: package-lock.json
npm ci
touch $@
build: node_modules/.stampSimple and effective for JavaScript projects.
-include $(DEPS)The leading dash suppresses missing-file errors on first run.
Patterns for static libraries and archive members.
libfoo.a: $(OBJ)
ar rcs $@ $^Common for C/C++ libraries.
libfoo.a: $(OBJ)
ar rcs $@ $?Efficient for large archives.