TExcelTAD
Home › GNU Make & Ninja › Recursive vs Non-recursive Make

Recursive vs Non-recursive Make

Once a project has several directories (a library, an app, tests), there are two ways to organise its Makefiles:

  • Recursive make: every directory has its own complete Makefile, and the top-level Makefile runs $(MAKE) -C dir for each one.
  • Non-recursive make: there is one Make process. Each directory contributes a fragment (module.mk) that the top-level Makefile includes, so Make sees the whole dependency graph at once.

Recursive make is older and still very common (GNU projects via Automake, many embedded SDKs). Peter Miller's 1997 paper Recursive Make Considered Harmful explained its main weakness: each sub-make sees only its own piece of the graph.

Recursive make

bash · try it yourself
mkdir -p rec/lib rec/app && cd rec
printf 'int answer(void) { return 41; }\n' > lib/answer.c
printf '#include <stdio.h>\nint answer(void);\nint main(void) { printf("%%d\\n", answer()); return 0; }\n' > app/main.c
cat > lib/Makefile <<'MK'
libanswer.a: answer.o
	ar rcs $@ $^
MK
cat > app/Makefile <<'MK'
app: main.o ../lib/libanswer.a
	gcc $^ -o $@
MK
cat > Makefile <<'MK'
.PHONY: all lib app
all: app
lib:
	$(MAKE) -C lib
app: lib
	$(MAKE) -C app
MK
make -j1
app/app
make -C lib
make[1]: Entering directory '/home/learner/rec/lib'
cc    -c -o answer.o answer.c
ar rcs libanswer.a answer.o
make[1]: Leaving directory '/home/learner/rec/lib'
make -C app
make[1]: Entering directory '/home/learner/rec/app'
cc    -c -o main.o main.c
gcc main.o ../lib/libanswer.a -o app
make[1]: Leaving directory '/home/learner/rec/app'
41

Points to notice:

  • Always call sub-makes as $(MAKE), never plain make. $(MAKE) is the same Make binary that is running, and Make passes options such as -n, -k and the -j jobserver to it through the MAKEFLAGS variable. Lines that mention $(MAKE) even run under make -n, so a dry run descends into subdirectories.
  • make[1]: Entering directory … lines tell you which sub-make printed what; IDEs use them to resolve file names in error messages. --no-print-directory turns them off.
  • The ordering app: lib exists only in the top-level Makefile. app/Makefile lists ../lib/libanswer.a as a plain file prerequisite but has no idea how it's built.

Where it breaks

Work in the app directory, as people do, after changing the library:

bash · try it yourself
mkdir -p rec/lib rec/app && cd rec
printf 'int answer(void) { return 41; }\n' > lib/answer.c
printf '#include <stdio.h>\nint answer(void);\nint main(void) { printf("%%d\\n", answer()); return 0; }\n' > app/main.c
printf 'libanswer.a: answer.o\n\tar rcs $@ $^\n' > lib/Makefile
printf 'app: main.o ../lib/libanswer.a\n\tgcc $^ -o $@\n' > app/Makefile
printf '.PHONY: all lib app\nall: app\nlib:\n\t$(MAKE) -C lib\napp: lib\n\t$(MAKE) -C app\n' > Makefile
make -j1 --no-print-directory >/dev/null
echo "--- fix the bug in lib, rebuild from app/:"
sleep 1 && sed -i 's/41/42/' lib/answer.c
make -j1 -C app --no-print-directory
app/app
--- fix the bug in lib, rebuild from app/:
make: 'app' is up to date.
41

The sub-make for app/ sees ../lib/libanswer.a and main.o, both older than app, and says "up to date". It can't see lib/answer.c at all. The same blindness causes over-building (every sub-make re-reads and re-checks its directory on every build, even when nothing changed), and limits -j: Make can't start compiling app/ files while lib/ is still running unless the top-level ordering allows it, and if you remove the ordering to gain speed, app may link before lib is ready.

Non-recursive make

In the non-recursive style each directory has a fragment that only adds to the graph, using paths relative to the top directory. One Make process includes them all:

bash · try it yourself
mkdir -p flat/lib flat/app && cd flat
printf 'int answer(void) { return 41; }\n' > lib/answer.c
printf '#include <stdio.h>\nint answer(void);\nint main(void) { printf("%%d\\n", answer()); return 0; }\n' > app/main.c
cat > lib/module.mk <<'MK'
LIB := build/lib/libanswer.a
LIB_OBJS := build/lib/answer.o
$(LIB): $(LIB_OBJS)
	ar rcs $@ $^
MK
cat > app/module.mk <<'MK'
APP := build/app/app
APP_OBJS := build/app/main.o
$(APP): $(APP_OBJS) $(LIB)
	gcc $^ -o $@
MK
cat > Makefile <<'MK'
.PHONY: all clean
all: build/app/app

include lib/module.mk app/module.mk

build/%.o: %.c
	@mkdir -p $(@D)
	gcc -c 
lt; -o $@ clean: ; rm -rf build MK make -j1 build/app/app echo "--- fix the bug in lib, ask only for the app:" sleep 1 && sed -i 's/41/42/' lib/answer.c make -j1 build/app/app build/app/app
gcc -c app/main.c -o build/app/main.o
gcc -c lib/answer.c -o build/lib/answer.o
ar rcs build/lib/libanswer.a build/lib/answer.o
gcc build/app/main.o build/lib/libanswer.a -o build/app/app
41
--- fix the bug in lib, ask only for the app:
gcc -c lib/answer.c -o build/lib/answer.o
ar rcs build/lib/libanswer.a build/lib/answer.o
gcc build/app/main.o build/lib/libanswer.a -o build/app/app
42

One graph, so asking for the app correctly rebuilt the library it depends on, and nothing else. With -j Make could compile app/main.c and lib/answer.c at the same time, because it knows they are independent.

Choosing

Recursive Non-recursive
Correct incremental builds across directories no, unless you always build from the top yes
Parallelism limited by the sub-make boundaries the whole graph
No-op build speed re-reads every directory's Makefile one read, one pass
Independence of directories each directory builds alone fragments share one namespace: prefix variable names (LIB_OBJS, APP_OBJS)
Typical users Automake projects, the Linux kernel's Kbuild (recursion with a shared rule set), vendor SDKs, wrappers around other build systems Android's former make-based build, many large hand-written Makefiles

Recursion is still the right tool for running another project's build: $(MAKE) -C third_party/zlib, or a top-level Makefile that just calls cmake --build. For your own code, prefer one graph. The Mini Project builds a multi-directory project this way.

Test yourself

Why should a recursive call be written $(MAKE) -C dir rather than make -C dir?

  1. make is not on the PATH inside recipes
  2. $(MAKE) uses the same Make binary and passes flags like -n, -k and -j (jobserver) to the sub-make
  3. $(MAKE) runs faster
  4. There is no difference

What is the core problem with recursive make?

  1. It can't build libraries
  2. Each sub-make sees only part of the dependency graph, so cross-directory changes can be missed
  3. It doesn't support pattern rules
  4. It needs Ninja

In a non-recursive build, how does each directory contribute its rules?

  1. By running its own Make process
  2. Through a fragment (e.g. module.mk) that the top-level Makefile includes
  3. By symlinking its Makefile
  4. Through environment variables