这篇文章回答了你的问题:
这篇文章很长,并且包含难以复制的表格,但这里是精髓:
您可以通过以下方式检测 Unix 风格的操作系统:
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
/* UNIX-style OS. ------------------------------------------- */
#endif
一旦你知道它是 Unix,你就可以找到它是否是 POSIX 和 POSIX 版本:
#include <unistd.h>
#if defined(_POSIX_VERSION)
/* POSIX compliant */
#endif
您可以通过以下方式检查 BSD 派生系统:
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD)
/* BSD (DragonFly BSD, FreeBSD, OpenBSD, NetBSD). ----------- */
#endif
#endif
和 Linux:
#if defined(__linux__)
/* Linux */
#endif
和苹果的操作系统
#if defined(__APPLE__) && defined(__MACH__)
/* Apple OSX and iOS (Darwin) */
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR == 1
/* iOS in Xcode simulator */
#elif TARGET_OS_IPHONE == 1
/* iOS on iPhone, iPad, etc. */
#elif TARGET_OS_MAC == 1
/* OS X */
#endif
#endif
带有 Cygwin 的 Windows
#if defined(__CYGWIN__) && !defined(_WIN32)
/* Cygwin POSIX under Microsoft Windows. */
#endif
非 POSIX Windows 具有:
#if defined(_WIN64)
/* Microsoft Windows (64-bit) */
#elif defined(_WIN32)
/* Microsoft Windows (32-bit) */
#endif
全文列出了以下符号,并显示了哪些系统定义了它们以及何时定义它们:_AIX、__APPLE__、__CYGWIN32__、__CYGWIN__、__DragonFly__、__FreeBSD__、__gnu_linux、@987654339 @,__hpux,linux,__linux,__linux__,__MACH__,__MINGW32__,__MINGW64__,__NetBSD__,__OpenBSD__,_POSIX_IPV6,_POSIX_IPV6,_POSIX_IPV6,@9876 _POSIX_THREADS,_POSIX_VERSION,sun,__sun,__SunOS,__sun__,__SVR4,__svr4__,TARGET_IPHONE_SIMULATOR,TARGET_OS_EMBEDDED,TARGET_OS_IPHONE,TARGET_OS_MAC,UNIX,unix,__unix,__unix__,WIN32,_WIN32,__WIN32,__WIN32__,WIN64,_WIN64,@987654375,@,@987654375,@,@987654376 __WINNT, __WINNT__.
A related article (archive.org link) 涵盖了检测编译器和编译器版本。它列出了以下符号:__clang__、__GNUC__、__GNUG__、__HP_aCC、__HP_cc、__IBMCPP__、__IBMC__、__ICC、__INTEL_COMPILER、_MSC_VER3、 __SUNPRO_C,__SUNPRO_CC 用于检测编译器,__clang_major__,__clang_minor__,__clang_patchlevel__,__clang_version__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__,__GNUC__,@9876544399@9876544399@9801 @, __IBMCPP__, __IBMC__, __ICC, __INTEL_COMPILER, __INTEL_COMPILER_BUILD_DATE, _MSC_BUILD, _MSC_FULL_VER, _MSC_VER, __PGIC_MINOR__, @987644412@, __PGIC__, __PGIC__, _MSC_FULL_VER __SUNPRO_CC、__VERSION__、__xlC_ver__、__xlC__、__xlc__ 用于检测编译器版本。