Chapter 4: Writing Rules 37
objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
Here ‘$<’ is the automatic variable that holds the name of the prerequisite and ‘$@’ is
the automatic variable that holds the name of the target; see Section 10.5.3 [Automatic
Variables], page 120.
Each target specified must match the target pattern; a warning is issued for each target
that does not. If you have a list of files, only some of which will match the pattern, you can
use the filter function to remove non-matching file names (see Section 8.2 [Functions for
String Substitution and Analysis], page 84):
files = foo.elc bar.o lose.o
$(filter %.o,$(files)): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
$(filter %.elc,$(files)): %.elc: %.el
emacs -f batch-byte-compile $<
In this example the result of ‘$(filter %.o,$(files))’ is bar.o lose.o , and the first static
pattern rule causes each of these object files to be updated by compiling the corresponding
C source file. The result of ‘$(filter %.elc,$(files))’ is foo.elc, so that file is made
from foo.el.
Another example shows how to use $* in static pattern rules:
bigoutput littleoutput : %output : text.g
generate text.g -$* > $@
When the generate command is run, $* will expand to the stem, either ‘big’ or ‘little’.
4.11.2 Static Pattern Rules versus Implicit Rules
A static pattern rule has much in common with an implicit rule defined as a pattern rule
(see Section 10.5 [Defining and Redefining Pattern Rules], page 118). Both have a pattern
for the target and patterns for constructing the names of prerequisites. The difference is in
how make decides when the rule applies.
An implicit rule can apply to any target that matches its pattern, but it does apply
only when the target has no recipe otherwise specified, and only when the prerequisites can
be found. If more than one implicit rule appears applicable, only one applies; the choice
depends on the order of rules.
By contrast, a static pattern rule applies to the precise list of targets that you specify
in the rule. It cannot apply to any other target and it invariably does apply to each of the
targets specified. If two conflicting rules apply, and both have recipes, that’s an error.
The static pattern rule can be better than an implicit rule for these reasons:
• You may wish to override the usual implicit rule for a few files whose names cannot be
categorized syntactically but can be given in an explicit list.
Kommentare zu diesen Handbüchern