本文翻译自:How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor? [duplicate]
This question already has an answer here:这个问题已经在这里有了答案:
How do I check OS with a preprocessor directive?如何使用预处理指令检查OS?16 answers16个答案
If there's some cross-platform C/C++ code that should be compiled on Mac OS X, iOS, Linux, Windows, how can I detect them reliably during preprocessor process?如果有一些跨平台的C / C ++代码应该在Mac OS X,iOS,Linux,Windows上编译,那么在预处理器过程中如何可靠地检测到它们?
#1楼
参考:/question/Oq3o/如何在C预处理器中可靠地检测Mac-OS-X-iOS-Linux-Windows-重复
#2楼
As Jake points out,TARGET_IPHONE_SIMULATOR
is a subset ofTARGET_OS_IPHONE
.正如Jake指出的那样,TARGET_IPHONE_SIMULATOR
是TARGET_OS_IPHONE
的子集。
Also,TARGET_OS_IPHONE
is a subset ofTARGET_OS_MAC
.此外,TARGET_OS_IPHONE
的一个子集TARGET_OS_MAC
。
So a better approach might be:因此,更好的方法可能是:
#ifdef _WIN64//define something for Windows (64-bit)#elif _WIN32//define something for Windows (32-bit)#elif __APPLE__#include "TargetConditionals.h"#if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR// define something for simulator #elif TARGET_OS_IPHONE// define something for iphone #else#define TARGET_OS_OSX 1// define something for OSX#endif#elif __linux// linux#elif __unix // all unices not caught above// Unix#elif __posix// POSIX#endif
#3楼
Kind of a corollary answer: the people on [this site] have taken the time to make tables ofmacros defined for every OS/compiler pair.一个必然的答案: [此站点]上的人花了一些时间为每个OS /编译器对定义宏表。
For example, you can see that_WIN32
is NOT defined on Windows with Cygwin (POSIX), while it IS defined for compilation on Windows, Cygwin (non-POSIX), and MinGW with every available compiler (Clang, GNU, Intel, etc.).例如,您可以看到_WIN32
在Windows上未使用Cygwin(POSIX)定义,而它是在Windows,Cygwin(非POSIX)和MinGW上使用每个可用的编译器(Clang,GNU,Intel等)定义的。 )。
Anyway, I found the tables quite informative and thought I'd share here.无论如何,我发现这些表非常有用,并认为我会在这里分享。
#4楼
There are predefined macros that are used by most compilers, you can find the list here .大多数编译器都使用预定义的宏,您可以在此处找到列表。GCC compiler predefined macros can be found here .GCC编译器预定义的宏可以在这里找到。Here is an example for gcc:这是gcc的示例:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)//define something for Windows (32-bit and 64-bit, this part is common)#ifdef _WIN64//define something for Windows (64-bit only)#else//define something for Windows (32-bit only)#endif#elif __APPLE__#include <TargetConditionals.h>#if TARGET_IPHONE_SIMULATOR// iOS Simulator#elif TARGET_OS_IPHONE// iOS device#elif TARGET_OS_MAC// Other kinds of Mac OS#else# error "Unknown Apple platform"#endif#elif __linux__// linux#elif __unix__ // all unices not caught above// Unix#elif defined(_POSIX_VERSION)// POSIX#else# error "Unknown compiler"#endif
The defined macros depend on the compiler that you are going to use.定义的宏取决于您将要使用的编译器。
The_WIN64
#ifdef
can be nested into the_WIN32
#ifdef
because_WIN32
is even defined when targeting the Windows x64 version.该_WIN64
#ifdef
可以被嵌套到_WIN32
#ifdef
因为_WIN32
针对Windows的64位版本时,甚至定义。This prevents code duplication if some header includes are common to both (alsoWIN32
without underscore allows IDE to highlight the right partition of code).如果某些头文件包含了两者,这可以防止代码重复(同样,不带下划线的WIN32
也允许IDE突出显示正确的代码分区)。