Lesson 4 of 5
Makefile - Variables, Pattern Rules & Phony Targets
A `Makefile` automates building a C project - it tracks which files changed and recompiles only what's needed. Key concepts: **targets** (what to build), **prerequisites** (what it depends on), **recipes** (how to build), **variables** (reusable settings), **pattern rules** (`%.o: %.c`), **automatic variables** (`$@`, `$<`, `$^`), and `.PHONY` targets (actions that aren't files).
Professional Makefile - Full Annotated Template
# Makefile - professional multi-file C project template
# ── Compiler settings ─────────────────────────────────────────
CC = gcc
CFLAGS = -Wall -Wextra -Wpedantic -std=c99
LDFLAGS =
LIBS = -lm
# ── Debug vs Release ─────────────────────────────────────────
# make DEBUG=1 → debug build
# make → release build (default)
ifdef DEBUG
CFLAGS += -g -O0 -fsanitize=address,undefined -fno-omit-frame-pointer
LDFLAGS += -fsanitize=address,undefined
TARGET = myapp_debug
else
CFLAGS += -O2 -DNDEBUG
TARGET = myapp
endif
# ── Source files (auto-discovered) ────────────────────────────
SRCS = main.c list.c arena.c db.c
# Or: SRCS = $(wildcard src/*.c)
# ── Object files (pattern substitution) ───────────────────────
OBJS = $(SRCS:.c=.o) # replaces .c with .o for each source
# ── Default target - first target in the file is the default ──
.PHONY: all clean run test
all: $(TARGET)
# ── Link - combine object files into executable ───────────────
$(TARGET): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
# $@ = target name ($TARGET here)
# $^ = all prerequisites ($OBJS here)
# ── Pattern rule - compile any .c to .o ───────────────────────
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
# $< = first prerequisite (the .c file)
# $@ = target (the .o file)
# ── Dependencies - header changes trigger recompile ───────────
# Manual: main.o: main.c list.h db.h
# Or auto-generate with:
# DEPS = $(OBJS:.o=.d)
# -include $(DEPS)
# %.d: %.c
# $(CC) -MM -MT $@ $< > $@
# And add to CFLAGS: -MMD -MP
# ── Phony targets - don't produce a file ──────────────────────
clean:
rm -f $(OBJS) $(TARGET) myapp myapp_debug
run: $(TARGET)
./$(TARGET)
test: $(TARGET)
valgrind --leak-check=full --error-exitcode=1 ./$(TARGET)
# ── Usage ─────────────────────────────────────────────────────
# make → build release (myapp)
# make DEBUG=1 → build debug (myapp_debug) with ASAN
# make run → build + run
# make test → build + valgrind
# make clean → remove build artifacts
# make -j4 → parallel build (4 jobs) for speedCommon Mistakes
- Using spaces instead of tabs for recipe lines - Make REQUIRES a tab character at the start of each recipe line. Spaces cause 'missing separator' errors. Configure your editor to not convert tabs to spaces in Makefiles.
- Not marking phony targets with `.PHONY` - if a file named `clean` exists on disk, `make clean` does nothing (the target is already 'up to date'). Always `.PHONY: all clean test run`.
- Missing header dependencies - if `main.c` includes `list.h` and `list.h` changes, Make won't recompile `main.o` unless the dependency is listed. Use `-MMD -MP` to auto-generate `.d` dependency files.
Tip
Tip
Practice Makefile Variables Pattern Rules Phony Targets in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Make uses timestamps to recompile only modified source files and their dependents
Practice Task
Note
Practice Task - (1) Write a working example of Makefile Variables Pattern Rules Phony Targets from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Makefile Variables Pattern Rules Phony Targets is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- A `Makefile` automates building a C project - it tracks which files changed and recompiles only what's needed.
- Using spaces instead of tabs for recipe lines - Make REQUIRES a tab character at the start of each recipe line. Spaces cause 'missing separator' errors. Configure your editor to not convert tabs to spaces in Makefiles.
- Not marking phony targets with `.PHONY` - if a file named `clean` exists on disk, `make clean` does nothing (the target is already 'up to date'). Always `.PHONY: all clean test run`.
- Missing header dependencies - if `main.c` includes `list.h` and `list.h` changes, Make won't recompile `main.o` unless the dependency is listed. Use `-MMD -MP` to auto-generate `.d` dependency files.
Finished this lesson?