Learn C
#include "advanced/makefiles.h"

Building Multi-File Programs with Make in C

#concept

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.

#how it works
1

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.

2

Pattern rules compile

%.o: %.c with $(CC) $(CFLAGS) -c

lt; -o $@ compiles every source uniformly — one rule, all files.

3

Variables centralize flags

CC=gcc and CFLAGS=-Wall -Wextra -g -O2 in one place — quoted by every recipe, changed once.

4

.PHONY for non-files

.PHONY: clean test — marks targets that aren't files so same-named files never shadow them.

#example
CC=gcc
CFLAGS=-Wall -Wextra -g -O2
 
app: main.o util.o
$(CC) -o app main.o util.o
 
%.o: %.c
$(CC) $(CFLAGS) -c
lt; -o $@
 
.PHONY: clean
clean:
rm -f app *.o
Output
(make builds app; touch util.c && make rebuilds only util.o + link)
1Variables fix the compiler and flags project-wide; the link rule joins objects into app.
2The pattern rule compiles each .c to .o with identical flags — recipes use tabs, never spaces.
3Touching one source rebuilds just its object plus the link — incremental builds working as designed.
#use cases
Multi-file projects with incremental rebuilds
Centralized warning and optimization flags per project
clean/test convenience targets via .PHONY
Reading any open-source C project's build before touching CMake

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.