Are there C compilers that will warn against using undeclared definitions

Recently, I came across a situation where the following construction

#if BYTE_ORDER == LITTLE_ENDIAN do_something(); #endif 

compiles 'do_something ()' if neither BYTE_ORDER nor LITTLE_ENDIAN . Although this is not unreasonable behavior, I cannot find any option on gcc to warn me in this situation.

Without warning, you may encounter a rather alarming situation where someone can delete an obviously unused header and it will completely change the compilation result because it included a header that defined these two macros (and defined them differently).

+5
source share
1 answer

From man gcc :

 -Wundef Warn if an undefined identifier is evaluated in an #if directive. 

In this way:

 echo -e '#if BYTE_ORDER == LITTLE_ENDIAN\n#endif'|gcc -E - -Wundef 

Print

 # 1 "<stdin>" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "<stdin>" <stdin>:1:5: warning: "BYTE_ORDER" is not defined [-Wundef] <stdin>:1:19: warning: "LITTLE_ENDIAN" is not defined [-Wundef] 

And it gets even better with -Werror=undef . 😉

+5
source

All Articles