This month's article is about a little GNU Make Gotcha: an easy way to get tripped up by two pieces of GNU Make syntax that do similar things, yet one has a deceptive name. ifndef isn't really ifndef at all, it would better if it were called ifempty, while ?= does make its decision based on whether the variable is defined or not.
Compare these two ways of conditionally setting the variable FOO in a Makefile:
ifndef FOO
FOO = New Value
endif
FOO ?= New Value
What ?= doesThe ?= operator in GNU Make sets the variable mentioned on its left-hand side to the value on the right-hand side if the left-hand side is not defined. So, for example the following Makefile sets FOO to New Value:
FOO ?= New Value
FOO = Old Value
FOO ?= New Value
FOO =
FOO ?= New Value
ifeq ($(origin FOO),undefined)
FOO = New Value
endif
What ifndef doesifndef, on the other hand, tests whether a variable is empty or not. ifndef does not check to see if the variable is defined. ifndef actually means "if the variable is undefined or is defined but is empty". Thus
ifndef FOO
FOO = New Value
endif
FOO = New Value
endif
Since an undefined variable is always treated as having an empty value when read.





