C++ Support

  The Android platform provides a very minimal C++ runtime support library (/system/lib/libstdc++) and corresponding headers for it in the NDK.

  By default, this 'system' runtime does not provide the following:

  • Standard C++ Library support (except a few trivial headers).
  • C++ exceptions support
  • RTTI support

  However, the NDK provides various "helper C++ runtimes" which can provide them, or a subset of these features.

  To select the runtime you want to use, define APP_STL inside your Application.mk to one of the following values:

    system          -> Use the default minimal system C++ runtime library.
gabi++_static -> Use the GAbi++ runtime as a static library.
gabi++_shared -> Use the GAbi++ runtime as a shared library.
stlport_static -> Use the STLport runtime as a static library.
stlport_shared -> Use the STLport runtime as a shared library.
gnustl_static -> Use the GNU STL as a static library.
gnustl_shared -> Use the GNU STL as a shared library.
c++_static -> Use the LLVM libc++ as a static library.
c++_shared -> Use the LLVM libc++ as a shared library.

  The 'system' runtime is the default if there is no APP_STL definition in your Application.mk. As an example, to use the static GNU STL, add a line like:

APP_STL := gnustl_static

  To your Application.mk. You can only select a single C++ runtime that all your code will depend on. It is not possible to mix shared libraries compiled against different C++ runtimes.

  IMPORTANT: Defining APP_STL in Android.mk has no effect!

  If you are not using the NDK build system, you can still use STLport, libc++ or GNU STL via "make-standalone-toolchain.sh --stl=". see Standalone Toolchainfor more details.

  The capabilities of the various runtimes vary. See this table:

                C++       C++    Standard
Exceptions RTTI Library system no no no
gabi++ yes yes no
stlport yes yes yes
gnustl yes yes yes
libc++ yes yes yes

2.Runtimes

2.1 System runtime

The system runtime only provides a very small number of C++ standard headers.

This corresponds to the actual C++ runtime provided by the Android platform. If you use it, your C++ binaries will automatically be linked against the system libstdc++.

The only headers provided here are the following:

cassert cctype cerrno cfloat climits cmath csetjmp csignal cstddef
cstdint cstdio cstdlib cstring ctime cwchar new stl_pair.h typeinfo
utility

Anything else is not supported, including std::string or std::vector.

2.2 GAbi++ runtime

  This is a new minimalistic runtime that provides the same headers than the system one, with the addition of RTTI (RunTime Type Information) and exception handling support.

If you insist on using it, read the "RTTI Support" and "Static runtime considerations" sections below.

