Determine System Type Using Compiler Predefined Macros

Leave a Comment

How to list compiler predefined macros

Clang/LLVM:
clang -dM -E -x c /dev/null     
clang++ -dM -E -x c++ /dev/null
GNU GCC/G++:
gcc -dM -E -x c /dev/null   
g++ -dM -E -x c++ /dev/null 

How to detect the operating system type using compiler predefined macros

Linux

#if defined(__linux__)
    /* Linux. --------------------------------------------------- */

#endif

BSD

#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD)
    /* BSD (DragonFly BSD, FreeBSD, OpenBSD, NetBSD). ----------- */

#endif
#endif

Reference

http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

0 comments:

Post a Comment