GNU Make 4 cheatsheet
GNU Make 4: Rules and targets
8 common GNU Make 4 tasks.
Basic rule
A target, its prerequisites and a recipe. Recipe lines must begin with a tab, not spaces.
app: main.o util.o
cc -o app main.o util.o
Declare phony targets
Mark targets that are commands, not files, so a file with the same name doesn't stop them running.
.PHONY: all clean test
Write a clean target
Remove build outputs. The leading minus ignores errors, such as files that don't exist.
clean:
-rm -f app *.o
Compile every source file
Build any .o file from the matching .c file. $< is the first prerequisite and $@ is the target.
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
Hide commands from the output
Start a recipe line with @ so make doesn't print it.
build:
@echo "Building..."
Run commands in one shell
Each recipe line runs in its own shell, so join commands with && or use a line continuation.
deploy:
cd dist && \
tar czf ../release.tgz .
Create a folder before building
An order-only prerequisite (after |) must exist but doesn't force a rebuild.
build/%.o: src/%.c | build
$(CC) -c $< -o $@
build:
mkdir -p build