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

Time:10-19

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.
  • It says in an #if directive. Is it because the LINUX is not inside that structure?

  • If that's the case, how can I tell compiler to emit warnings for the undefined symbol LINUX?


  • clang version 12.0.1
  • gcc (GCC) 11.1.0
  • Target: x86_64-pc-linux-gnu (artixlinux)

CodePudding user response:

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.

  • Related