C Reference ยท Concepts and Tools
Make and build systems for C
Learn how make builds C projects: targets, prerequisites, variables, pattern rules, header dependencies, and when to move on to CMake, Meson or Ninja.
make rebuilds files only when their inputs changed. You describe each file as a rule: a target, the prerequisites it depends on, and the recipe (shell commands) that produces it. When you run make, it compares file modification times and runs just the recipes that are out of date.
For C this fits naturally: every .o file depends on its .c file and the headers it includes, and the executable depends on all the .o files. Edit one source file and only that file is recompiled, then relinked. The Makefiles on this page are read-only because the online runner builds one file at a time and has no shell or make; the programs show the ideas that make is built on.
app: main.o util.o gcc main.o util.o -o app
Recipe lines must start with a tab character, not spaces.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
A complete Makefile
Read-only; save it as Makefile next to main.c, util.c and util.h.
CC := gcc CFLAGS := -std=c23 -Wall -Wextra -O2 -MMD -MP LDFLAGS := LDLIBS := -lm SRCS := main.c util.c OBJS := $(SRCS:.c=.o) DEPS := $(OBJS:.o=.d) app: $(OBJS) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ -include $(DEPS) .PHONY: clean clean: rm -f app $(OBJS) $(DEPS)
| Piece | Meaning |
|---|---|
CC, CFLAGS, LDFLAGS, LDLIBS |
Conventional variable names: compiler, compile flags, linker flags, libraries. Users can override them: make CFLAGS="-O0 -g". |
$(SRCS:.c=.o) |
Substitution: turns main.c util.c into main.o util.o. |
$@, $<, $^ |
Automatic variables: the target, the first prerequisite, all prerequisites. |
%.o: %.c |
A pattern rule: how to build any .o from the matching .c. |
-MMD -MP and -include $(DEPS) |
The compiler writes .d files listing the headers each object includes, so editing a header rebuilds what depends on it. |
.PHONY |
Names that are actions, not files, so make clean runs even if a file called clean exists. |
Usage notes
- Run it:
makebuilds the first target;make cleanruns another;make -j4builds four files in parallel;make -nonly prints the commands. - Forgotten header dependencies are the classic bug: change a struct in a header and some objects stay stale. The
-MMD -MPpattern above fixes it. - Timestamps, not contents. A file restored from a backup with an old date may not trigger a rebuild;
touch fileormake -Bforces it. - Portability. GNU make has features (like
:=,$(wildcard), pattern rules) that othermakeprograms lack. Windows users often use MSYS2 or WSL. - Larger projects usually move to a generator: CMake (the most common, produces Makefiles or Ninja files), Meson (fast, readable), Bazel, or Ninja directly for speed. They find libraries, handle platforms and build out of tree.
- Keep builds out of the source tree (
build/directory) and out of version control. - A unity build (including all
.cfiles from one) is sometimes faster but loses internal linkage separation.