Latest C++11 features with Android NDK

23,673

Solution 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

Solution 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/

Solution 3

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

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

Solution 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.

Share:
23,673
Kimi
Author by

Kimi

Updated on August 05, 2022

Comments

  • Kimi
    Kimi almost 2 years

    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?

  • Kimi
    Kimi almost 11 years
    You can use a compiler this way, but there is something else to be done to use threading (I am using APP_STL := gnustl_static) since I'm getting an error error: no member named 'thread' in namespace 'std'; did you mean 'fread'?
  • Samveen
    Samveen almost 11 years
    @Kimi the NDK documentation in $NDK/docs/STABLE-APIS.html says the following: 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). Could you paste the issues you face into the question, as that'll make it easier to trace the issue?
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com almost 8 years
    Weird, clang3.6 is using 3.8 for me (currently the most recent on 5.1.1, checked via preprocessor #ifdef), like clang. And clang3.8 is an error.