Out-of-Tree Builds & VPATH
An in-tree build drops .o, .d and executables next to the sources. It works, but git status gets noisy, clean must hunt for files, and you can only have one configuration at a time. An out-of-tree build writes everything into a separate directory: build/, or one directory per configuration (build/debug, build/release). Deleting that directory is a perfect clean. CMake and Meson insist on it; with Make you set it up yourself.
Pattern rules with a build directory
The stem of a pattern rule can carry directories, so $(BUILD)/%.o: src/%.c maps every source to an object in a mirrored tree. The only extra work is creating the directories, here with mkdir -p $(@D) in the recipe:
mkdir -p proj/src/util && cd proj
printf 'const char *version(void);\n' > src/util/version.h
printf '#include "version.h"\nconst char *version(void) { return VERSION; }\n' > src/util/version.c
printf '#include <stdio.h>\n#include "util/version.h"\nint main(void) { printf("app %%s\\n", version()); return 0; }\n' > src/main.c
cat > Makefile <<'MK'
BUILD ?= build
CFLAGS ?= -O2
CPPFLAGS := -Isrc -DVERSION='"1.0"'
SRCS := src/main.c src/util/version.c
OBJS := $(patsubst src/%.c,$(BUILD)/%.o,$(SRCS))
$(BUILD)/app: $(OBJS)
gcc $^ -o $@
$(BUILD)/%.o: src/%.c
@mkdir -p $(@D)
gcc $(CPPFLAGS) $(CFLAGS) -MMD -MP -c lt; -o $@
-include $(OBJS:.o=.d)
.PHONY: clean
clean:
rm -rf $(BUILD)
MK
make -j1
./build/app
tree --noreport build
git init -q && git status --shortgcc -Isrc -DVERSION='"1.0"' -O2 -MMD -MP -c src/main.c -o build/main.o
gcc -Isrc -DVERSION='"1.0"' -O2 -MMD -MP -c src/util/version.c -o build/util/version.o
gcc build/main.o build/util/version.o -o build/app
app 1.0
build
├── app
├── main.d
├── main.o
└── util
├── version.d
└── version.o
?? Makefile
?? build/
?? src/
All build products live under build/; a one-line .gitignore (build/) keeps the repository clean.
One directory per configuration
Because BUILD and CFLAGS are variables, two configurations can live side by side. Each has its own objects, so switching between them never mixes -O0 objects with -O2 ones, and never forces a full rebuild:
mkdir -p proj/src && cd proj
printf '#include <stdio.h>\nint main(void) {\n#ifdef NDEBUG\n puts("release");\n#else\n puts("debug");\n#endif\n return 0;\n}\n' > src/main.c
cat > Makefile <<'MK'
CONFIG ?= debug
BUILD := build/$(CONFIG)
ifeq ($(CONFIG),release)
CFLAGS := -O2 -DNDEBUG
else
CFLAGS := -O0 -g
endif
$(BUILD)/app: $(BUILD)/main.o
gcc $^ -o $@
$(BUILD)/%.o: src/%.c
@mkdir -p $(@D)
gcc $(CFLAGS) -c lt; -o $@
MK
make -j1
make -j1 CONFIG=release
build/debug/app; build/release/app
make -j1
make -j1 CONFIG=releasegcc -O0 -g -c src/main.c -o build/debug/main.o
gcc build/debug/main.o -o build/debug/app
gcc -O2 -DNDEBUG -c src/main.c -o build/release/main.o
gcc build/release/main.o -o build/release/app
debug
release
make: 'build/debug/app' is up to date.
make: 'build/release/app' is up to date.
VPATH: finding sources somewhere else
The opposite approach keeps the Makefile's rules simple (%.o: %.c) and tells Make where to search for prerequisites it can't find in the current directory:
| Form | Meaning |
|---|---|
VPATH = src:lib |
search these directories (colon- or space-separated) for any prerequisite |
vpath %.c src |
search src only for files matching %.c |
vpath %.h include |
search include only for headers |
When Make finds a prerequisite through the search path, the automatic variables contain the found path, so recipes must use lt; and $^, never hard-coded names. This is how Autotools-style builds work: you run Make in the build directory and point it at the source tree:
mkdir -p tool/src tool/build && cd tool
printf 'int twice(int x) { return 2 * x; }\n' > src/twice.c
printf '#include <stdio.h>\nint twice(int);\nint main(void) { printf("%%d\\n", twice(21)); return 0; }\n' > src/main.c
cat > src/Makefile <<'MK'
SRCDIR := $(dir $(lastword $(MAKEFILE_LIST)))
vpath %.c $(SRCDIR)
tool: main.o twice.o
gcc $^ -o $@
%.o: %.c
gcc -c lt; -o $@
MK
cd build
make -j1 -f ../src/Makefile
./tool
ls
ls ../srcgcc -c ../src/main.c -o main.o
gcc -c ../src/twice.c -o twice.o
gcc main.o twice.o -o tool
42
main.o
tool
twice.o
Makefile
main.c
twice.c
$(MAKEFILE_LIST) holds the makefiles read so far, so $(dir $(lastword …)) is the directory of the current Makefile, which lets it find its sources wherever it's run from. The source tree stayed clean.
VPATH has a sharp edge: it searches for targets too. If an old main.o is lying in the source directory, Make may find it there, consider it up to date and link it. Keep source trees free of build products (one more reason to always build out of tree), and prefer the explicit $(BUILD)/%.o: src/%.c style for new projects.
Test yourself
Why are separate build directories per configuration better than a target-specific debug: CFLAGS += -g?
- They compile faster
- Each configuration has its own objects, so switching never mixes flags or forces a full rebuild
- Make requires it
- They avoid the TAB rule
With vpath %.c ../src, what is lt; when Make finds main.c in ../src?
main.c../src/main.csrc/main.c- empty
In $(BUILD)/%.o: src/%.c, what creates build/util/ before the compile?
- Make, automatically
- The compiler
- Something you add:
mkdir -p $(@D)in the recipe, or an order-only directory prerequisite vpath