[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: GCC compiler



Le dimanche 29 avril 2007 19:40, John L Fjellstad a écrit :
> "Michael Marsh" <michael.a.marsh@gmail.com> writes:
> > If I'm using GNU make, I hardly ever use "=" instead of ":=", unless I
> > really want to define a macro.  You're a lot more likely to get what
> > you expect most of the time, and you can use "+=".
>
> What's the difference?  My book on Make mentions "=", but not ":=". I
> knew about ":=" but I always thought they were pretty much the same.
>

The difference is that the value of a variable defined with := is expanded 
immediately, while with = it is only computed when used. Here's an example:

BAR = $(FOO)
FOO = some value

all:
	@echo "BAR = $(BAR)"

the BAR variable will here be expanded to "some value", but if you write 
BAR := $(FOO) instead, it will be expanded to an empty string because FOO is 
undefined at this time. That's why you can only use += with a variable 
defined with := , because it wouldn't make sense in the other case. That's 
also why (I guess) Michael says that you're more likely to get what you 
expect with := , because it's closer to what happens with other scripting 
languages. It's true that the = syntax can be quite tricky and is often the 
cause of hard to find bugs, but it's also often useful.

This is documented in the make info page, under section 6.5 "How to use 
variables/Setting". I don't know if this is specific to gnu make or not.

-- 
Cédric Lucantis



Reply to: