Chapter 3: Writing Makefiles 19
$*, etc. during the second expansion and they will have their expected values, just as in
the recipe. All you have to do is defer the expansion by escaping the $. Also, secondary
expansion occurs for both explicit and implicit (pattern) rules. Knowing this, the possible
uses for this feature increase dramatically. For example:
.SECONDEXPANSION:
main_OBJS := main.o try.o test.o
lib_OBJS := lib.o api.o
main lib: $$($$@_OBJS)
Here, after the initial expansion the prerequisites of both the main and lib targets will
be $($@_OBJS). During the secondary expansion, the $@ variable is set to the name of the
target and so the expansion for the main target will yield $(main_OBJS), or main.o try.o
test.o, while the secondary expansion for the lib target will yield $(lib_OBJS), or lib.o
api.o.
You can also mix in functions here, as long as they are properly escaped:
main_SRCS := main.c try.c test.c
lib_SRCS := lib.c api.c
.SECONDEXPANSION:
main lib: $$(patsubst %.c,%.o,$$($$@_SRCS))
This version allows users to specify source files rather than object files, but gives the
same resulting prerequisites list as the previous example.
Evaluation of automatic variables during the secondary expansion phase, especially of
the target name variable $$@, behaves similarly to evaluation within recipes. However, there
are some subtle differences and “corner cases” which come into play for the different types
of rule definitions that make understands. The subtleties of using the different automatic
variables are described below.
Secondary Expansion of Explicit Rules
During the secondary expansion of explicit rules, $$@ and $$% evaluate, respectively, to the
file name of the target and, when the target is an archive member, the target member name.
The $$< variable evaluates to the first prerequisite in the first rule for this target. $$^ and
$$+ evaluate to the list of all prerequisites of rules that have already appeared for the same
target ($$+ with repetitions and $$^ without). The following example will help illustrate
these behaviors:
.SECONDEXPANSION:
foo: foo.1 bar.1 $$< $$^ $$+ # line #1
foo: foo.2 bar.2 $$< $$^ $$+ # line #2
foo: foo.3 bar.3 $$< $$^ $$+ # line #3
In the first prerequisite list, all three variables ($$<, $$^, and $$+) expand to the empty
string. In the second, they will have values foo.1, foo.1 bar.1, and foo.1 bar.1 respec-
Comentários a estes Manuais