Variables & Their Flavours
Variables keep a Makefile short and configurable: the compiler, its flags and the list of objects are each written once. You reference a variable with $(NAME) (or ${NAME}). The part that surprises everyone is that GNU Make has several assignment operators, and they differ in when the right-hand side is evaluated.
The five assignment operators
| Operator | Name | Right-hand side evaluated… |
|---|---|---|
= |
recursive (lazy) | every time the variable is used |
:= (also ::=) |
simple (immediate) | once, at the point of assignment |
?= |
conditional | like =, but only if the variable is not defined yet |
+= |
append | keeps the flavour the variable already has (lazy if new) |
!= |
shell assignment | runs the right-hand side as a shell command, immediately, and stores its output |
The difference between = and := shows up when a variable refers to another variable that changes later. $(info …) prints while Make reads the file, which makes it ideal for experiments:
mkdir -p flavours && cd flavours
cat > Makefile <<'MK'
LEVEL = 1
LAZY = level $(LEVEL)
NOW := level $(LEVEL)
LEVEL = 2
$(info LAZY = $(LAZY))
$(info NOW = $(NOW))
CFLAGS ?= -O2
CFLAGS += -Wall
$(info CFLAGS = $(CFLAGS))
FILES != printf '%s ' a b c
$(info FILES = $(FILES))
$(info flavours: LAZY is $(flavor LAZY), NOW is $(flavor NOW))
all: ;
MK
make -j1LAZY = level 2
NOW = level 1
CFLAGS = -O2 -Wall
FILES = a b c
flavours: LAZY is recursive, NOW is simple
make: 'all' is up to date.
LAZY stores the text level $(LEVEL) and expands it when used, after LEVEL became 2. NOW was expanded immediately. $(flavor …) asks Make which kind a variable is.
Rules of thumb:
- Use
:=by default. It's predictable and cheap: a lazy variable that calls$(shell …)or$(wildcard …)runs that again on every use. - Use
=when you deliberately want late binding, for example a command template that mentions variables set later or per target. - A recursive variable can't refer to itself:
CFLAGS = $(CFLAGS) -gis an error (Recursive variable 'CFLAGS' references itself). Use+=.
Where values come from
A variable can be set in five places. When the same name is set in more than one, this is the priority, highest first:
| Source | Example |
|---|---|
override directive in the Makefile |
override CFLAGS += -fPIC |
| command line | make CFLAGS=-O0 |
| the Makefile | CFLAGS = -O2 |
| the environment | CFLAGS=-O1 make |
| Make's built-in defaults | CC = cc, RM = rm -f |
?= exists exactly for the environment case: it sets a default unless the user already provided a value. make -e makes the environment beat the Makefile, which is rarely what you want. $(origin NAME) tells you where a value came from:
mkdir -p origins && cd origins
cat > Makefile <<'MK'
CFLAGS = -O2
PREFIX ?= /usr/local
override WARN = -Wall
show:
@echo "CC = $(CC) ($(origin CC))"
@echo "CFLAGS = $(CFLAGS) ($(origin CFLAGS))"
@echo "PREFIX = $(PREFIX) ($(origin PREFIX))"
@echo "WARN = $(WARN) ($(origin WARN))"
MK
make -j1
echo "---"
PREFIX=/opt/tools make -j1 CFLAGS=-O0 WARN=-wCC = cc (default)
CFLAGS = -O2 (file)
PREFIX = /usr/local (file)
WARN = -Wall (override)
---
CC = cc (default)
CFLAGS = -O0 (command line)
PREFIX = /opt/tools (environment)
WARN = -Wall (override)
The command line beat the Makefile's CFLAGS = -O2, the environment filled in PREFIX because the Makefile used ?=, and override kept WARN even though the command line tried to change it.
Printing while Make reads: info, warning, error
| Function | Prints | Then |
|---|---|---|
$(info text) |
text on standard output |
continues |
$(warning text) |
Makefile:LINE: text on standard error |
continues |
$(error text) |
Makefile:LINE: *** text. Stop. |
stops with status 2 |
They expand to nothing, so they can go anywhere: at the top level, inside a recipe, or inside a conditional to validate input:
mkdir -p checks && cd checks
cat > Makefile <<'MK'
MODE ?= debug
$(info building in $(MODE) mode)
ifeq ($(filter $(MODE),debug release),)
$(error MODE must be debug or release, not '$(MODE)')
endif
ifeq ($(MODE),debug)
$(warning debug builds are slow)
endif
all: ;
MK
make -j1
echo "---"
make -j1 MODE=release
echo "---"
make -j1 MODE=fast
echo "exit status: $?"building in debug mode
Makefile:7: debug builds are slow
make: 'all' is up to date.
---
building in release mode
make: 'all' is up to date.
---
building in fast mode
Makefile:4: *** MODE must be debug or release, not 'fast'. Stop.
exit status: 2
Target-specific variables
A variable can have a different value while one target (and everything it builds) is being made:
debug: CFLAGS += -g -O0
debug: appmake debug builds app with the extra flags. It's a neat trick, but Make doesn't know the objects were built with other flags: a later plain make thinks they are up to date. Separate build directories per configuration (Out-of-Tree Builds) are the robust answer.
Test yourself
A = x, then B := $(A), then A = y. What is $(B)?
xy$(A)- empty
Which assignment sets a default that a value from the environment can replace?
CFLAGS := -O2CFLAGS ?= -O2override CFLAGS = -O2CFLAGS != -O2
What does $(warning ...) do?
- Stops the build
- Prints a message with the file and line number and continues
- Prints only under
make -d - Turns compiler warnings into errors