如何可靠地检测的Mac OS X,iOS设备的Linux时,Windows用C preprocessor? [英] How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?
问题描述
如果有一些跨平台的C / C ++ code应该在Mac OS X,iOS的,Linux和Windows上编译,我怎么能在preprocessor过程中检测到它们可靠?
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?
推荐答案
有由大多数编译器使用predefined宏,你可以找到列表中的 [这里] 。 GCC编译器predefined宏可以发现 [这里] 。
下面是gcc的一个例子:
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:
#ifdef _WIN32
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
//define something for Windows (64-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
这个定义macroses取决于编译器,您要使用。
This defined macroses depends on compiler that you are going to use.
的 _WIN64
#IFDEF
可以嵌套到 _WIN32
#IFDEF
,因为 _WIN32
针对Windows,不仅在x86版本时定义。如果一些包括该prevents code复制是共同的。
The _WIN64
#ifdef
can be nested into the _WIN32
#ifdef
because _WIN32
is defined when targeting Windows, not only the x86 version. This prevents code duplication if some includes are common to both.
这篇关于如何可靠地检测的Mac OS X,iOS设备的Linux时,Windows用C preprocessor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!