{"id":699,"date":"2011-09-20T13:28:37","date_gmt":"2011-09-20T04:28:37","guid":{"rendered":"http:\/\/jungwonkim.com\/?p=699"},"modified":"2011-09-20T13:28:37","modified_gmt":"2011-09-20T04:28:37","slug":"make-automatic-variables","status":"publish","type":"post","link":"https:\/\/blog.jungwon.kim\/?p=699","title":{"rendered":"Make Automatic Variables"},"content":{"rendered":"<p><a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Automatic-Variables.html\">http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Automatic-Variables.html<\/a><\/p>\n<p><span class=\"Apple-style-span\" style=\"font-weight: bold;\">10.5.3 Automatic Variables<\/span><\/p>\n<p><a name=\"index-automatic-variables-933\"><\/a><a name=\"index-variables_002c-automatic-934\"><\/a><a name=\"index-variables_002c-and-implicit-rule-935\"><\/a>Suppose you are writing a pattern rule to compile a \u00e2\u20ac\u02dc<samp>.c<\/samp>\u00e2\u20ac\u2122 file into a \u00e2\u20ac\u02dc<samp>.o<\/samp>\u00e2\u20ac\u2122 file: how do you write the \u00e2\u20ac\u02dc<samp>cc<\/samp>\u00e2\u20ac\u2122 command so that it operates on the right source file name? You cannot write the name in the recipe, because the name is different each time the implicit rule is applied.<\/p>\n<p>What you do is use a special feature of <code>make<\/code>, the <dfn>automatic variables<\/dfn>. 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 \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 for the object file name and \u00e2\u20ac\u02dc<samp>$&lt;<\/samp>\u00e2\u20ac\u2122 for the source file name.<\/p>\n<p><a name=\"index-automatic-variables-in-prerequisites-936\"><\/a><a name=\"index-prerequisites_002c-and-automatic-variables-937\"><\/a>It&#8217;s very important that you recognize the limited scope in which automatic variable values are available: they only have values within the recipe. In particular, you cannot use them anywhere within the target list of a rule; they have no value there and will expand to the empty string. Also, they cannot be accessed directly within the prerequisite list of a rule. A common mistake is attempting to use <code>$@<\/code> within the prerequisites list; this will not work. However, there is a special feature of GNU <code>make<\/code>, secondary expansion (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Secondary-Expansion.html#Secondary-Expansion\">Secondary Expansion<\/a>), which will allow automatic variable values to be used in prerequisite lists.<\/p>\n<p>Here is a table of automatic variables: <a name=\"index-g_t_0024_0040_0040-938\"><\/a><a name=\"index-g_t_0040_0040-_0040r_007b_0028automatic-variable_0029_007d-939\"><\/a><\/p>\n<dl>\n<dt><code>$@<\/code><\/dt>\n<dd>The file name of the target of the rule. If the target is an archive member, then \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 is the name of the archive file. In a pattern rule that has multiple targets (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Pattern-Intro.html#Pattern-Intro\">Introduction to Pattern Rules<\/a>), \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 is the name of whichever target caused the rule&#8217;s recipe to be run.<a name=\"index-g_t_0024_0025-940\"><\/a><a name=\"index-g_t_0025-_0040r_007b_0028automatic-variable_0029_007d-941\"><\/a><\/p>\n<\/dd>\n<dt><code>$%<\/code><\/dt>\n<dd>The target member name, when the target is an archive member. See <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Archives.html#Archives\">Archives<\/a>. For example, if the target is<samp>foo.a(bar.o)<\/samp>then \u00e2\u20ac\u02dc<samp>$%<\/samp>\u00e2\u20ac\u2122 is<samp>bar.o<\/samp>and \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 is<samp>foo.a<\/samp>. \u00e2\u20ac\u02dc<samp>$%<\/samp>\u00e2\u20ac\u2122 is empty when the target is not an archive member.<a name=\"index-g_t_0024_003c-942\"><\/a><a name=\"index-g_t_003c-_0040r_007b_0028automatic-variable_0029_007d-943\"><\/a><\/p>\n<\/dd>\n<dt><code>$&lt;<\/code><\/dt>\n<dd>The name of the first prerequisite. If the target got its recipe from an implicit rule, this will be the first prerequisite added by the implicit rule (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Implicit-Rules.html#Implicit-Rules\">Implicit Rules<\/a>).<a name=\"index-g_t_0024_003f-944\"><\/a><a name=\"index-g_t_003f-_0040r_007b_0028automatic-variable_0029_007d-945\"><\/a><\/p>\n<\/dd>\n<dt><code>$?<\/code><\/dt>\n<dd>The names of all the prerequisites that are newer than the target, with spaces between them. For prerequisites which are archive members, only the named member is used (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Archives.html#Archives\">Archives<\/a>). <a name=\"index-prerequisites_002c-list-of-changed-946\"><\/a><a name=\"index-list-of-changed-prerequisites-947\"><\/a><a name=\"index-g_t_0024_005e-948\"><\/a><a name=\"index-g_t_005e-_0040r_007b_0028automatic-variable_0029_007d-949\"><\/a><\/dd>\n<dt><code>$^<\/code><\/dt>\n<dd>The names of all the prerequisites, with spaces between them. For prerequisites which are archive members, only the named member is used (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Archives.html#Archives\">Archives<\/a>). 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 <code>$^<\/code> contains just one copy of the name. This list does <strong>not<\/strong>contain any of the order-only prerequisites; for those see the \u00e2\u20ac\u02dc<samp>$|<\/samp>\u00e2\u20ac\u2122 variable, below. <a name=\"index-prerequisites_002c-list-of-all-950\"><\/a><a name=\"index-list-of-all-prerequisites-951\"><\/a><a name=\"index-g_t_0024_002b-952\"><\/a><a name=\"index-g_t_002b-_0040r_007b_0028automatic-variable_0029_007d-953\"><\/a><\/dd>\n<dt><code>$+<\/code><\/dt>\n<dd>This is like \u00e2\u20ac\u02dc<samp>$^<\/samp>\u00e2\u20ac\u2122, but prerequisites listed more than once are duplicated in the order they were listed in the makefile. This is primarily useful for use in linking commands where it is meaningful to repeat library file names in a particular order.<a name=\"index-g_t_0024_007c-954\"><\/a><a name=\"index-g_t_007c-_0040r_007b_0028automatic-variable_0029_007d-955\"><\/a><\/p>\n<\/dd>\n<dt><code>$|<\/code><\/dt>\n<dd>The names of all the order-only prerequisites, with spaces between them.<a name=\"index-g_t_0024_002a-956\"><\/a><a name=\"index-g_t_002a-_0040r_007b_0028automatic-variable_0029_007d-957\"><\/a><\/p>\n<\/dd>\n<dt><code>$*<\/code><\/dt>\n<dd>The stem with which an implicit rule matches (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Pattern-Match.html#Pattern-Match\">How Patterns Match<\/a>). If the target is<samp>dir\/a.foo.b<\/samp>and the target pattern is<samp>a.%.b<\/samp>then the stem is<samp>dir\/foo<\/samp>. The stem is useful for constructing names of related files. <a name=\"index-stem_002c-variable-for-958\"><\/a>In a static pattern rule, the stem is part of the file name that matched the \u00e2\u20ac\u02dc<samp>%<\/samp>\u00e2\u20ac\u2122 in the target pattern.In an explicit rule, there is no stem; so \u00e2\u20ac\u02dc<samp>$*<\/samp>\u00e2\u20ac\u2122 cannot be determined in that way. Instead, if the target name ends with a recognized suffix (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Suffix-Rules.html#Suffix-Rules\">Old-Fashioned Suffix Rules<\/a>), \u00e2\u20ac\u02dc<samp>$*<\/samp>\u00e2\u20ac\u2122 is set to the target name minus the suffix. For example, if the target name is \u00e2\u20ac\u02dc<samp>foo.c<\/samp>\u00e2\u20ac\u2122, then \u00e2\u20ac\u02dc<samp>$*<\/samp>\u00e2\u20ac\u2122 is set to \u00e2\u20ac\u02dc<samp>foo<\/samp>\u00e2\u20ac\u2122, since \u00e2\u20ac\u02dc<samp>.c<\/samp>\u00e2\u20ac\u2122 is a suffix. GNU <code>make<\/code> does this bizarre thing only for compatibility with other implementations of <code>make<\/code>. You should generally avoid using \u00e2\u20ac\u02dc<samp>$*<\/samp>\u00e2\u20ac\u2122 except in implicit rules or static pattern rules.<\/p>\n<p>If the target name in an explicit rule does not end with a recognized suffix, \u00e2\u20ac\u02dc<samp>$*<\/samp>\u00e2\u20ac\u2122 is set to the empty string for that rule.<\/p>\n<\/dd>\n<\/dl>\n<p>\u00e2\u20ac\u02dc<samp>$?<\/samp>\u00e2\u20ac\u2122 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<samp>lib<\/samp>is supposed to contain copies of several object files. This rule copies just the changed object files into the archive:<\/p>\n<pre>     lib: foo.o bar.o lose.o win.o\n             ar r lib $?<\/pre>\n<p>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&#8217;s directory name or just the file name within the directory. The variant variables&#8217; names are formed by appending \u00e2\u20ac\u02dc<samp>D<\/samp>\u00e2\u20ac\u2122 or \u00e2\u20ac\u02dc<samp>F<\/samp>\u00e2\u20ac\u2122, respectively. These variants are semi-obsolete in GNU <code>make<\/code> since the functions <code>dir<\/code> and <code>notdir<\/code> can be used to get a similar effect (see <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/File-Name-Functions.html#File-Name-Functions\">Functions for File Names<\/a>). Note, however, that the \u00e2\u20ac\u02dc<samp>D<\/samp>\u00e2\u20ac\u2122 variants all omit the trailing slash which always appears in the output of the <code>dir<\/code> function. Here is a table of the variants: <a name=\"index-g_t_0024_0028_0040_0040D_0029-959\"><\/a><a name=\"index-g_t_0040_0040D-_0040r_007b_0028automatic-variable_0029_007d-960\"><\/a><\/p>\n<dl>\n<dt>\u00e2\u20ac\u02dc<samp>$(@D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>The directory part of the file name of the target, with the trailing slash removed. If the value of \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 is<samp>dir\/foo.o<\/samp>then \u00e2\u20ac\u02dc<samp>$(@D)<\/samp>\u00e2\u20ac\u2122 is<samp>dir<\/samp>. This value is<samp>.<\/samp>if \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 does not contain a slash.<a name=\"index-g_t_0024_0028_0040_0040F_0029-961\"><\/a><a name=\"index-g_t_0040_0040F-_0040r_007b_0028automatic-variable_0029_007d-962\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(@F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>The file-within-directory part of the file name of the target. If the value of \u00e2\u20ac\u02dc<samp>$@<\/samp>\u00e2\u20ac\u2122 is<samp>dir\/foo.o<\/samp>then \u00e2\u20ac\u02dc<samp>$(@F)<\/samp>\u00e2\u20ac\u2122 is<samp>foo.o<\/samp>. \u00e2\u20ac\u02dc<samp>$(@F)<\/samp>\u00e2\u20ac\u2122 is equivalent to \u00e2\u20ac\u02dc<samp>$(notdir $@)<\/samp>\u00e2\u20ac\u2122.<a name=\"index-g_t_0024_0028_002aD_0029-963\"><\/a><a name=\"index-g_t_002aD-_0040r_007b_0028automatic-variable_0029_007d-964\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(*D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd><a name=\"index-g_t_0024_0028_002aF_0029-965\"><\/a><a name=\"index-g_t_002aF-_0040r_007b_0028automatic-variable_0029_007d-966\"><\/a><\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(*F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>The directory part and the file-within-directory part of the stem;<samp>dir<\/samp>and<samp>foo<\/samp>in this example.<a name=\"index-g_t_0024_0028_0025D_0029-967\"><\/a><a name=\"index-g_t_0025D-_0040r_007b_0028automatic-variable_0029_007d-968\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(%D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd><a name=\"index-g_t_0024_0028_0025F_0029-969\"><\/a><a name=\"index-g_t_0025F-_0040r_007b_0028automatic-variable_0029_007d-970\"><\/a><\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(%F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>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<samp><var>archive<\/var>(<var>member<\/var>)<\/samp>and is useful only when <var>member<\/var> may contain a directory name. (See <a href=\"http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Archive-Members.html#Archive-Members\">Archive Members as Targets<\/a>.)<a name=\"index-g_t_0024_0028_003cD_0029-971\"><\/a><a name=\"index-g_t_003cD-_0040r_007b_0028automatic-variable_0029_007d-972\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(&lt;D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd><a name=\"index-g_t_0024_0028_003cF_0029-973\"><\/a><a name=\"index-g_t_003cF-_0040r_007b_0028automatic-variable_0029_007d-974\"><\/a><\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(&lt;F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>The directory part and the file-within-directory part of the first prerequisite.<a name=\"index-g_t_0024_0028_005eD_0029-975\"><\/a><a name=\"index-g_t_005eD-_0040r_007b_0028automatic-variable_0029_007d-976\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(^D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd><a name=\"index-g_t_0024_0028_005eF_0029-977\"><\/a><a name=\"index-g_t_005eF-_0040r_007b_0028automatic-variable_0029_007d-978\"><\/a><\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(^F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>Lists of the directory parts and the file-within-directory parts of all prerequisites.<a name=\"index-g_t_0024_0028_002bD_0029-979\"><\/a><a name=\"index-g_t_002bD-_0040r_007b_0028automatic-variable_0029_007d-980\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(+D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd><a name=\"index-g_t_0024_0028_002bF_0029-981\"><\/a><a name=\"index-g_t_002bF-_0040r_007b_0028automatic-variable_0029_007d-982\"><\/a><\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(+F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>Lists of the directory parts and the file-within-directory parts of all prerequisites, including multiple instances of duplicated prerequisites.<a name=\"index-g_t_0024_0028_003fD_0029-983\"><\/a><a name=\"index-g_t_003fD-_0040r_007b_0028automatic-variable_0029_007d-984\"><\/a><\/p>\n<\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(?D)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd><a name=\"index-g_t_0024_0028_003fF_0029-985\"><\/a><a name=\"index-g_t_003fF-_0040r_007b_0028automatic-variable_0029_007d-986\"><\/a><\/dd>\n<dt>\u00e2\u20ac\u02dc<samp>$(?F)<\/samp>\u00e2\u20ac\u2122<\/dt>\n<dd>Lists of the directory parts and the file-within-directory parts of all prerequisites that are newer than the target.<\/dd>\n<\/dl>\n<p>Note that we use a special stylistic convention when we talk about these automatic variables; we write \u00e2\u20ac\u0153the value of \u00e2\u20ac\u02dc<samp>$&lt;<\/samp>\u00e2\u20ac\u2122\u00e2\u20ac\u009d, rather than \u00e2\u20ac\u0153the variable <code>&lt;<\/code>\u00e2\u20ac\u009d<!-- \/@w --> as we would write for ordinary variables such as <code>objects<\/code> and <code>CFLAGS<\/code>. We think this convention looks more natural in this special case. Please do not assume it has a deep significance; \u00e2\u20ac\u02dc<samp>$&lt;<\/samp>\u00e2\u20ac\u2122 refers to the variable named <code>&lt;<\/code>just as \u00e2\u20ac\u02dc<samp>$(CFLAGS)<\/samp>\u00e2\u20ac\u2122 refers to the variable named <code>CFLAGS<\/code>. You could just as well use \u00e2\u20ac\u02dc<samp>$(&lt;)<\/samp>\u00e2\u20ac\u2122 in place of \u00e2\u20ac\u02dc<samp>$&lt;<\/samp>\u00e2\u20ac\u2122.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.gnu.org\/s\/hello\/manual\/make\/Automatic-Variables.html 10.5.3 Automatic Variables Suppose you are writing a pattern rule to compile a \u00e2\u20ac\u02dc.c\u00e2\u20ac\u2122 file into a \u00e2\u20ac\u02dc.o\u00e2\u20ac\u2122 file: how do you write the \u00e2\u20ac\u02dccc\u00e2\u20ac\u2122 command so that it operates on the right source file name? You cannot write<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-699","post","type-post","status-publish","format-standard","hentry","category-cs"],"_links":{"self":[{"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=699"}],"version-history":[{"count":0,"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jungwon.kim\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}