MIbrah
MIbrah

Reputation: 1057

Scenarios for using ':=' vs '=' in makefile? (With examples if possible)

I started digging into details of makefile recently. I understand that ':=' is immediate and '=' is deferred. However, I need some simplified scenarios/examples of when I need to use one versus the other in order to fully appreciate the difference.

Upvotes: 2

Views: 416

Answers (1)

user657267
user657267

Reputation: 21000

If .foo and .bar files had exactly the same recipe you wouldn't want to have to write it out twice, recursively expanded variables to the rescue:

foobar = do_stuff $@
%.foo:
    $(foobar)
%.bar:
    $(foobar)

Unless you need recursive expansion, simply expanded variables should be used by default as they are easier to reason about and more robust.

some_deps = stuff I really want
some_other_deps = $(some_deps) some more

### [...]

some_deps = unrelated stuff

foo: $(some_other_deps) # Oops, should have used := for some_other_deps

You can find more examples in the manual.

Upvotes: 2

Related Questions