1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<h1 class="subsection">Recursively Expanded Variable Assignment</h1> <p>The first flavor of variable is a <em>recursively expanded</em> variable. Variables of this sort are defined by lines using ‘<samp>=</samp>’ (see <a href="setting">Setting Variables</a>) or by the <code>define</code> directive (see <a href="multi_002dline">Defining Multi-Line Variables</a>). The value you specify is installed verbatim; if it contains references to other variables, these references are expanded whenever this variable is substituted (in the course of expanding some other string). When this happens, it is called <em>recursive expansion</em>. </p> <p>For example, </p> <div class="example"> <pre class="example">foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:;echo $(foo)
</pre>
</div> <p>will echo ‘<samp>Huh?</samp>’: ‘<samp>$(foo)</samp>’ expands to ‘<samp>$(bar)</samp>’ which expands to ‘<samp>$(ugh)</samp>’ which finally expands to ‘<samp>Huh?</samp>’. </p> <p>This flavor of variable is the only sort supported by most other versions of <code>make</code>. It has its advantages and its disadvantages. An advantage (most would say) is that: </p> <div class="example"> <pre class="example">CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
</pre>
</div> <p>will do what was intended: when ‘<samp>CFLAGS</samp>’ is expanded in a recipe, it will expand to ‘<samp>-Ifoo -Ibar -O</samp>’. A major disadvantage is that you cannot append something on the end of a variable, as in </p> <div class="example"> <pre class="example">CFLAGS = $(CFLAGS) -O
</pre>
</div> <p>because it will cause an infinite loop in the variable expansion. (Actually <code>make</code> detects the infinite loop and reports an error.) </p> <p>Another disadvantage is that any functions (see <a href="functions">Functions for Transforming Text</a>) referenced in the definition will be executed every time the variable is expanded. This makes <code>make</code> run slower; worse, it causes the <code>wildcard</code> and <code>shell</code> functions to give unpredictable results because you cannot easily control when they are called, or even how many times. </p><div class="_attribution">
<p class="_attribution-p">
Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br>
<a href="https://www.gnu.org/software/make/manual/html_node/Recursive-Assignment.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Recursive-Assignment.html</a>
</p>
</div>
|