page.title=C++ Library Support @jd:body

On this page

  1. Helper Runtimes
  2. Runtime Characteristics
  3. Important Considerations
  4. Licensing

The Android platform provides a very minimal C++ runtime support library ({@code libstdc++}). This minimal support does not include, for example:

The NDK provides headers for use with this default library. In addition, the NDK provides a number of helper runtimes that provide additional features. This page provides information about these helper runtimes, their characteristics, and how to use them.

Helper Runtimes

Table 1 provides names, brief explanations, and features of runtimes available inthe NDK.

Table 1. NDK Runtimes and Features.

Name Explanation> Features
{@code libstdc++} (default) The default minimal system C++ runtime library. N/A
{@code gabi++_static} The GAbi++ runtime (static). C++ Exceptions and RTTI
{@code gabi++_shared} The GAbi++ runtime (shared). C++ Exceptions and RTTI
{@code stlport_static} The STLport runtime (static). C++ Exceptions and RTTI; Standard Library
{@code stlport_shared} The STLport runtime (shared). C++ Exceptions and RTTI; Standard Library
{@code gnustl_static} The GNU STL (static). C++ Exceptions and RTTI; Standard Library
{@code gnustl_shared} The GNU STL (shared). C++ Exceptions and RTTI; Standard Library
{@code c++_static} The LLVM libc++ runtime (static). C++ Exceptions and RTTI; Standard Library
{@code c++_shared} The LLVM libc++ runtime (shared). C++ Exceptions and RTTI; Standard Library

How to set your runtime

Use the {@code APP_STL} variable in your {@code Application.mk} file to specify the runtime you wish to use. Use the values in the "Name" column in Table 1 as your setting. For example:

APP_STL := gnustl_static

You may only select one runtime for your app, and can only do in {@code Application.mk}.

Even if you do not use the NDK build system, you can still use STLport, libc++ or GNU STL. For more information on how to use these runtimes with your own toolchain, see Standalone Toolchain.

Runtime Characteristics

libstdc++ (default system runtime)

This runtime only provides the following headers, with no support beyond them:

GAbi++ runtime

This runtime provides the same headers as the default runtime, but adds support for RTTI (RunTime Type Information) and exception handling.

STLport runtime

This runtime is an Android port of STLport (http://www.stlport.org). It provides a complete set of C++ standard library headers. It also, by embedding its own instance of GAbi++, provides support for RTTI and exception handling.

While shared and static versions of this runtime are avilable, we recommend using the shared version. For more information, see Static runtimes.

The shared library file is named {@code libstlport_shared.so} instead of {@code libstdc++.so} as is common on other platforms.

In addition to the static- and shared-library options, you can also force the NDK to build the library from sources by adding the following line to your {@code Application.mk} file, or setting it in your environment prior to building:

STLPORT_FORCE_REBUILD := true

GNU STL runtime

This runtime is the GNU Standard C++ Library, ({@code libstdc++-v3}). Its shared library file is named {@code libgnustl_shared.so}.

libC++ runtime:

This runtime is an Android port of LLVM libc++. Its shared library file is named {@code libc++_shared.so}.

By default, this runtime compiles with {@code -std=c++11}. As with GNU {@code libstdc++}, you need to explicitly turns on exceptions or RTTI support. For information on how to do this, see C++ Exceptions and RTTI.

The NDK provides prebuilt static and shared libraries for {@code libc++} compiled by Clang 3.4, but you can force the NDK to rebuild {@code libc++} from sources by adding the following line to your {@code Application.mk} file, or setting it in your environment prior to building:

LIBCXX_FORCE_REBUILD := true

atomic support

If you include {@code <atomic>}, it's likely that you also need {@code libatomic}. If you are using {@code ndk-build}, add the following line:

LOCAL_LDLIBS += -latomic

If you are using your own toolchain, use:

-latomic

Note: {@code -latomic} is only available for GCC 4.8. Because Clang 3.5 and Clang 3.6 use GCC 4.8's headers and libraries, as well as its {@code as} and {@code ld} options, those versions of Clang also get {@code -latomic}.

Compatibility

Around 99% of tests pass when compiling {@code libc++} with Clang 3.4 for all supported ABIs. The failures are mostly in the areas of {@code wchar_t} and locales that Android bionic doesn't support. Switching locale from the default produces the following warning in {@code logcat}:

newlocale() WARNING: Trying to set locale to en_US.UTF-8 other than "", "C" or "POSIX"

We do not recommend using {@code libc++} with GCC 4.6 because of GCC 4.6's limited c++11 support.

For information on {@code libc++} tests that fail to compile, {@code black_list*} in {@code $NDK/tests/device/test-libc++-shared-full/jni/Android.mk}. For information about tests that fail to run correctly, see {@code $NDK/tests/device/test-libc++-shared-full/BROKEN_RUN}. {@code $NDK}, here, is the your NDK installation's root directory.

Important Considerations

C++ Exceptions

In all versions of the NDK later than NDKr5, the NDK toolchain allows you to use C++ runtimes that support exception handling. However, to ensure compatibility with earlier releases, it compiles all C++ sources with {@code -fno-exceptions} support by default. You can enable C++ exceptions either for your entire app, or for individual modules.

To enable exception-handling support for your entire app, add the following line to your {@code Application.mk} file. To enable exception-handling support for individual modules', add the following line to their respective {@code Android.mk} files.

APP_CPPFLAGS += -fexceptions

RTTI

In all versions of the NDK later than NDKr5, the NDK toolchain allows you to use C++ runtimes that support RTTI. However, to ensure compatibility with earlier releases, it compiles all C++ sources with {@code -fno-rtti} by default.

To enable RTTI support for your entire app for your entire application, add the following line to your {@code Application.mk} file:

APP_CPPFLAGS += -frtti
To enable RTTI support for individual modules, add the following line to their respective {@code Android.mk} files:
LOCAL_CPP_FEATURES += rtti
Alternatively, you can use:
LOCAL_CPPFLAGS += -frtti

Static runtimes

Linking the static library variant of a C++ runtime to more than one binary may result in unexpected behavior. For example, you may experience:

In addition, if you link two shared libraries–or a shared library and an executable– against the same static runtime, the final binary image of each shared library includes a copy of the runtime's code. Having multiple instances of runtime code is problematic because of duplication of certain global variables that the runtime uses or provides internally.

This problem does not apply to a project comprising a single shared library. For example, you can link against {@code stlport_static}, and expect your app to behave correctly. If your project requires several shared library modules, we recommend that you use the shared library variant of your C++ runtime.

Shared runtimes

If your app targets a version of Android earlier than Android 4.3 (Android API level 18), and you use the shared library variant of a given C++ runtime, you must load the shared library before any other library that depends on it.

For example, an app may have the following modules:

You must load the libraries in reverse dependency order:

    static {
      System.loadLibrary("stlport_shared");
      System.loadLibrary("bar");
      System.loadLibrary("foo");
    }

Note: Do not use the {@code lib} prefix when calling {@code System.loadLibrary()}.

Licensing

STLport is licensed under a BSD-style open-source license. See {@code $NDK/sources/cxx-stl/stlport/README} for more details about STLport.

GNU libstdc++ is covered by the GPLv3 license, and not the LGPLv2 or LGPLv3. For more information, see License on the GCC website.

LLVM {@code libc++} is dual-licensed under both the University of Illinois "BSD-Like" license and the MIT license.