Pick the Right Assignment in Make: =, :=, ?= and +=
Learn the difference between = and := in GNU Make, when ?= and += fit, and how a recursive variable can silently run a shell command many times.

GNU Make has more than one way to assign a variable, and they are not interchangeable. The operator you pick decides when the right-hand side is evaluated, whether a value from the command line or the environment survives, and whether a later line appends or replaces. Most Makefiles that behave strangely have a = where a := belonged, or the other way round. This post defines each assignment operator, shows the evaluation order with runnable examples, explains the performance trap of a recursive variable that calls the shell, and ends with a short rule for choosing. The semantics come from the "How to Use Variables" chapter of the GNU Make 4.4.1 manual.
Recursive and simply expanded variables
The two basic flavours differ in one thing: when the value is expanded.
VAR = textmakes a recursively expanded variable.makestores the text unexpanded and expands it each time the variable is used.VAR := textmakes a simply expanded variable.makeexpands the text once, at the moment of assignment, and stores the result.
The difference is invisible when the text has no references. It matters as soon as the text mentions another variable.
A = $(B)
B = first
AS := $(B)
B = second
show:
@echo "A is $(A)"
@echo "AS is $(AS)"make showA is second
AS is firstA was defined in terms of B, but was not evaluated until it was used, by which time B held second. AS captured the value of B at the line where it was assigned, which was first. A recursive variable behaves like a macro. A simply expanded one behaves like an ordinary assignment in other languages, and that is why most style guides recommend := as the default.
The performance trap
Recursive variables get expanded every time they are used. If the expansion runs a command, the command runs every time too.
FILES = $(shell find src -name '*.c')
OBJS = $(FILES:.c=.o)
%.o: %.c
$(CC) -c $< -o $@FILES is recursive. Every reference to OBJS expands FILES, which runs find again. A Makefile that mentions $(OBJS) in ten places spawns ten shell processes at start up, and a pattern rule that mentions it per target can spawn thousands. On a large tree this is the difference between make starting instantly and pausing for seconds. The fix is one character.
FILES := $(shell find src -name '*.c')
OBJS := $(FILES:.c=.o)Now find runs once, when the line is read, and the list is stored. Use := for any variable whose value comes from $(shell ...), $(wildcard ...) over a large tree, or any other costly function. There is a second reason: a recursive variable can refer to itself and produce an infinite loop. CFLAGS = $(CFLAGS) -O2 is an error, make reports "Recursive variable 'CFLAGS' references itself", and the simply expanded form CFLAGS := $(CFLAGS) -O2 is valid because the right side is expanded before the assignment completes.
Conditional assignment with ?=
VAR ?= text assigns only if the variable is not already defined. It is the correct operator for defaults that the user may override from the environment or the command line.
CC ?= gcc
PREFIX ?= /usr/local
show:
@echo "CC=$(CC) PREFIX=$(PREFIX)"make show
CC=clang make show
make show PREFIX=/opt/acmeCC=cc PREFIX=/usr/local
CC=clang PREFIX=/usr/local
CC=cc PREFIX=/opt/acmeThe first line prints cc, not gcc. That is a trap worth knowing: make defines CC itself with the default value cc before reading your Makefile, so ?= sees it as already defined and does nothing. To really override a built in default, use = or :=. For variables that make does not define, such as PREFIX, ?= works as expected, and ?= is the usual choice for installation prefixes.
?= creates a recursive variable, like =. Use := with a guard if you need the other flavour, for example ifeq ($(origin PREFIX),undefined) before a :=, though this is rarely needed.
Appending with +=
VAR += text appends with a space. It keeps the flavour of the variable: if VAR was simply expanded, the appended text is expanded now, and if it was recursive, the text is added unexpanded. If VAR was not defined, += creates it as recursive.
CFLAGS := -Wall
CFLAGS += -O2
CFLAGS += -g
show:
@echo "$(CFLAGS)"-Wall -O2 -gThis is the clean way to build a flag list up in steps, perhaps with conditions between the lines. Because an undefined variable becomes recursive, start the list with := when you can, so every later += stays simply expanded.
The shell assignment !=
GNU Make 4.0 added VAR != command, which runs the command and assigns its output. It is the same as VAR := $(shell command) except that the result is stored as a recursive variable, so its value is re-expanded on use, which matters only if the output contains dollar signs. In new code, prefer := $(shell ...), which is more widely supported and stores a simply expanded value.
Precedence: command line, environment, Makefile
When the same variable is defined in several places, there is an order. From highest to lowest:
A value given on the
makecommand line, such asmake CFLAGS=-O0.A variable set with
overridein the Makefile.A normal assignment in the Makefile.
An environment variable.
The built in defaults.
So a normal assignment in the Makefile beats the environment, which surprises people who set CFLAGS in their shell and see it ignored. Use ?= in the Makefile if the environment should win, or run make -e to make the environment override the Makefile. The command line beats everything, even the Makefile's own assignment, unless that assignment uses override.
override CFLAGS += -WallWith override, the line still appends to a command-line value, which is how a Makefile can insist on a flag no matter what the user passes.
Which one to use
| Situation | Operator |
|---|---|
| A default the user may override | ?= |
A constant, a list, or anything from $(shell) | := |
| Building a flag list in steps | := first, then += |
| A value that should track later changes to other variables | = |
| A flag the Makefile must keep despite the command line | override |
When in doubt, use :=. The cases where you want laziness are real, a template that is filled in later with define and $(call) for instance, but they are the exception.
Checking what you ended up with
When a variable misbehaves, ask make how it was defined, not what you meant. The $(origin) function reports where a value came from, and $(flavor) says whether it is recursive or simple.
CFLAGS := -O2
show:
@echo "origin: $(origin CFLAGS), flavor: $(flavor CFLAGS)"
@echo "origin: $(origin CC), flavor: $(flavor CC)"origin: file, flavor: simple
origin: default, flavor: recursiveThe first line says CFLAGS was set in the Makefile and expanded once. The second shows that CC is a built-in default, which is why ?= does nothing to it. With these two functions you can answer nearly every question of the form "why does my variable have that value" in one command, instead of guessing from the order of the lines in a long file. Make a habit of running them before editing the assignment operators in a Makefile you did not write.


