Build tools and Make
Typing gcc -std=c23 -Wall -Wextra -c stats.c && gcc … && gcc -o report … after every change gets old fast, and in a project with 200 files you only want to recompile what changed. Make solves both: you describe how each file is built once, and it rebuilds exactly what's out of date. It's been doing this since 1976, and it's still used to build the Linux kernel, Git, Redis and countless other projects.
A first Makefile
We'll use the stats project from the previous lesson. Create the files:
#ifndef STATS_H
#define STATS_H
#include <stddef.h>
double stats_mean(const double *v, size_t n);
#endif#include "stats.h"
double stats_mean(const double *v, size_t n) {
double s = 0;
for (size_t i = 0; i < n; i++) s += v[i];
return n ? s / n : 0;
}#include <stdio.h>
#include "stats.h"
int main(void) {
double ms[] = {120, 85, 310, 95};
printf("mean response: %.1f ms\n", stats_mean(ms, 4));
return 0;
}And a file called Makefile in the same folder:
CC = gcc
CFLAGS = -std=c23 -Wall -Wextra -O2
report: main.o stats.o
$(CC) -o report main.o stats.o
main.o: main.c stats.h
$(CC) $(CFLAGS) -c main.c
stats.o: stats.c stats.h
$(CC) $(CFLAGS) -c stats.c
clean:
rm -f report *.ocd app
make
./reportgcc -std=c23 -Wall -Wextra -O2 -c main.c gcc -std=c23 -Wall -Wextra -O2 -c stats.c gcc -o report main.o stats.o mean response: 152.5 ms
Each rule has three parts:
target: prerequisites
<TAB>recipe (the command that builds target)- target: the file to build (
report,main.o). - prerequisites: the files it's built from. If any of them is newer than the target, the recipe runs.
- recipe: shell commands, each line starting with a real Tab character, not spaces. (Spaces give the famous error
missing separator.)
make with no argument builds the first target in the file. make clean runs the clean rule.
Only what changed
cd app
make # nothing to do
touch stats.c # pretend we edited stats.c
make
touch stats.h # the header is used by both .c files
makemake: 'report' is up to date. --- touch stats.c: gcc -std=c23 -Wall -Wextra -O2 -c stats.c gcc -o report main.o stats.o --- touch stats.h: gcc -std=c23 -Wall -Wextra -O2 -c main.c gcc -std=c23 -Wall -Wextra -O2 -c stats.c gcc -o report main.o stats.o
Make compares file modification times. Changing stats.c rebuilt only stats.o and relinked; changing the header rebuilt both, because both rules list it. That's why the header must appear in the prerequisites, and why the dependency list matters.
Variables and automatic variables
A realistic Makefile avoids repeating itself:
CC = gcc
CFLAGS = -std=c23 -Wall -Wextra -O2
LDLIBS = -lm
SRCS = main.c stats.c
OBJS = $(SRCS:.c=.o)
TARGET = report
$(TARGET): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
%.o: %.c stats.h
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: clean run
clean:
rm -f $(TARGET) $(OBJS)
run: $(TARGET)
./$(TARGET)cd app
make clean
make runrm -f report main.o stats.o gcc -std=c23 -Wall -Wextra -O2 -c main.c -o main.o gcc -std=c23 -Wall -Wextra -O2 -c stats.c -o stats.o gcc -o report main.o stats.o -lm ./report mean response: 152.5 ms
| Syntax | Meaning |
|---|---|
VAR = value, $(VAR) |
Define and use a variable |
$(SRCS:.c=.o) |
Substitution: main.c stats.c → main.o stats.o |
%.o: %.c |
A pattern rule: how to make any .o from the .c with the same name |
$@ |
The target |
$< |
The first prerequisite |
$^ |
All prerequisites |
.PHONY: clean run |
These targets aren't files; always run them (even if a file called clean exists) |
Variables can be overridden from the command line, which is handy for debug builds:
cd app
make clean
make CFLAGS="-std=c23 -g -O0 -fsanitize=address" LDFLAGS=-fsanitize=address
./reportgcc -std=c23 -g -O0 -fsanitize=address -c main.c -o main.o gcc -std=c23 -g -O0 -fsanitize=address -c stats.c -o stats.o gcc -fsanitize=address -o report main.o stats.o -lm mean response: 152.5 ms
Automatic header dependencies
Listing every header by hand is error-prone: forget one and Make won't rebuild when it changes, giving you strange bugs from stale object files. GCC can write the dependency lists for you with -MMD -MP:
CC = gcc
CFLAGS = -std=c23 -Wall -Wextra -O2 -MMD -MP
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
DEPS = $(OBJS:.o=.d)
report: $(OBJS)
$(CC) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
-include $(DEPS)
.PHONY: clean
clean:
rm -f report $(OBJS) $(DEPS)cd app
make clean
make
cat main.dgcc -std=c23 -Wall -Wextra -O2 -MMD -MP -c main.c -o main.o gcc -std=c23 -Wall -Wextra -O2 -MMD -MP -c stats.c -o stats.o gcc -o report main.o stats.o main.o: main.c stats.h stats.h:
Each .d file is a little Makefile rule GCC generated, saying which headers each object depends on. -include reads them (the - means "don't complain if they don't exist yet"). $(wildcard *.c) picks up new source files automatically. This is a good template for small and medium C projects.
Parallel builds
make -j8 runs up to 8 recipes at once, and make -j$(nproc) uses every CPU core. Because Make knows the dependencies, it only runs independent steps in parallel. On big projects that's a huge speed-up.
Make isn't only for C
Make runs any commands, so many projects use a Makefile as a list of shortcuts, whatever the language:
.PHONY: test lint deploy
test:
pytest -q
lint:
ruff check .
deploy: test lint
./scripts/deploy.shmake deploy now runs tests and lint first, and stops if either fails.
Beyond Make: CMake and friends
For large or cross-platform projects, people usually generate build files instead of writing them:
| Tool | Notes |
|---|---|
| CMake | The most common for C and C++. You describe targets in CMakeLists.txt; it generates Makefiles, Ninja files or Visual Studio projects |
| Meson + Ninja | Fast and simple; used by GNOME, systemd and QEMU |
Autotools (./configure && make && make install) |
The classic for older Unix software |
A minimal CMakeLists.txt for the same project:
cmake_minimum_required(VERSION 3.20)
project(report C)
set(CMAKE_C_STANDARD 23)
add_executable(report main.c stats.c)
target_compile_options(report PRIVATE -Wall -Wextra)cmake -B build # configure (once)
cmake --build build # build, only what changedPractice
- Write a Makefile for a project with three
.cfiles and two headers, using a pattern rule and-MMD -MP. - Add a
debugtarget that builds with-g -O0 -fsanitize=address,undefinedinto a separate binary. - Break your Makefile by replacing a Tab with spaces and read the error.
- Time a build with
make -j1andmake -j$(nproc)on a bigger project (trygit clone https://github.com/redis/redis && cd redis && time make -j$(nproc)).
1.When does Make run a rule's recipe?
2.What causes "Makefile:5: *** missing separator. Stop."?
3.In a rule, what is $@?
4.Why use -MMD -MP when compiling?