Make & Ninja: Build Executors
Compiling one C file by hand is easy. Compiling forty files, linking them into two libraries and three programs, and doing it again after every edit is not. You want to type one command and have the computer rebuild only what changed, in the right order. That is the job of a build executor, and GNU Make and Ninja are the two you will meet in almost every C and C++ project.
What a build tool actually knows
Every build is a graph. Files are nodes; "this file is made from those files by running this command" is an edge. In Build Fundamentals you drew that graph by hand. A build tool keeps it in a file and answers two questions each time you run it:
- What is out of date? Make compares modification times: a target older than any of its inputs must be rebuilt. Ninja does the same and also remembers the exact command it used last time.
- In what order? Inputs before outputs (a topological order of the graph), and independent steps in parallel when you allow it.
Make and Ninja side by side
| GNU Make | Ninja | |
|---|---|---|
| First released | 1976 (Unix Make); GNU Make in the late 1980s | 2012 (written for Chromium) |
| Written by | humans | mostly generators: CMake, Meson, GN, gyp |
| Language | rich: variables, functions, conditionals, pattern rules | deliberately tiny: rules, build statements, variables |
| Rebuild check | timestamps | timestamps plus the command line that built each file |
| Parallel by default | no (-j N to enable) |
yes (CPU count + 2) |
| Where you'll see it | Linux kernel, GNU projects, small C projects, task runners | almost every CMake or Meson build |
The usual relationship is: you write a high-level description (CMakeLists.txt, meson.build), a generator turns it into a build.ninja or a Makefile, and Ninja or Make runs it. Learning both executors lets you read, debug and speed up whatever those generators produce. The Modern CMake course builds on this one.
A first look at each
Here is the same one-file program built by Make and then by Ninja. Each session in this course runs in a fresh, empty directory: it creates its files with heredocs, then runs the real tools (GNU Make 4.3, Ninja 1.13.2, GCC 13).
mkdir -p greet && cd greet
cat > greet.c <<'SRC'
#include <stdio.h>
int main(void) { puts("Hello from a build tool"); return 0; }
SRC
cat > Makefile <<'MK'
greet: greet.c
cc -Wall -o greet greet.c
MK
make -j1 # greet does not exist yet: build it
./greet
make -j1 # nothing changed: nothing to do
sleep 1 && touch greet.c
make -j1 # greet.c is newer than greet: rebuildcc -Wall -o greet greet.c
Hello from a build tool
make: 'greet' is up to date.
cc -Wall -o greet greet.c
Three runs, three different decisions, and each one came from comparing the time stamps of greet and greet.c. The Ninja version needs a named rule first, then a build statement that uses it:
mkdir -p greet && cd greet
cat > greet.c <<'SRC'
#include <stdio.h>
int main(void) { puts("Hello from a build tool"); return 0; }
SRC
cat > build.ninja <<'NJ'
rule cc
command = cc -Wall -o $out $in
description = CC $out
build greet: cc greet.c
NJ
ninja -j1
./greet
ninja -j1[1/1] CC greet
Hello from a build tool
ninja: no work to do.
Ninja prints a short progress line ([done/total] description) instead of the full command; ninja -v shows the commands.
How this course works
- Real sessions (
bash run): every Makefile andbuild.ninjain this course was run, and the output under it is what the tools printed. We always build with one job (-j1) so the output is the same on every run; parallel builds get their own lesson. - Runnable models (
js run): small JavaScript programs that re-implement the core ideas (a mini Make deciding what to rebuild, a Ninja-style scheduler running N jobs). You can edit and run them in the browser. - Toolkit and Field Notes lessons collect the tools and the hard-won practical rules, and the course ends with exercises and a mini project that builds one multi-directory C project with both a Makefile and a hand-written
build.ninja.
A tiny model to start with: the whole rebuild rule of Make in a few lines.
// Make's core question: is the target missing, or older than any prerequisite?
function needsRebuild(target, prereqs, mtime) {
if (!(target in mtime)) return `${target}: missing -> build`;
const newer = prereqs.filter((p) => mtime[p] > mtime[target]);
return newer.length ? `${target}: older than ${newer.join(', ')} -> rebuild` : `${target}: up to date`;
}
console.log(needsRebuild('greet', ['greet.c'], { 'greet.c': 100 }));
console.log(needsRebuild('greet', ['greet.c'], { 'greet.c': 100, greet: 105 }));
console.log(needsRebuild('greet', ['greet.c'], { 'greet.c': 110, greet: 105 }));greet: missing -> build
greet: up to date
greet: older than greet.c -> rebuild
Test yourself
How does GNU Make decide that a target must be rebuilt?
- It hashes every source file
- The target is missing or older than one of its prerequisites
- It always rebuilds everything
- It asks the compiler
Who usually writes a build.ninja file?
- Developers, by hand, for every project
- A generator such as CMake or Meson
- The compiler
- The linker
What does Ninja remember about each output that Make does not?
- Its size
- The command line that built it
- Its owner
- Its file permissions