To set a variable from the makefile, write a line starting with the variable name followed by ‘=’ or ‘:=’. Whatever follows the ‘=’ or ‘:=’ on the line becomes the value. For example,
objects = main.o foo.o bar.o utils.o
defines a variable named objects
. Whitespace around the variable
name and immediately after the ‘=’ is ignored.
Variables defined with ‘=’ are recursively expanded variables. Variables defined with ‘:=’ are simply expanded variables; these definitions can contain variable references which will be expanded before the definition is made. See The Two Flavors of Variables.
The variable name may contain function and variable references, which are expanded when the line is read to find the actual variable name to use.
There is no limit on the length of the value of a variable except the
amount of swapping space on the computer. When a variable definition is
long, it is a good idea to break it into several lines by inserting
backslash-newline at convenient places in the definition. This will not
affect the functioning of make
, but it will make the makefile easier
to read.
Most variable names are considered to have the empty string as a value if you have never set them. Several variables have built-in initial values that are not empty, but you can set them in the usual ways (see Variables Used by Implicit Rules). Several special variables are set automatically to a new value for each rule; these are called the automatic variables (see Automatic Variables).
If you'd like a variable to be set to a value only if it's not already
set, then you can use the shorthand operator ‘?=’ instead of
‘=’. These two settings of the variable ‘FOO’ are identical
(see The origin
Function):
FOO ?= bar
and
ifeq ($(origin FOO), undefined) FOO = bar endif