Building Multi-File Programs with Make in C
Beyond a few files, manual gcc lines don't scale — Make rebuilds only what changed, expressed as target: dependencies rules with tab-indented recipes. A minimal Makefile names the binary, lists objects, and links; pattern rules (%.o: %.c) compile each source with shared flags once.
Variables (CC, CFLAGS) centralize compiler and options (-Wall -Wextra -g -O2), .PHONY marks non-file targets like clean and test, and automatic variables ($@ target,
lt; first prerequisite) keep recipes generic. For bigger projects, CMake generates the Makefiles — but reading Make remains mandatory.Targets depend, recipes build
app: main.o util.o + tab gcc -o app main.o util.o — make rebuilds app only when its prerequisites are newer.
Pattern rules compile
%.o: %.c with $(CC) $(CFLAGS) -c
lt; -o $@ compiles every source uniformly — one rule, all files.Variables centralize flags
CC=gcc and CFLAGS=-Wall -Wextra -g -O2 in one place — quoted by every recipe, changed once.
.PHONY for non-files
.PHONY: clean test — marks targets that aren't files so same-named files never shadow them.
CC=gccCFLAGS=-Wall -Wextra -g -O2 app: main.o util.o $(CC) -o app main.o util.o %.o: %.c $(CC) $(CFLAGS) -c lt; -o $@ .PHONY: cleanclean: rm -f app *.oRecommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.