Chapter 6: How to Use Variables 65
ifdef do_sort
func := sort
else
func := strip
endif
bar := a d b g q c
foo := $($(func) $(bar))
attempts to give ‘foo’ the value of the variable ‘sort a d b g q c’ or ‘strip a d b g q c’,
rather than giving ‘a d b g q c’ as the argument to either the sort or the strip function.
This restriction could be removed in the future if that change is shown to be a good idea.
You can also use computed variable names in the left-hand side of a variable assignment,
or in a define directive, as in:
dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print =
lpr $($(dir)_sources)
endef
This example defines the variables ‘dir’, ‘foo_sources’, and ‘foo_print’.
Note that nested variable references are quite different from recursively expanded vari-
ables (see Section 6.2 [The Two Flavors of Variables], page 60), though both are used
together in complex ways when doing makefile programming.
6.4 How Variables Get Their Values
Variables can get values in several different ways:
• You can specify an overriding value when you run make. See Section 9.5 [Overriding
Variables], page 103.
• You can specify a value in the makefile, either with an assignment (see Section 6.5
[Setting Variables], page 65) or with a verbatim definition (see Section 6.8 [Defining
Multi-Line Variables], page 69).
• Variables in the environment become make variables. See Section 6.10 [Variables from
the Environment], page 70.
• Several automatic variables are given new values for each rule. Each of these has a
single conventional use. See Section 10.5.3 [Automatic Variables], page 120.
• Several variables have constant initial values. See Section 10.3 [Variables Used by
Implicit Rules], page 115.
6.5 Setting Variables
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
Comentários a estes Manuais