174
KDIR ?= $(shell uname -r)

What is the meaning of ?=?

I have understood the difference between :=, += and = from another thread available in Stack Overflow, but unable to find the explanation for ?=.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
codedoc
  • 2,079
  • 2
  • 11
  • 15

2 Answers2

240

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

For example:

KDIR ?= "foo"
KDIR ?= "bar"

test:
    echo $(KDIR)

Would print "foo"

GNU manual: http://www.gnu.org/software/make/manual/html_node/Setting.html

rado
  • 4,040
  • 3
  • 32
  • 26
Simon
  • 2,721
  • 1
  • 13
  • 15
  • 1
    Does it mean that KDIR is already declared somewhere in the system? – codedoc Jul 16 '14 at 09:49
  • 2
    It doesn't have to be, since `?=` can be used to apply a default/fallback value to a variable, it may be allowing `KDIR` to be set in the environment. – Simon Jul 16 '14 at 09:59
  • 2
    @Simon Note that command line arguments to make already override Makefile variables without the need of `?=`. – CMCDragonkai Aug 22 '18 at 04:52
15

Thanks to Simon and R.T. for their quick and correct response.

Also, I have found the GNU manual that explains everything in detail: http://www.gnu.org/software/make/manual/html_node/Setting.html

codedoc
  • 2,079
  • 2
  • 11
  • 15