【问题标题】:Latest C++11 features with Android NDKAndroid NDK 的最新 C++11 功能
【发布时间】:2013-06-13 03:01:19
【问题描述】:

我正在尝试将 C++11 线程工具与 Android NDK 一起使用,但不确定如何使其使用最新的编译器。

我有 Clang 3.2 并且可以构建 iOS 应用程序。我想知道是否有办法用 Android NDK 做到这一点?

如果不是,那我应该如何使用 gcc 4.8 构建?

【问题讨论】:

标签: android gcc c++11 android-ndk clang


【解决方案1】:

(我正在处理 NDK 版本 r9b) 要为应用程序的所有源代码(以及包括的任何模块)启用 C++11 支持,请在 Application.mk 中进行以下更改:

# use this to select gcc instead of clang
NDK_TOOLCHAIN_VERSION := 4.8
# OR use this to select the latest clang version:
NDK_TOOLCHAIN_VERSION := clang


# then enable c++11 extentions in source code
APP_CPPFLAGS += -std=c++11
# or use APP_CPPFLAGS := -std=gnu++11

否则,如果您希望仅在您的模块中支持 C++11,请将此行添加到您的 Android.mk 中,而不是使用 APP_CPPFLAGS

LOCAL_CPPFLAGS += -std=c++11

在这里阅读更多: http://adec.altervista.org/blog/ndk_c11_support/

【讨论】:

    【解决方案2】:

    NDK 修订版 10 具有 Clang 3.6 工具链。使用它:

    NDK_TOOLCHAIN_VERSION := clang3.6
    

    或使用最新的可用 Clang 工具链

    NDK_TOOLCHAIN_VERSION := clang
    

    【讨论】:

    • 很奇怪,clang3.6 正在为我使用 3.8(目前是 5.1.1 上的最新版本,通过预处理器 #ifdef 检查),例如 clang。而clang3.8 是一个错误。
    【解决方案3】:

    NDK revision 8e 捆绑了 Clang 3.2 编译器。使用它,你就可以开始了。

    【讨论】:

    • 您可以通过这种方式使用编译器,但是使用线程(我正在使用 APP_STL := gnustl_static)还有其他事情要做,因为我收到一个错误错误:没有名为“线程”的成员' 在命名空间 'std' 中;你的意思是“fread”吗?
    • @Kimi $NDK/docs/STABLE-APIS.html 中的 NDK 文档说明如下:Note that the Android C library includes support for pthread (<pthread.h>), so "LOCAL_LIBS := -lpthread" is not needed. The same is true for real-time extensions (-lrt on typical Linux distributions)。您能否将您遇到的问题粘贴到问题中,以便更轻松地跟踪问题?
    【解决方案4】:

    首先,要决定使用哪个工具链,编辑您的“application.mk”(不要与 android.mk 混淆)并插入 gcc 4.8:

    NDK_TOOLCHAIN_VERSION := 4.8
    

    或者如果你想要clang:

    NDK_TOOLCHAIN_VERSION := clang
    

    但这与线程无关。这只会定义要使用的工具链。

    现在关于线程,这里是一个简单的 android NDK 示例:

    #include <pthread.h> // <--- IMPORTANT
    
    // This will be used to pass some data to the new thread, modify as required
    struct thread_data_arguments
    {
        int  value_a
        bool value_b;
    };
    
    //---------------------------------
    
    // This function will be executed in the new thread, do not forget to put * at the start of the function name declaration
    void *functionRunningInSeparateThread(void *arguments)
    {
        struct thread_data_arguments *some_thread_arguments = (struct thread_data_arguments*)arguments;
    
        if (some_thread_arguments->value_b == true)
        {
            printf("VALUE= %i", some_thread_arguments->value_a);
        }
    
        // Signal the end of the thread execution
        pthread_exit(0);
    }
    
    //---------------------------------
    
    // This is the actual function creating and starting the new thread
    void startThread()
    {
        // Lets pass some data to the new thread, you can pass anything even large data, 
        // for that you only need to modify thread_data_arguments as required
        struct thread_data_arguments *some_thread_arguments;
        some_thread_arguments = (thread_data_arguments*)malloc(sizeof(*some_thread_arguments));
    
        some_thread_arguments->value_a = 12345;
        some_thread_arguments->value_b = true;
    
        // Create and start the new thread
        pthread_create(&native_thread, NULL, functionRunningInSeparateThread, (void*)some_thread_arguments)
    }
    

    【讨论】:

      【解决方案5】:

      对于 ndk 构建,打开 Application.mk 并添加以下信息。在其中(如果使用 r8e):

      NDK_TOOLCHAIN_VERSION=4.7
      

      注意:如果您使用的是 NDK 修订版 9,请使用 4.8。

      【讨论】:

        【解决方案6】:

        请注意,Android gcc 支持现已弃用。您现在应该使用 clang。请阅读version 11 release notes。您可以指定:

        NDK_TOOLCHAIN_VERSION=clang
        

        根据您安装的 NDK 使用最新版本。此外——在撰写本文时——最新的 NDK (v12) 只能通过 Android Studio 访问,不能通过下载页面或独立 SDK 管理器访问。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-14
          • 1970-01-01
          • 1970-01-01
          • 2013-08-11
          • 1970-01-01
          相关资源
          最近更新 更多