15

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
  • 13,621
  • 9
  • 55
  • 84
  • See e.g. http://stackoverflow.com/questions/15269496/how-to-compile-c11-code-with-android-ndk-and-eclipse and http://stackoverflow.com/questions/14014659/android-ndk-error-must-be-enabled-with-the-std-c11-or-std-gnu11-compiler – Michael Jun 17 '13 at 07:57
  • @Michael, I need to build with clang 3.2 or gcc 4.8, or be sure that it is not possible and use pthreads. – Kimi Jun 17 '13 at 08:39
  • Since NDK revision 10d - GCC 4.8 is the default for all 32-bit ABIs – Victor Laskin Feb 04 '15 at 22:09

6 Answers6

18

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

Mouze
  • 706
  • 5
  • 10
11

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
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
2

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

Samveen
  • 3,482
  • 35
  • 52
  • 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'? – Kimi Jun 19 '13 at 11:55
  • @Kimi the NDK documentation in `$NDK/docs/STABLE-APIS.html` says the following: `Note that the Android C library includes support for pthread (), 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? – Samveen Jun 25 '13 at 12:15
1

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)
}
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
1

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.

nomann
  • 2,257
  • 2
  • 21
  • 24
0

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.

EntangledLoops
  • 1,951
  • 1
  • 23
  • 36