Latest C++11 features with Android NDK

2019-01-16 20:40发布

问题:

I am trying to use C++11 threading facilities with Android NDK, but not sure how to make it use the latest compilers.

I have Clang 3.2 and can build iOS apps. I wonder if there is a way to do it with Android NDK?

If not, then how should I build with gcc 4.8?

回答1:

NDK revision 10 has the Clang 3.6 toolchain. Use it:

NDK_TOOLCHAIN_VERSION := clang3.6

or use the latest available Clang toolchain

NDK_TOOLCHAIN_VERSION := clang


回答2:

(I'm addressing the NDK version r9b) To enable C++11 support for all source code of the application (and so any modules included) make the following change in the 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

Otherwise, if you wish to have C++11 support only in your module, add this lines into your Android.mk instead of use APP_CPPFLAGS

LOCAL_CPPFLAGS += -std=c++11

Read more here: http://adec.altervista.org/blog/ndk_c11_support/



回答3:

NDK revision 8e has the Clang 3.2 compiler bundled in it. Use it and you're good to go.



回答4:

First, to decide which toolchain to use, edit your "application.mk" (do not confuse with android.mk) and insert for gcc 4.8:

NDK_TOOLCHAIN_VERSION := 4.8

or if you want clang:

NDK_TOOLCHAIN_VERSION := clang

But this has nothing to do with threads. This will only define which toolchain to use.

Now about threads, here is a simple example for 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:

For ndk builds, open Application.mk and add following info. in it (if using r8e):

NDK_TOOLCHAIN_VERSION=4.7

Note: Please use 4.8 in case you are using NDK revision 9.



回答6:

Note that Android gcc support is now deprecated. You should now be using clang. Please read the version 11 release notes. You can specify:

NDK_TOOLCHAIN_VERSION=clang

To use the latest version based on your installed NDK. Also---as of this writing---the latest NDK (v12) is only accessable via Android Studio, and not through either the Downloads page or Standalone SDK Manager.