Wednesday, 10 July 2019

Makefile variable assign difference between ?= vs =.

Makefile diff between ?= vs =.


  • If you’d like a variable to be set to a value only if it’s not already set,
  • then you can use the shorthand operator ‘?=’ instead of ‘=’.
Note: Got the comment as "Can you make this ?= so that it can be overriden in the invocation of the makefile?", so it will be good to use?=

These below two settings of the variable ‘FOO’ are identical

FOO ?= bar

and

ifeq ($(origin FOO), undefined)
FOO = bar
endif 

Program:

 # ?= indicates to set the KDIR variable only if it's not set/doesn't have a value.
KDIR ?= "?=foo"
KDIR ?= "?=bar"

EDIR = "=foo"
EDIR = "=bar"

test:
        echo $(KDIR)
        echo
        echo $(EDIR)
 

Output:


~/velrajk/sample/makefile/make_?=$ make
echo "?=foo"
?=foo
echo

echo "=bar"
=bar
 

Reference:
     https://www.gnu.org/software/make/manual/html_node/Setting.html
     https://stackoverflow.com/questions/24777289/what-is-in-makefile


No comments:

Post a Comment