【问题标题】:Is there an #ifdef ANDROID equivalent to #ifdef WIN32是否有与#ifdef WIN32 等效的#ifdef ANDROID
【发布时间】:2011-12-12 14:11:51
【问题描述】:

我有一些 C++ 代码有一堆 #ifdef WIN32 否则我们假设它的 IOS 代码。不过,我现在正尝试将相同的 c++ 代码用于 android 端口。

#ifdef WIN32 是否有某种等价物||安卓?

【问题讨论】:

    标签: android c++


    【解决方案1】:

    宏

    关于预定义宏,有著名的predef.sf.net。

    寻找 Android 会弹出the devices page。那里:

    安卓

    头文件中必须包含以下宏。

    Type    | Macro           | Format  | Description
    Version | __ANDROID_API__ | V       | V = API Version
    

    例子

    Android Version | __ANDROID_API__
    1.0             | 1
    1.1             | 2
    1.5             | 3
    1.6             | 4
    2.0             | 5
    2.0.1           | 6
    2.1             | 7
    2.2             | 8
    2.3             | 9
    2.3.3           | 10
    3.0             | 11 
    


    示例

    #ifdef __ANDROID__
    # include <android/api-level.h>
    #endif
    
    #ifdef __ANDROID_API__
    this will be contained on android
    #endif
    
    #ifndef __ANDROID_API__
    this will NOT be contained for android builds
    #endif
    
    #if defined(WIN32) || defined(__ANDROID_API__)
    this will be contained on android and win32
    #endif
    

    如果要包含足够高版本的代码块,则必须先检查是否存在,然后才能进行算术比较:

    #ifdef __ANDROID_API__
    # if __ANDROID_API__ > 6 
        at least android 2.0.1
    # else 
        less than 2.0.1
    # endif
    #endif
    


    多个条件

    你不能这样做#ifdef FOO || BAR。标准只定义了语法

    # ifdef identifier new-line
    

    但你可以使用一元运算符defined:

    #if defined(FOO) && defined(BAR)
    

    您也可以使用! 否定结果:

    #if !defined(FOO) && defined(BAR)
       this is included only if there is no FOO, but a BAR.
    

    当然还有逻辑或:

    #if defined(FOO) || defined(BAR)
       this is included if there is FOO or BAR (or both)
    

    【讨论】:

    • 我不太确定这有什么帮助
    • @Metropolis:我的理解是他正在寻找一面旗帜。我将添加一个关于如何使用它的示例。
    • 这看起来很有希望。我会试一试,然后回复你。感谢您提供额外的示例。
    • 我实际上尝试了“#if defined(WIN32) || defined(ANDROID_API)”,它忽略了如果,但如果我将其更改为“#if defined( WIN32)|| 定义(ANDROID)”它工作。知道为什么会这样吗?在我们决定什么是正确的之前,我从正确答案中删除了这个。
    • 使用ANDROID确实有效,我只是不明白为什么。
    猜你喜欢
    • 2012-03-21
    • 2010-10-29
    • 2014-10-16
    • 2013-06-10
    • 2017-01-10
    • 2010-11-21
    • 2010-12-15
    • 2011-02-18
    • 1970-01-01
    相关资源
    最近更新 更多