cmacros

Macros not working as I expect on Windows


I have the below code I want to print Use skeleton! if SKELETON_ENABLED is defined and is not running on MAC OS, and Don't use skeleton!\n otherwise.

The program prints Don't use skeleton!\n, even though SKELETON_ENABLED is defined and the code is running on a machine with Windows OS.

How can I fix the code to print Use skeleton!?

#include <stdio.h>

#define SKELETON_ENABLED

#define USE_SKELETON (defined(SKELETON_ENABLED) && (!defined(__OSX__)))

int main()
{
#if USE_SKELETON
    puts("Use skeleton!");
#else
    puts("Don't use skeleton!\n");
#endif
    return 0;
}

Solution

  • You should put the conditions around defining the macro, not in the macro definition

    #if defined(SKELETON_ENABLED) && !defined(__OSX__)
    #define USE_SKELETON 1
    #else
    #define USE_SKELETON 0
    #endif
    

    BTW, __OSX__ is not predefined on a Mac. You can use

    clang -dM -E -x c /dev/null
    

    to see all the predefined macros, the only one containing OSX is TARGET_OS_OSX.