Skip to main content
add some syntax highlighting
Source Link
muru
  • 78.1k
  • 16
  • 213
  • 319

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)
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)
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)
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)
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.

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.

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.

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

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.