# (make.info)Flavors

Next: Advanced Prev: Reference Up: Using Variables
```
The Two Flavors of Variables
============================

There are two ways that a variable in GNU `make' can have a value;
we call them the two "flavors" of variables.  The two flavors are
distinguished in how they are defined and in what they do when expanded.

The first flavor of variable is a "recursively expanded" variable.
Variables of this sort are defined by lines using `=' (Note: Setting
Variables.) or by the `define' directive (Note: Defining
Variables Verbatim.).  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 "recursive expansion".

For example,

foo = \$(bar)
bar = \$(ugh)
ugh = Huh?

all:;echo \$(foo)

will echo `Huh?': `\$(foo)' expands to `\$(bar)' which expands to
`\$(ugh)' which finally expands to `Huh?'.

This flavor of variable is the only sort supported by other versions
of `make'.  It has its advantages and its disadvantages.  An advantage
(most would say) is that:

CFLAGS = \$(include_dirs) -O
include_dirs = -Ifoo -Ibar

will do what was intended: when `CFLAGS' is expanded in a command, it
will expand to `-Ifoo -Ibar -O'.  A major disadvantage is that you
cannot append something on the end of a variable, as in

CFLAGS = \$(CFLAGS) -O

because it will cause an infinite loop in the variable expansion.
(Actually `make' detects the infinite loop and reports an error.)

Another disadvantage is that any functions (Note: Functions for
Transforming Text.) referenced in the definition will be
executed every time the variable is expanded.  This makes `make' run
slower; worse, it causes the `wildcard' and `shell' functions to give
unpredictable results because you cannot easily control when they are
called, or even how many times.

To avoid all the problems and inconveniences of recursively expanded
variables, there is another flavor: simply expanded variables.

"Simply expanded variables" are defined by lines using `:=' (*note
Setting Variables: Setting.).  The value of a simply expanded variable
is scanned once and for all, expanding any references to other
variables and functions, when the variable is defined.  The actual
value of the simply expanded variable is the result of expanding the
text that you write.  It does not contain any references to other
variables; it contains their values *as of the time this variable was
defined*.  Therefore,

x := foo
y := \$(x) bar
x := later

is equivalent to

y := foo bar
x := later

When a simply expanded variable is referenced, its value is
substituted verbatim.

Here is a somewhat more complicated example, illustrating the use of
`:=' in conjunction with the `shell' function.  (Note: The `shell'
Function.)  This example also shows use of the variable
`MAKELEVEL', which is changed when it is passed down from level to
level.  (*Note Communicating Variables to a Sub-`make':
Variables/Recursion, for information about `MAKELEVEL'.)

ifeq (0,\${MAKELEVEL})
cur-dir   := \$(shell pwd)
whoami    := \$(shell whoami)
host-type := \$(shell arch)
MAKE := \${MAKE} host-type=\${host-type} whoami=\${whoami}
endif

An advantage of this use of `:=' is that a typical `descend into a
directory' command then looks like this:

\${subdirs}:
\${MAKE} cur-dir=\${cur-dir}/\$@ -C \$@ all

Simply expanded variables generally make complicated makefile
programming more predictable because they work like variables in most
programming languages.  They allow you to redefine a variable using its
own value (or its value processed in some way by one of the expansion
functions) and to use the expansion functions much more efficiently
(Note: Functions for Transforming Text.).

You can also use them to introduce controlled leading whitespace into
variable values.  Leading whitespace characters are discarded from your
input before substitution of variable references and function calls;
this means you can include leading spaces in a variable value by
protecting them with variable references, like this:

nullstring :=
space := \$(nullstring) # end of the line

Here the value of the variable `space' is precisely one space.  The
comment `# end of the line' is included here just for clarity.  Since
trailing space characters are *not* stripped from variable values, just
a space at the end of the line would have the same effect (but be
rather hard to read).  If you put whitespace at the end of a variable
value, it is a good idea to put a comment like that at the end of the
line to make your intent clear.  Conversely, if you do *not* want any
whitespace characters at the end of your variable value, you must
remember not to put a random comment on the end of the line after some
whitespace, such as this:

dir := /foo/bar    # directory to put the frobs in

Here the value of the variable `dir' is `/foo/bar    ' (with four
trailing spaces), which was probably not the intention.  (Imagine
something like `\$(dir)/file' with this definition!)

```

automatically generated by info2www
{"ft_lang_label":"__label__en","ft_lang_prob":0.8484716,"math_prob":0.85349995,"size":5053,"snap":"2019-13-2019-22","text_gpt3_token_len":1172,"char_repetition_ratio":0.14775203,"word_repetition_ratio":0.016049383,"special_character_ratio":0.23213932,"punctuation_ratio":0.11425462,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9646822,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-05-22T11:25:02Z\",\"WARC-Record-ID\":\"<urn:uuid:086d5a65-1e74-4164-be8a-4dff68c88f24>\",\"Content-Length\":\"6327\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:491c568b-4094-4294-a407-cd44c957822a>\",\"WARC-Concurrent-To\":\"<urn:uuid:a64a6855-e7ed-454a-9602-50b07a199ca4>\",\"WARC-IP-Address\":\"128.2.42.95\",\"WARC-Target-URI\":\"http://www-cgi.cs.cmu.edu/cgi-bin/info2www?(make.info)Flavors\",\"WARC-Payload-Digest\":\"sha1:WL7DG7Y2ZKAZSVQLOFK5GKLPPWH4LJ3J\",\"WARC-Block-Digest\":\"sha1:YWWC6JB3YNUMOYA4DATQR6WUD2JXURDS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-22/CC-MAIN-2019-22_segments_1558232256797.20_warc_CC-MAIN-20190522103253-20190522125253-00276.warc.gz\"}"}