[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Suppose you are writing a pattern rule to compile a `.c' file into a `.o' file: how do you write the `cc' command so that it operates on the right source file name? You cannot write the name in the command, because the name is different each time the implicit rule is applied.
What you do is use a special feature of make
, the automatic
variables. These variables have values computed afresh for each rule that
is executed, based on the target and prerequisites of the rule. In this
example, you would use `$@' for the object file name and `$<'
for the source file name.
Here is a table of automatic variables:
$@
$%
make
to Update Archive Files. For example, if the target is `foo.a(bar.o)' then
`$%' is `bar.o' and `$@' is `foo.a'. `$%' is
empty when the target is not an archive member.
$<
$?
make
to Update Archive Files).
$^
make
to Update Archive Files). A target has only one prerequisite on each other file
it depends on, no matter how many times each file is listed as a
prerequisite. So if you list a prerequisite more than once for a target,
the value of $^
contains just one copy of the name.
$+
$*
In a static pattern rule, the stem is part of the file name that matched the `%' in the target pattern.
In an explicit rule, there is no stem; so `$*' cannot be determined
in that way. Instead, if the target name ends with a recognized suffix
(see section Old-Fashioned Suffix Rules), `$*' is set to
the target name minus the suffix. For example, if the target name is
`foo.c', then `$*' is set to `foo', since `.c' is a
suffix. GNU make
does this bizarre thing only for compatibility
with other implementations of make
. You should generally avoid
using `$*' except in implicit rules or static pattern rules.
If the target name in an explicit rule does not end with a recognized suffix, `$*' is set to the empty string for that rule.
`$?' is useful even in explicit rules when you wish to operate on only the prerequisites that have changed. For example, suppose that an archive named `lib' is supposed to contain copies of several object files. This rule copies just the changed object files into the archive:
lib: foo.o bar.o lose.o win.o ar r lib $? |
Of the variables listed above, four have values that are single file
names, and three have values that are lists of file names. These seven
have variants that get just the file's directory name or just the file
name within the directory. The variant variables' names are formed by
appending `D' or `F', respectively. These variants are
semi-obsolete in GNU make
since the functions dir
and
notdir
can be used to get a similar effect (see section Functions for File Names). Note, however, that the
`D' variants all omit the trailing slash which always appears in
the output of the dir
function. Here is a table of the variants:
Note that we use a special stylistic convention when we talk about these
automatic variables; we write "the value of `$<'", rather than
"the variable <
" as we would write for ordinary variables
such as objects
and CFLAGS
. We think this convention
looks more natural in this special case. Please do not assume it has a
deep significance; `$<' refers to the variable named <
just
as `$(CFLAGS)' refers to the variable named CFLAGS
.
You could just as well use `$(<)' in place of `$<'.
GNU make
provides support for the SysV make
feature that
allows special variable references $$@
, $$(@D)
, and
$$(@F)
(note the required double-"$"!) to appear with the
prerequisites list (normal automatic variables are available
only within a command script). When appearing in a prerequisites
list, these variables are expanded to the name of the target, the
directory component of the target, and the file component of the
target, respectively.
Note that these variables are available only within explicit and
static pattern (see section Static Pattern Rules) rules;
they have no special significance within implicit (suffix or pattern)
rules. Also note that while SysV make
actually expands its
entire prerequisite list twice, GNU make
does not behave
this way: instead it simply expands these special variables without
re-expanding any other part of the prerequisites list.
This somewhat bizarre feature is included only to provide some
compatibility with SysV makefiles. In a native GNU make
file
there are other ways to accomplish the same results. This feature is
disabled if the special pseudo target .POSIX
is defined.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |