Android NDK中的最新C++11功能特性

15

我尝试在Android NDK中使用C++11线程库,但不确定如何让它使用最新的编译器。

我有Clang 3.2并且可以构建iOS应用程序。 我想知道是否有一种方法可以在Android NDK中这样做?

如果不行,那么我该如何使用gcc 4.8进行构建?


请参考以下链接:https://dev59.com/T2Up5IYBdhLWcg3wdXWa 和 https://dev59.com/MmYr5IYBdhLWcg3wBlzp。 - Michael
@Michael,我需要使用clang 3.2或gcc 4.8构建,或者确定不可能并使用pthreads。 - Kimi
自NDK修订版10d起,GCC 4.8是所有32位ABI的默认设置。 - Victor Laskin
6个回答

18

(我在谈论 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/


11

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 则会出错。 - Ciro Santilli OurBigBook.com

2

NDK修订版8e中包含Clang 3.2编译器。使用它,一切就准备就绪。


你可以这样使用编译器,但是要使用线程还需要做一些其他的事情(我正在使用APP_STL:= gnustl_static),因为我遇到了错误error: no member named 'thread' in namespace 'std'; did you mean 'fread'? - Kimi
@Kimi,NDK文档在$NDK/docs/STABLE-APIS.html中指出:请注意,Android C库包括对pthread(<pthread.h>)的支持,因此不需要使用“LOCAL_LIBS:= -lpthread”。对于实时扩展(在典型的Linux发行版上为-lrt),也是如此。您能否将您面临的问题粘贴到问题中,这样更容易跟踪问题? - Samveen

1

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

NDK_TOOLCHAIN_VERSION=4.7

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

1
首先,为了决定使用哪个工具链,请编辑您的 "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)
}

0
请注意,Android gcc支持现已弃用。您现在应该使用clang。请阅读版本11发行说明。您可以指定:
NDK_TOOLCHAIN_VERSION=clang

为了使用基于您安装的 NDK 的最新版本。此时此刻,最新的 NDK(v12)只能通过 Android Studio 访问,而不能通过下载页面或独立 SDK 管理器访问。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接