1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<h1 class="section">Appending More Text to Variables</h1> <p>Often it is useful to add more text to the value of a variable already defined. You do this with a line containing ‘<samp>+=</samp>’, like this: </p> <div class="example"> <pre class="example">objects += another.o
</pre>
</div> <p>This takes the value of the variable <code>objects</code>, and adds the text ‘<samp>another.o</samp>’ to it (preceded by a single space, if it has a value already). Thus: </p> <div class="example"> <pre class="example">objects = main.o foo.o bar.o utils.o
objects += another.o
</pre>
</div> <p>sets <code>objects</code> to ‘<samp>main.o foo.o bar.o utils.o another.o</samp>’. </p> <p>Using ‘<samp>+=</samp>’ is similar to: </p> <div class="example"> <pre class="example">objects = main.o foo.o bar.o utils.o
objects := $(objects) another.o
</pre>
</div> <p>but differs in ways that become important when you use more complex values. </p> <p>When the variable in question has not been defined before, ‘<samp>+=</samp>’ acts just like normal ‘<samp>=</samp>’: it defines a recursively-expanded variable. However, when there <em>is</em> a previous definition, exactly what ‘<samp>+=</samp>’ does depends on what flavor of variable you defined originally. See <a href="flavors">The Two Flavors of Variables</a>, for an explanation of the two flavors of variables. </p> <p>When you add to a variable’s value with ‘<samp>+=</samp>’, <code>make</code> acts essentially as if you had included the extra text in the initial definition of the variable. If you defined it first with ‘<samp>:=</samp>’ or ‘<samp>::=</samp>’, making it a simply-expanded variable, ‘<samp>+=</samp>’ adds to that simply-expanded definition, and expands the new text before appending it to the old value just as ‘<samp>:=</samp>’ does (see <a href="setting">Setting Variables</a>, for a full explanation of ‘<samp>:=</samp>’ or ‘<samp>::=</samp>’). In fact, </p> <div class="example"> <pre class="example">variable := value
variable += more
</pre>
</div> <p>is exactly equivalent to: </p> <div class="example"> <pre class="example">variable := value
variable := $(variable) more
</pre>
</div> <p>On the other hand, when you use ‘<samp>+=</samp>’ with a variable that you defined first to be recursively-expanded using plain ‘<samp>=</samp>’ or ‘<samp>:::=</samp>’, <code>make</code> appends the un-expanded text to the existing value, whatever it is. This means that </p> <div class="example"> <pre class="example">variable = value
variable += more
</pre>
</div> <p>is roughly equivalent to: </p> <div class="example"> <pre class="example">temp = value
variable = $(temp) more
</pre>
</div> <p>except that of course it never defines a variable called <code>temp</code>. The importance of this comes when the variable’s old value contains variable references. Take this common example: </p> <div class="example"> <pre class="example">CFLAGS = $(includes) -O
…
CFLAGS += -pg # enable profiling
</pre>
</div> <p>The first line defines the <code>CFLAGS</code> variable with a reference to another variable, <code>includes</code>. (<code>CFLAGS</code> is used by the rules for C compilation; see <a href="catalogue-of-rules">Catalogue of Built-In Rules</a>.) Using ‘<samp>=</samp>’ for the definition makes <code>CFLAGS</code> a recursively-expanded variable, meaning ‘<samp>$(includes) <span class="nolinebreak">-O</span></samp>’ is <em>not</em> expanded when <code>make</code> processes the definition of <code>CFLAGS</code>. Thus, <code>includes</code> need not be defined yet for its value to take effect. It only has to be defined before any reference to <code>CFLAGS</code>. If we tried to append to the value of <code>CFLAGS</code> without using ‘<samp>+=</samp>’, we might do it like this: </p> <div class="example"> <pre class="example">CFLAGS := $(CFLAGS) -pg # enable profiling
</pre>
</div> <p>This is pretty close, but not quite what we want. Using ‘<samp>:=</samp>’ redefines <code>CFLAGS</code> as a simply-expanded variable; this means <code>make</code> expands the text ‘<samp>$(CFLAGS) <span class="nolinebreak">-pg</span></samp>’ before setting the variable. If <code>includes</code> is not yet defined, we get ‘<samp> <span class="nolinebreak">-O</span> <span class="nolinebreak">-pg</span></samp>’, and a later definition of <code>includes</code> will have no effect. Conversely, by using ‘<samp>+=</samp>’ we set <code>CFLAGS</code> to the <em>unexpanded</em> value ‘<samp>$(includes) <span class="nolinebreak">-O</span> <span class="nolinebreak">-pg</span></samp>’. Thus we preserve the reference to <code>includes</code>, so if that variable gets defined at any later point, a reference like ‘<samp>$(CFLAGS)</samp>’ still uses its value. </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/Appending.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Appending.html</a>
</p>
</div>
|