繁体   English   中英

Android NDK的最新C ++ 11功能

[英]Latest C++11 features with Android NDK

我正在尝试使用Android NDK的C ++ 11线程工具,但不知道如何使它使用最新的编译器。

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

如果没有,那么我应该如何用gcc 4.8构建?

(我正在解决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/

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

NDK_TOOLCHAIN_VERSION := clang3.6

或使用最新的Clang工具链

NDK_TOOLCHAIN_VERSION := clang

NDK版本8e捆绑了Clang 3.2编译器。 使用它,你很高兴。

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

NDK_TOOLCHAIN_VERSION := 4.8

或者如果你想要铿锵:

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)
}

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

NDK_TOOLCHAIN_VERSION=4.7

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

请注意, 现在不推荐使用Android gcc支持 你现在应该使用clang。 请阅读版本11发行说明 您可以指定:

NDK_TOOLCHAIN_VERSION=clang

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM