Ali
Ali

Reputation: 420

-Wundef does not warn about an undefined symbol in front of #ifdef

Please consider the following code:

// program.cpp

#include <iostream>

int main() {
#ifdef LINUX
    std::cout << "Linux\n";
#elif MAC
    std::cout << "Mac\n";
#elif WINDOWS
    std::cout << "Windows\n";
#elif BSD
    std::cout << "BSD\n";
#else
    std::cout << "Something else\n";
#endif
    return 0;
}

If I compile it with both clang and gcc,

clang++ -Wundef -DBSD -o program program.cpp
# or
g++ -Wundef -DBSD -o program program.cpp

I will get warnings for not defining symbols MAC and WINDOWS, but no warning for the symbol LINUX:

program.cpp:6:7: warning: 'MAC' is not defined, evaluates to 0 [-Wundef]
#elif MAC
      ^
program.cpp:8:7: warning: 'WINDOWS' is not defined, evaluates to 0 [-Wundef]
#elif WINDOWS
      ^
2 warnings generated.

According to the gcc man page:

-Wundef 
    Warn if an undefined identifier is evaluated in an "#if" directive.
    Such identifiers are replaced with zero.

Upvotes: 0

Views: 178

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545943

The reason is that your preprocessor code asks if LINUX is defined. But for MAC, WINDOWS and BSD you don’t bother checking whether the symbol is defined; instead, your code assumes it is defined and asks for its value.

Change your code to use #elif defined(…) instead of #elif … to fix the warning.

Upvotes: 3

Related Questions