62 GNU make
effect (but be rather hard to read). If you put whitespace at the end of a variable value,
it is a good idea to put a comment like that at the end of the line to make your intent
clear. Conversely, if you do not want any whitespace characters at the end of your variable
value, you must remember not to put a random comment on the end of the line after some
whitespace, such as this:
dir := /foo/bar # directory to put the frobs in
Here the value of the variable dir is ‘/foo/bar ’ (with four trailing spaces), which was
probably not the intention. (Imagine something like ‘$(dir)/file’ with this definition!)
There is another assignment operator for variables, ‘?=’. This is called a conditional
variable assignment operator, because it only has an effect if the variable is not yet defined.
This statement:
FOO ?= bar
is exactly equivalent to this (see Section 8.10 [The origin Function], page 94):
ifeq ($(origin FOO), undefined)
FOO = bar
endif
Note that a variable set to an empty value is still defined, so ‘?=’ will not set that
variable.
6.3 Advanced Features for Reference to Variables
This section describes some advanced features you can use to reference variables in more
flexible ways.
6.3.1 Substitution References
A substitution reference substitutes the value of a variable with alterations that you specify.
It has the form ‘$(var:a=b)’ (or ‘${var:a=b}’) and its meaning is to take the value of the
variable var, replace every a at the end of a word with b in that value, and substitute the
resulting string.
When we say “at the end of a word”, we mean that a must appear either followed by
whitespace or at the end of the value in order to be replaced; other occurrences of a in the
value are unaltered. For example:
foo := a.o b.o c.o
bar := $(foo:.o=.c)
sets ‘bar’ to ‘a.c b.c c.c’. See Section 6.5 [Setting Variables], page 65.
A substitution reference is actually an abbreviation for use of the patsubst expansion
function (see Section 8.2 [Functions for String Substitution and Analysis], page 84). We
provide substitution references as well as patsubst for compatibility with other implemen-
tations of make.
Another type of substitution reference lets you use the full power of the patsubst func-
tion. It has the same form ‘$(var:a=b)’ described above, except that now a must contain a
single ‘%’ character. This case is equivalent to ‘$(patsubst a,b,$(var))’. See Section 8.2
[Functions for String Substitution and Analysis], page 84, for a description of the patsubst
function.
Comentarios a estos manuales