如何在C预处理器中检测可靠的Mac OS X、iOS、Linux、Windows ?

时间:2021-11-24 16:49:07

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上进行编译,那么在预处理过程中我如何能够可靠地检测它们呢?

3 个解决方案

#1


412  

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的一个例子:

#ifdef _WIN32
   //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

This defined macroses depends on compiler that you are going to use.

这个定义的宏依赖于您将要使用的编译器。

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.

_WIN64 #ifdef可以嵌套到_WIN32 #ifdef中,因为在针对Windows的时候定义了_WIN32,而不仅仅是x86版本。这可以防止代码重复,如果其中一些包含对两个都是通用的。

#2


27  

As Jake points out, TARGET_IPHONE_SIMULATOR is a subset of TARGET_OS_IPHONE.

正如Jake指出的,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

#3


1  

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.

这是一个必然的答案:在[本网站]上的人们已经花了时间来为每个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.

不管怎样,我发现这些表格信息量很大,我想分享一下。

#1


412  

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的一个例子:

#ifdef _WIN32
   //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

This defined macroses depends on compiler that you are going to use.

这个定义的宏依赖于您将要使用的编译器。

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.

_WIN64 #ifdef可以嵌套到_WIN32 #ifdef中,因为在针对Windows的时候定义了_WIN32,而不仅仅是x86版本。这可以防止代码重复,如果其中一些包含对两个都是通用的。

#2


27  

As Jake points out, TARGET_IPHONE_SIMULATOR is a subset of TARGET_OS_IPHONE.

正如Jake指出的,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

#3


1  

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.

这是一个必然的答案:在[本网站]上的人们已经花了时间来为每个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.

不管怎样,我发现这些表格信息量很大,我想分享一下。