Makefile cheatsheet

Usage

make looks for the file named Makefile in the CWD.

Options

Target

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
all: target

target: dep1 dep2
    build cmds

dep1:
    build cmds

dep2:
    build cmds

clean:
    clean cmds

.PHONY clean all

Note: each build cmd runs in a separate shell!

Variable

Multi-lines

1
2
3
VAR := VERY VERY VERY \
       VERY VERY LONG \
       CMD

Common variables

Special variables

For example:

1
2
3
4
5
output.txt: input1.txt input2.txt
    cat $^ > $@
# $^ : input1.txt input2.txt
# $@ : output.txt
# $< : input1.txt

Other

Detect changes in .h dependencies

  1. Use gcc with:
  2. Use -include $(OBJ:.o=.d) in the Makefile to include all dependency files (.d)