GNU Make Gotcha: ifndef and ?=

[article]
Summary:

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

and

FOO ?= New Value

They look like they should do the same thing, and they do... almost.
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

But this one does not:

FOO = Old Value
FOO ?= New Value

And neither does this one (even though FOO was initially empty):

FOO =
FOO ?= New Value

In fact ?= is exactly the same as the following which uses the GNU Make $(origin) function to determine if a variable is undefined. $(origin FOO) will return a string that shows whether and how FOO is defined (see the GNU Make manual for more information). If FOO is undefined then $(origin FOO) is undefined.

ifeq ($(origin FOO),undefined)
FOO = New Value
endif

Note that variables defined with ?= are expanded just like variables defined with =. They are expanded when used and not when defined just like a normal GNU Make variable.
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

will set FOO to the New Value if FOO is undefined or FOO is empty. So ifndef can be rewritten

ifeq ($(FOO),)
FOO = New Value
endif

Since an undefined variable is always treated as having an empty value when read.

 

About the author

TechWell Contributor's picture TechWell Contributor

The opinions and positions expressed within these guest posts are those of the author alone and do not represent those of the TechWell Community Sites. Guest authors represent that they have the right to distribute this content and that such content is not violating the legal rights of others. If you would like to contribute content to a TechWell Community Site, email editors@techwell.com.

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!

Upcoming Events

May 04
May 04
May 04
Jun 01