如何在 C 预处理器中可靠地检测 Mac OS X、iOS、Linux、Windows?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5919996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?
提问by Eonil
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 上编译,我如何在预处理过程中可靠地检测它们?
回答by Evgeny Gavrin
There are predefined macros that are used by most compilers, you can find the list here. GCC compiler predefined macros can be found here. Here is an example for gcc:
大多数编译器都使用预定义的宏,您可以在此处找到列表。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. This prevents code duplication if some header includes are common to both
(also WIN32
without underscore allows IDE to highlight the right partition of code).
该_WIN64
#ifdef
可嵌套到_WIN32
#ifdef
,因为_WIN32
针对的Windows版本64时,甚至定义。如果某些标头包含是共同的,这可以防止代码重复(也WIN32
没有下划线允许 IDE 突出显示正确的代码分区)。
回答by PatchyFog
As Jake points out, TARGET_IPHONE_SIMULATOR
is a subset of TARGET_OS_IPHONE
.
正如Hyman指出的那样,TARGET_IPHONE_SIMULATOR
是TARGET_OS_IPHONE
.
Also, TARGET_OS_IPHONE
is a subset of TARGET_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
回答by houtanb
Kind of a corollary answer: the people on [this site]have taken the time to make tables of macros defined for every OS/compiler pair.
一种必然的答案:[本站点] 上的人们花时间为每个操作系统/编译器对定义了宏表。
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
不是在带有 Cygwin (POSIX) 的 Windows 上定义的,而它是为在 Windows、Cygwin(非 POSIX)和 MinGW 上使用每个可用的编译器(Clang、GNU、Intel 等)定义的。 .
Anyway, I found the tables quite informative and thought I'd share here.
无论如何,我发现这些表格非常有用,并认为我会在这里分享。