简体   繁体   中英

What is the difference between := and += in make file?

what is working difference in the below statements?

 LDDIRS := -L$(ORACLE_LIB)
 LDDIRS += -L$(ORACLE_LIB)
  • := (Simply Expanded Variable ) The value is scanned for once and for all expanding any
    references to other variables and functions, when variable is defined. eg x:=foo
    y:=$(x) bar
    x:=later
    so above is equivalent to
    y:=foo bar
    x:=later

  • += is used for appending more text to variables eg
    objects=main.o foo.o bar.o
    objects+=new.o
    which will set objects to 'main.o foo.o bar.o new.o'

  • = is for recursively expanded variable.The value is install verbatim; if it contains reference to other variables these variables are expanded whenever this variable is substituted.And this is known as recursive expansion.

"=" is for defining recursively expanded variable. The follow make file will print out "y is later bar"

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

all:;echo "y is" $(y)

":=" is for defining simply expanded variable, which is expanded once and for all. The following make file will print out "y is foo bar"

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

all:;echo "y is" $(y)

Also, as other people pointed earlier, you can get more details in Using Variables section of the GNU make manual.

Hope this helps :-)

:= Defines the variable here to be the left hand side, += adds the right hand side to the existing value of the variable. Compare := with = which evaluates the right hand side at the place of use (rather than in this particular line)

You can look at the manual here (Assuming that you are using GNU make)

GNU Make has three assignment operators, ":=" , "=", "?=" and one "+=" for appending 
to the varibles.  

- ":=" performs immediate evaluation of the right-hand side and stores an actual   
  string into the left-hand side. 
  eg: 
  x:=foo
  y:=$(x) bar 
  x:=later
  so above is equivalent to
  y:=foo bar 
  x:=later

  test above example 
  x := foo 
  y := $(x) bar 
  x := later

  all:;echo "y is" $(y)

  output
  ------
  y is foo bar 

- "=" is like a formula definition; it stores the right-hand side in an 
  unevaluated form and then evaluates this form each time the left-hand 
  side is used.

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

  all:;echo "y is" $(y)

  output
  ------
  y is  later foo 
  • "?=" Assign only if it's not set/doesn't have a value. eg: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR) Would print "foo"

    • "+=" is used for appending more text to variables. eg objects=main.o foo.o bar.o objects+=new.o

      which will set objects to 'main.o foo.o bar.o new.o'

From This website

for the syntax := Link to place on page

Simply expanded variables are defined by lines using ':=' (see Setting Variables). 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.

for the syntax += Link to place on page

When the variable in question has not been defined before, '+=' acts just like normal '=': it defines a recursively-expanded variable. However, when there is a previous definition, exactly what '+=' does depends on what flavor of variable you defined originally. See The Two Flavors of Variables, for an explanation of the two flavors of variables.

the := will set the value once to the variable, ie it wont be re-evaluated everytime make encouters that variable. Can make a huge difference in performance when compiling the code.

+= will simply add up a value to the variable.

The := is for assignation, in the same manner as = .

+= add a new value to the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM