Make:AutomaticVariables
lastmod 2018-08-17created 2018-06-173 edits
Detailed notes on the automatic variables used in make.
Automatic Variables
-
$* - The target name without its extension.
- The stem with which an implicit rule matches (See section
How Patterns Match1). If the target is 'dir/a.foo.b' and the target pattern is 'a.%.b', then the stem is 'dir/foo'. The stem is useful for constructing names of related files. In a static pattern rule, the stem is the 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 sectionOld-Fashioned Suffix Rules2),$*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 make implementations. 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 dependencies that have changed. For example, suppose that an archive named 'lib' is supposed to contain copies of several object files. The following rule copies just the changed object files into the archive.
-
$@ - Replaced with the target.
- The file name of the target of the rule. If the target is an archive member, then
$@is the name of the archive file. In a pattern rule that has multiple targets (See sectionIntroduction to Pattern Rules3),$@is the name of whichever target caused the rule's commands to be run.
-
$% - The target member name, when the target is an archive member (See section
Using make to Update Archive Files4). 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.
-
$^ - Replaced with all dependencies.
- The names of all the dependencies, with spaces between them. For dependencies which are archive members, only the named member is used (See section
Using make to Update Archive Files5). A target has only one dependency on each other file it depends on, no matter how many times each file is listed as a dependency. So if you list a dependency more than once for a target, the value of$^contains just one copy of the name.
-
$< - Replaced with the single leftmost dependency among those listed.
- The name of the first dependency. If the target got its commands from an implicit rule, this will be the first dependency added by the implicit rule (See section
Using Implicit Rules6)
-
$? - Compares the modification dates of the target and dependencies and is replaced with only the dependencies modified more recently. Mainly used for creating and maintaining libraries.
- The names of all the dependencies that are newer than the target, with spaces between them. For dependencies which are archive members, only the named member is used (see section
Using make to Update Archive Files7).
-
$+ - This is like
$^, but dependencies listed more than once are duplicated in the order they were listed in the makefile. This is primarily useful in link commands where it is meaningful to repeat library file names in a particular order.
Table of automatic variable variants
-
$(@D) - The directory part of the file name of the target, with the trailing slash removed. If the value of
$@is 'dir/foo.o' then$(@D)is 'dir'. This value is '.' if$@does not contain a slash.
-
$(@F) - The file-within-directory part of the file name of the target. If the value of
$@is 'dir/foo.o' then$(@F)is 'foo.o'.$(@F)is equivalent to$(notdir $@).
-
$(*D),$(*F) - The directory part and the file-within-directory part of the stem. In this example, they would be 'dir' and 'foo'.
-
$(%D),$(%F) - The directory part and the file-within-directory part of the target archive member name. This makes sense only for archive member targets of the form 'archive(member)' and is useful only when member may contain a directory name. (See section
Archive Members as Targets08)
-
$(<D),$(<F) - The directory part and the file-within-directory part of the first dependency.
-
$(^D),$(^F) - Lists of the directory parts and the file-within-directory parts of all dependencies.
-
$(?D),$(?F) - Lists of the directory parts and the file-within-directory parts of all dependencies that are newer than the target.
See also
References
-
See the
viper.pe.kr/make-ko/make-ko_10.html#SEC98entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_10.html#SEC102entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_10.html#SEC95entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_11.html#SEC104entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_11.html#SEC104entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_10.html#SEC89entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_11.html#SEC104entry in Viper.pe.kr-make-ko.7z ↩ -
See the
viper.pe.kr/make-ko/make-ko_11.html#SEC105entry in Viper.pe.kr-make-ko.7z ↩