使用跨平台 Android/iOS 的 C 中的 Ifdef 无法正常工作

Ifdef in C using crossplatform Android/iOS doesn´t work propertly

我有一段代码可以改变不同平台之间的调用。但是我在使用 iphone 和 ipad 时检测到问题,TARGET_OS_IPHONE 定义仅适用于 iphone,但不适用于 ipad,我不想尝试使用否则要考虑的是 ipad,因为将来这可能会成为问题的根源。

我花了 2 天时间寻找由此引起的 opencv 矩阵中的颜色问题.....

我的问题,是否存在一个合适的官方解决方案来执行 iOS S.O (Iphone AND Ipad) 中的一段代码?

作为参考,我总是在 this link 中查找。

select代码示例:

#ifdef __linux__ 
    // All linux arch
#elif _WIN32
    // Windows 32 and 64
#elif __APPLE__
    #ifdef TARGET_OS_IPHONE
         // iOS, no work with iPAD
    #elif TARGET_IPHONE_SIMULATOR
        // iOS Simulator
    #elif TARGET_OS_MAC
        // Other kinds of Mac OS
    #else
        // Unsupported platform
    #endif
#elif __ANDROID__
  // Android all versions
#else
  //  Unsupported architecture
#endif

尝试在“__APPLE__”的情况下包含 "TargetConditionals.h" 文件。 此文件包括所有引用 Apple 设备的宏。

无论如何,使用你的例子,正确的方法应该是:

#ifdef __linux__ 
// All linux arch
#elif _WIN32
// Windows 32 and 64
#elif __APPLE__
    #include "TargetConditionals.h"     // <--- Include this
    #ifdef TARGET_OS_IOS                // <--- Change this with a proper definition
    // iOS, including iPhone and iPad
    #elif TARGET_IPHONE_SIMULATOR
    // iOS Simulator
    #elif TARGET_OS_MAC
    // Other kinds of Mac OS
    #else
    // Unsupported platform
#endif
#elif __ANDROID__
// Android all versions
#else
//  Unsupported architecture
#endif

此外,您可以在那里定义 'TARGET_OS_IPHONE',但 'TARGET_OS_IOS' 更合适。

您可以直接包含此文件,也可以在您的磁盘中按照以下路径找到它:'/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/TargetConditionals.h '.