【问题标题】:Ifdef in C using crossplatform Android/iOS doesn´t work propertly使用跨平台 Android/iOS 的 C 中的 Ifdef 无法正常工作
【发布时间】:2016-05-03 04:01:54
【问题描述】:

我有一段代码可以改变不同平台之间的调用。但是我发现使用 iphone 和 ipad 有问题,TARGET_OS_IPHONE 定义只适用于 iphone,但没有 ipad,我不想尝试使用 else 认为是 ipad,因为将来这可能会成为问题的根源。

我已经失去了 2 天的时间来寻找由此引起的 opencv 矩阵中的颜色问题.....

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

作为参考,我总是查看this 链接。

选择代码示例:

#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

【问题讨论】:

    标签: android ios cross-platform c-preprocessor


    【解决方案1】:

    尝试在“__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/包括/TargetConditionals.h'。

    【讨论】:

    • 哦!将代码段移动到另一个项目时,我似乎错过了该标题。它提供了我正在寻找的所有宏......非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2019-09-11
    • 1970-01-01
    相关资源
    最近更新 更多