Enable C++11 compilation support in Android NDK toolchain

Summary

Android NDK allows to use C++11 extensions from the r8e (released on March 2013), but the NDK build toolchain needs some configuration steps, otherwise the compilation will result in errors. Some multi-thread features missing from g++ 4.6 are now avaliable in STL using g++ 4.8.

Warning: Clang 3.3 compiler from NDK r9b is C++11-feature complete, STL and stdlib++ on Android are not. So currently full support of the new C++11 features in not stable

[banner align=”aligncenter”]

How to activate C++11 features

The following instructions address a NDK installation in a Win 7 Pro 64x (SP 1)/Cygwin environment, but can be applicable to all dev systems.

C++11

Requirements:

  1. must have NDK version r8e or above installed (view the RELEASE.TXT file in the NDK root folder). Better if r9b is used.

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
# otherwise, the following line select the latest clang version.
# NDK_TOOLCHAIN_VERSION := clang
# enable c++11 extensions in source code
APP_CPPFLAGS += -std=c++11
# or use APP_CPPFLAGS := -std=gnu++11
# Following is STL implementation. Can be use _static or _shared version, the last one need a call to loadLibrary BEFORE use the C++ code in the shared library
APP_STL := gnustl_static

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

Features supported

  • atomic
  • chrono
  • condition_variable
  • memory (shared_ptr, unique_ptr)
  • mutex (lock_guard,mutex,unique_lock)
  • thread (see comments)
  • lamdas and auto keyword

An example of using the new standard can be found in the NDK (from r9), in {NDK_PATH}\samples\test-libstdc++

std::thread support in NDK 9b

Currently (as February 2014) many developers complain of crashes in the use of std :: thread. See these following discussions:

https://code.google.com/p/android/issues/detail?id=58616

 

Some useful reference

For understand the current level of support of C++ language in the NDK, the first place to view is the CPLUSPLUS-SUPPORT.html in the NDK documentation.

ISO C++ Standard Home Page
http://isocpp.org/std/the-standard

For Eclipse usage, please view this thread:
http://stackoverflow.com/questions/15269496/how-to-compile-c11-code-with-android-ndk-and-eclipse

A good start point to understand the new C++11 standard can be found here:
http://www.stroustrup.com/C++11FAQ.html

The status of C++11 Language Features in many compilers can be found here:
http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport

A specific page addressing C++11 support of the llvm clang compiler can be found here:
http://clang.llvm.org/cxx_status.html

Interesting Proof of concept of a multi-threading task, implemented with different thread-safe constructs
https://vilimpoc.org/blog/2013/10/05/c11-support-on-android-with-g-4-8/

 

Pubblicato da adec

I'm a computer science engineer, Rome, Italy.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.