Why are two undefined macros considered equal in GCC without warning?

Given the following code, what do you think the result might be? And what do you expect from the result?

#include <stdio.h>

#if AAAA == BBBB
    #define STRING "foo"
#else
    #define STRING "bar"
#endif


int main() {
    printf("%s\n", STRING);
    return 0;
}

      

Build and Test:

~/test$ gcc -Wall test.c 
~/test$ ./a.out 
foo

      

I know this is technically correct since both AAAA and BBBB are "UNDEFINED" and the developer needs to make sure both macros are defined before using it, but I really expect gcc to give a warning when I specify -Wall . But this is not the case.

This is really a problem and it makes sense. If you are using macros like __BIG_ENDIAN and __BYTE_ORDER to decide what your program should be, this can be a very tricky problem.

For example, I found code like this in the SKIA library in AOSP:

#if __BYTE_ORDER == __BIG_ENDIAN
    #define SK_CPU_BENDIAN
    #undef  SK_CPU_LENDIAN
#else
    #define SK_CPU_LENDIAN
    #undef  SK_CPU_BENDIAN
#endif

      

If you try to migrate a piece of your code snippet from that library and fail to include the correct headers (in this case endian.h), you're screwed ...

+3


source to share





All Articles