2.3 STLport runtime

  This is a port of STLport (http://www.stlport.org) that can be used on Android. It will provide you with a complete set of C++ standard library headers, with RTTI and exception handling support.

  That's because the library embeds its own copy of GAbi++.

  Available as both both static and shared libraries. To use it, use either one of these two lines in your Application.mk:

  APP_STL := stlport_shared
APP_STL := stlport_static

  Note that 'stlport_shared' is preferred, for reasons explained in "Static runtime considerations". The shared library file is named libstlport_shared.so instead of "libstdc++.so" as on other platforms.

2.4 GNU STL runtime

  This is the GNU Standard C++ Library (a.k.a. libstdc++-v3), providing the more features. Note that the shared library file is named "libgnustl_shared.so".

  If you want to use it, please read the "C++ Exceptions support", "RTTI Support" and "Static runtime considerations" sections below.

2.5 libC++ runtime:

  This is a port of LLVM libc++: http://libcxx.llvm.org/. Note that the shared library file is named "libc++_shared.so".

  Please read the "C++ Exceptions support", "RTTI Support" and "Static runtime considerations" sections below.

3.Important Considerations

3.1 C++ Exceptions support

  The NDK toolchain supports C++ exceptions, since NDK r5, however all C++ sources are compiled with -fno-exceptions support by default, for compatibility reasons with previous releases.

  To enable it, use the new LOCAL_CPP_FEATURES variable in your Android.mk, as in:

LOCAL_CPP_FEATURES += exceptions

  See docs/ANDROID-MK.html for more details about this variable.

  Another way to do the same is to define it in your LOCAL_CPPFLAGS definition (but using LOCAL_CPP_FEATURES is preferred), as in:

LOCAL_CPPFLAGS += -fexceptions 

  More simply, add a single line to your Application.mk, the setting will automatically apply to all your project's NDK modules:

APP_CPPFLAGS += -fexceptions

  IMPORTANT: You will have to select a C++ runtime that supports exceptions to be able to link / run your code.

3.2 RTTI support

  Similarly, the NDK toolchain supports C++ RTTI (RunTime Type Information) since NDK r5, but all C++ sources are built with -fno-rtti by default for compatibility reasons. To enable it, add the following to your module declarations:

LOCAL_CPP_FEATURES += rtti

  This will be equivalent to:

LOCAL_CPPFLAGS += -frtti

  Or more simply to your Application.mk:

APP_CPPFLAGS += -frtti

3.3 Static runtimes

  Please keep in mind that the static library variant of a given C++ runtime SHALL ONLY BE LINKED INTO A SINGLE BINARY for optimal conditions.

  What this means is that if your project consists of a single shared library, you can link against, e.g., stlport_static, and everything will work correctly.

  On the other hand, if you have two shared libraries in your project (e.g. libfoo.so and libbar.so) which both link against the same static runtime, each one of them will include a copy of the runtime's code in its final binary image. This is problematic because certain global variables used/provided internally by the runtime are duplicated.

  This is likely to result in code that doesn't work correctly, for example:

  • memory allocated in one library, and freed in the other would leak or even corrupt the heap.
  • exceptions raised in libfoo.so cannot be caught in libbar.so (and may simply crash the program).
  • the buffering of std::cout not working properly

  This problem also happens if you want to link an executable and a shared library to the same static library.

  In other words, if your project requires several shared library modules, then use the shared library variant of your C++ runtime.

3.4 Shared runtimes

  Due to an issue which is fixed in JB-MR2 API level 18 (issue 41790 and 34416 ), if you use the shared library variant of a given C++ runtime, keep in mind that you must load it before any library that depends on it when your application starts.

  As an example, let's consider the case where we have the following modules

  • libfoo.so
  • libbar.so which is used by libfoo.so
  • libstlport_shared.so, used by both libfoo and libbar

  You will need to load the libraries in reverse dependency order, as in:

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

  Note that you shouldn't use the 'lib' prefix when calling System.loadLibrary(), unless you specify the full path as in:

System.loadLibrary("/path/to/libstlport_shared.so")

  Which is not recommended, since this hard-codes the path in your code.

4. EXTRAS

4.1 STLport-specific issues

  This NDK provides prebuilt static and shared libraries for STLport, but you can force it to be rebuilt from sources by defining the following in your environment or your Application.mk before building:

STLPORT_FORCE_REBUILD := true

  STLport is licensed under a BSD-style open-source license. See sources/cxx-stl/stlport/README for more details about the library.

4.2 GNU libstdc++ license is GPLv3 + linking exception

  Be aware that the GNU libstdc++ is covered by the GPLv3 license (and not the LGPLv2 or LGPLv3 as some assume), full details available here:

      http://gcc.gnu.org/onlinedocs/libstdc++/manual/license.html

  Be sure that you comply with all clauses of this license.

4.3 libc++-specific issues:

"-std=c++11" is turned on by default.

  Similiar to GNU libstdc++, you need to explicitly turns on exceptions or rtti support in "LOCAL_CPP_FEATURES" if you wish.

  It's likely that you need libatomic if you #include <atomic>. Add "LOCAL_LDLIBS += -latomic" for ndk-build, and "-latomic" for standalone toolchain. Note that -latomic is only available in gcc4.8, not gcc4.6 (deprecated and removed since r10e). Clang3.4/3.3 use gcc4.8's as/ld/headers/libraries so they get -latomic too. The version of libatomic in gcc4.8 may work for gcc4.6, although it's not tested and you have to copy them manually.

  This NDK provides prebuilt static and shared libraries for libc++ compiled by clang3.4, but you can force it to be rebuilt from sources by defining the following in your environment or your Application.mk before building:

LIBCXX_FORCE_REBUILD := true

  Around 99% of current 4640 tests passes when compiling libc++ with clang3.4 for all supported ABIs. The remaining fails are mostly in the areas of wchar_t and locale Android bionic don't support. Switching locale from the default produces the following warning in logcat

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

  Initial support at r9d uses gabi++ as run-time also causes some issues in the nested exception and propagation too. Almost all are fixed after switching to libc++abi.

  Header include/atomic is fixed for gcc which doesn't support Atomic in c++ mode. A dozen more fails when compiling libc++ with gcc4.8/gcc4.9 mostly in is_trivially* tests.

Using libc++ with gcc4.6 is not recommended (at least not tested at this moment) because its c++11 support isn't great

See "black_list*" in tests/device/test-libc++-shared-full/jni/Android.mk for tests which fail to compile, and tests/device/test-libc++-shared-full/BROKEN_RUN for tests which fails to run correctly.

NDK(17)让ndk支持完整C++,exception,rtti,的更多相关文章

  1. Android JNI和NDK学习(04)--NDK调试方法(转)

    本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3092812.html 本文主要介绍在ndk中添加log的方法.然后,我们就可 ...

  2. ndk学习5: ndk中使用c++

    默认情况下ndk不支持标准C++库,异常, rtti等   在ndk文档有关于C++ support的详细介绍   一. 使用C++标准库 介绍: 默认是使用最小额度的C++运行时库, 在Applic ...

  3. 利用office2000组件进行填充打印报不支持集合。 (Exception from HRESULT: 0x80020011 (DISP_E_NOTACOLLECTION))

    环境:win2008 64位+.net4.0 +office2000 错误提示: 不支持集合. (Exception from HRESULT: 0x80020011 (DISP_E_NOTACOLL ...

  4. NDK学习二: NDK目录结构

    NDK目录结构   NDK下载好之后目录结构如下:         目录名 描述 build   存放和编译相关的脚本文件,最外面的ndk-build就是调用该目录下的makefile文件,其中mak ...

  5. NDK学习笔记-NDK开发流程

    本文主要是说明一下在eclipse下如何对NDK进行配置 配置NDK 虽然现在基本上都使用Android Studio进行Android开发,但一些项目在eclipse中仍有运用,这里讲一讲eclip ...

  6. android studio ndk配置和ndk开发

    配置开发环境: 1:下载ndk,导入android studio中. 2:在项目中引入NDK   3:在计算机path变量中导入NDK路径,在编译.h文件的时候会用到. 一:建立java的native ...

  7. NDK(1)配置ndk,含eclipse,Android Studio1.5.1

    现在的ndk配置已经非常简单,如果看到要cygwin的请关闭. 1,Eclipse 添加ndk linux,mac ,windows 相似, a.下载 ndk并解压, b.在eclipse的andro ...

  8. 【推荐】CentOS安装Subversion-1.8.17+HTTP协议支持配置

    注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 我们需要搭建一个自己的SVN服务器. 此外,搭建好的SVN服务器除了需要支持svn协议外,最好还需要支持HTTP协议和HTTPS协 ...

  9. NDK开发和NDK双进程守护

    https://www.jianshu.com/p/433b2c93c6a7 NDK进程守护 https://blog.csdn.net/k393393/article/details/7895435 ...

随机推荐

  1. Unable to create the store directory. (Exception from HRESULT: 0x80131468)

    一个ASP.NET的程序,使用了MS ReportViewer报告控件,在用该控件导出生成Excel文件时,先是提示行不能超过65535. 这个是由于Excel2003的行限制的原因.由于修改成用Ex ...

  2. 【python】 入门 搭建环境

    1.去官网下载包 基本程序编译器 python-2.7.10.msi 集成开发环境 pycharm-community-4.5.2.exe 包管理工具 pip-7.0.3.tar.gz 2.安装 按顺 ...

  3. 基于FPGA的线阵CCD图像测量系统研究——笔记

    本文是对基于FPGA的线阵CCD图像测量系统研究(作者:高尚)的阅读笔记 第一章绪论 1. 读读看 读了前面的摘要依然没有看懂作者要做什么.接着往下读....终于看到了一个字眼“基于机器视觉的图像测量 ...

  4. [原]项目进阶 之 持续构建环境搭建(二)Nexus私服器

    上一篇博文项目进阶 之 持续构建环境搭建(一)架构中,我们大致讲解了一下本系列所搭建环境的基本框架,这次开始我们进入真正的环境搭建实战.重点不在于搭建的环境是否成功和完善,而是在搭建过程中充分认识到每 ...

  5. 面试问到:JDBC、hibernate、ibati

    一.JDBC.Connection(连接) 优点:运行高效.快捷. 缺点:代码多.异常多.不支持跨平台. 二.ibatis 1.根据jdbc的基本建立连接. 2.通过anntation+xml.jav ...

  6. res/drawable目录下图片的Uri

    http://liuyun025.iteye.com/blog/1280838 有时候,我们要用到res/drawable目录下的图片Uri,而这个Uri该如何生存呢?下面就是这Uri的生成方法: U ...

  7. IOS 后台运行

    默认情况下,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beginBackgroundTaskWithExpirat ...

  8. DispatcherServlet--Spring的前置控制器作用简介

    参考网址:http://jinnianshilongnian.iteye.com/blog/1602617 Struts有一个ActionServlet,用来完成前置控制器(分发器)的功能.其实,所有 ...

  9. CentOS安装RockMongo

    rockmongo官网下载页面在这里: http://rockmongo.com/downloads 找到最新版本的下载链接,一般第一个就是: 右键复制url,比如说是这个: http://rockm ...

  10. Ajax出入江湖

    window.onload = initAll; var xhr = false; function initAll() { if (window.XMLHttpRequest) { xhr = ne ...