Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.

Rationale for fixing ifdef

Thiadmer Riemersma edited this page Jan 19, 2025 · 6 revisions

The "common sense" argument is that it avoids confusion. The name ifdef implies "if defined", it's illogical to give it the semantics of "if not empty" (a macro defined as empty, is not undefined).

The practical argument is that there already is a simple way to check whether a macro is non-empty, with ifneq. If ifdef checks for non-emptiness too, however, there is no straightforward way check whether a macro or variable is defined. (Note: you can do it with the origin function and compare the result with undefined.)

GNU Make's original semantics of ifdef is listed as a "gotcha" in many of the books and articles on Make, especially because ifdef is often used to test for macros that are set on the command line or stored as an environment variable. The "Unix Haters Handbook" rightfully makes fun of it. I acknowledge that GNU Make has kept the "if not empty" semantics for the sake of backward compatibility, but consider that only a small minority of makefiles require the empty == undefined equivalence, and those are easily adapted.

And as a final note, Make+ also offers ifset and ifclear to test whether a macro is non-empty or empty. These directives ignore white space, so if a macro contains only white space ifset sees it as not set. To test for emptiness without ignoring white space, use ifneq.

Clone this wiki locally