1

I am confused about the difference between them.

I have read the GNU documentation, but I am still confused.

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.

The value of a simply expanded variable is scanned once, expanding any references to other variables and functions, when the variable is defined. Once that expansion is complete the value of the variable is never expanded again: when the variable is used the value is copied verbatim as the expansion. If the value contained variable references the result of the expansion will contain their values as of the time this variable was defined.

Can anyone clearly explain their differences?

Thanks a lot.

1

1 Answer 1

3

What the documentation tries to say is simply a difference between handling nested expansions at time of use (recursive assignments, =), vs. at time of assignment (simple assignment, :=).


As simple examples:

This

foo = abc
bar = $(foo)
foo = xyz

all:
    echo $(bar)

prints "xyz", as $(foo) is expanded when $(bar) is used.

But this:

foo := abc
bar := $(foo)
foo := xyz

all:
    echo $(bar)

prints "abc", as $(foo) is expanded when bar is set.


Similarly, this

xyzzy = $(shell date >&2)

foo:
    : $(xyzzy)
    : $(xyzzy)

runs date (and prints to stderr) twice, when $(xyzzy) is used.

While this:

xyzzy := $(shell date >&2)

foo:
    : $(xyzzy)
    : $(xyzzy)

runs date (and prints to stderr) once, when xyzzy is set. In this case, date runs even if the target foo isn't processed.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.