地址: http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html

It is now possible to use the toolchain provided with the Android NDK as a standalone compiler. This can be useful if you already have your own build system, and only need to ability to invoke the cross-compiler to add support to Android for it. A typical use case if invoking the 'configure' script of an open-source library that expects a cross-compiler in the CC environment variable. This document explains how to do that:

1/ Selecting your toolchain:

Before anything else, you need to decide whether your standalone toolchain is going to target ARM-based devices, x86-based, or MIPS-based one. Each architecture corresponds to a different toolchain name: * arm-linux-androideabi-4.4.3 => targetting ARM-based Android devices * x86-4.4.3 => targetting x86-based Android devices * mipsel-linux-android-4.4.3 => targetting MIPS-based Android devices

2/ Selecting your sysroot:

The second thing you need to know is which Android native API level you want to target. Each one of them provides a different various APIs, which are documented under doc/STABLE-APIS.html, and correspond to the sub-directories of $NDK/platforms. This allows you to define the path to your 'sysroot', a GCC term for a directory containing the system headers and libraries of your target. Usually, this will be something like: SYSROOT=$NDK/platforms/android-/arch-/ Where is the API level number, and is the architecture ("arm", "x86", and "mips" are the supported values). For example, if you're targeting Android 2.2 (a.k.a. Froyo), you would use: SYSROOT=$NDK/platforms/android-8/arch-arm IMPORTANT: Note that only android-9 is supported for the x86 architecture. Note that android-9 and later are supported for the MIPS architecture.

3/ Invoking the compiler (the hard way):

Invoke the compiler using the --sysroot option to indicate where the system files for the platform you're targeting are located. For example, do: export CC="$NDK/toolchains/<name>/prebuilt/<system>/bin/<prefix>gcc --sysroot=$SYSROOT" $CC -o foo.o -c foo.c Where <name> is the toolchain's name, <system> is the host tag for your system, and <prefix> is a toolchain-specific prefix. For example, if you are on Linux using the NDK r5 toolchain, you would use: export CC="$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc --sysroot=$SYSROOT" As you can see, this is rather verbose, but it works!

4/ Invoking the compiler (the easy way):

The NDK allows you to create a "customized" toolchain installation to make life easier. For example, consider the following command: $NDK/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-toolchain This will create a directory named /tmp/my-android-toolchain containing a copy of the android-5/arch-arm sysroot, and of the toolchain binaries. Note that by default, the ARM-based toolchain will be selected by the script. Use the '--arch=x86' option to specify the x86-based one, use the '--arch=mips' option to specify the MIPS-based one, or alternatively '--toolchain='. You can later use it directly with something like: export PATH=/tmp/my-android-toolchain/bin:$PATH export CC=arm-linux-androideabi-gcc Note that without the --install-dir option, make-standalone-toolchain.sh will create a tarball in /tmp/ndk/<toolchain-name>.tar.bz2. This allows you to archive and redistribute the binaries easily. Another important benefit is that this standalone toolchain will contain a working copy of the GNU libstdc++, with working exceptions and RTTI support (as long as you link against libstdc++ or libsupc++) Use --help for more options and details.

IMPORTANT: The toolchain binaries do not depend or contain host-specific paths, in other words, they can be installed in any location, or even moved if you need to.

NOTE: You can still use the --sysroot option with the new toolchain, but it is now simply optional!

5/ ABI Compatibility:

The machine code generated by the toolchain should be compatible with the official Android 'armeabi' ABI (see docs/CPU-ARCH-ABIS.html) by default. It is recommended to use the -mthumb compiler flag to force the generation of 16-bit Thumb-1 instructions (the default being 32-bit ARM ones). If you want to target the 'armeabi-v7a' ABI, you will need ensure that the following two flags are being used: CFLAGS='-march=armv7-a -mfloat-abi=softfp' Note: The first flag enables Thumb-2 instructions, and the second one enables H/W FPU instructions while ensuring that floating-point parameters are passed in core registers, which is critical for ABI compatibility. Do *not* use these flags separately! If you want to use Neon instructions, you will need one more compiler flag: CFLAGS='-march=armv7-a -mfloat-abi=softfp -mfpu=neon' Note that this forces the use of VFPv3-D32, as per the ARM specification. Also, is is *required* to use the following linker flags that routes around a CPU bug in some Cortex-A8 implementations: LDFLAGS='-Wl,--fix-cortex-a8' If none of the above makes sense to you, it's probably better not to use the standalone toolchain, and stick to the NDK build system instead, which will handle all the details for you. You don't have to use any specific compiler flag when targetting the x86 ABI or the MIPS ABI.

6/ Warnings and Limitations:

6.1/ Windows support:

The Windows binaries do *not* depend on Cygwin. The good news is that they are thus faster, the bad news is that they do not understand the Cygwin path specification like /cygdrive/c/foo/bar (instead of C:/foo/bar). The NDK build system ensures that all paths passed to the compiler from Cygwin are automatically translated, and deals with other horrors for you. If you have a custom build system, you may need to deal with the problem yourself.

NOTE: There is no plan to support Cygwin / MSys at the moment, but contributions are welcome. Contact the android-ndk forum for details.

6.2/ wchar_t support:

As documented, the Android platform did not really support wchar_t until Android 2.3. What this means in practical terms is that: - If you target platform android-9 or higher, the size of wchar_t is 4 bytes, and most wide-char functions are available in the C library (with the exception of multi-byte encoding/decoding functions and wsprintf/wsscanf). - If you target any prior API level, the size of wchar_t will be 1 byte and none of the wide-char functions will work anyway. We recommend any developer to get rid of any dependencies on the wchar_t type and switch to better representations. The support provided in Android is only there to help you migrate existing code.

6.3/ Exceptions, RTTI and STL:

The toolchain binaries *do* support C++ exceptions and RTTI by default. They are enabled by default, so use -fno-exceptions and -fno-rtti if you want to disable them when building sources with them (e.g. to generate smaller machine code).

NOTE: You will need to explicitly link with libsupc++ if you use these features. To do this, use -lsupc++ when linking binaries, as in: arm-linux-androideabi-g++ .... -lsupc++

6.4/ C++ STL support:

The standalone toolchain also comes with a copy of the GNU libstdc++ library, which provides an implementation of the C++ Standard Template Library. To use it, you however need to link with the proper library: * Use -lstdc++ to link against the _static_ library version. This ensures that all required C++ STL code is included into your final binary. This is ideal if you are only generating a single shared library or executable. This is the recommended way to do it. * Use -lgnustl_shared to link against the _shared_ library version. This is required if you have several related shared libraries or executables that need to run in the same address space at runtime (some global variables need to be defined uniquely, which is not possible if you link the static libstdc++ against each one of your executables). If you use this option, you need to ensure that libgnustl_shared.so is also copied to your device for your code to load properly. The file is at: $TOOLCHAIN/arm-linux-androideabi/lib/ for ARM toolchains. $TOOLCHAIN/i686-linux-android/lib/ for x86 ones. $TOOLCHAIN/mipsel-linux-android/lib/ for MIPS toolchains.

IMPORTANT: The GNU libstdc++ is licensed under the GPLv3 with a linking exception. See the following URL for details: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch01s02.html

使用ndk standalone工具链来编译某个平台下的库的更多相关文章

  1. 把NDK的工具链提取出来单独使用

    独立toolchain 把NDK压缩包解压到系统,如/mnt目录下,后在/mnt目录下建立文件夹my_ndk_toolchain,然后再/mnt目录下执行以下命令:/mnt/android-ndk-r ...

  2. FriendlyARM交叉工具链以及编译第一个arm9应用

    不记录什么都会忘光!!!这两天又要用到开发板来做项目,可是好久没有碰了,最近一直在搞上层的东东,对rails和前端感兴趣,我这是不要毕业的节奏了吗?好吧,既然什么都忘光掉了,那就干脆来个痛快,重新装机 ...

  3. 编译工具链,生成各个平台的ffmpeg版本的库

    1.在开始动手编译ffmpeg之前我们来梳理一下几个概念,gcc.g++.msvc.mingw.clang.cmake.make.qmake 作为一个windows软件工程师,以为长时间浸淫在各种强大 ...

  4. STM32中ARM系列编译工具链的编译宏选择(__CC_ARM、__ICCARM__、__GNUC__、__TASKING__)

    一 前言 stm32 f103中.关系到一个选择何种编译宏的问题.这里就梳理一下吧. 二 正文 1  在 core_cm3.h 文件中,有如下代码: #if defined ( __CC_ARM ) ...

  5. CODING DevOps 系列第一课:基于开源工具链打造持续交付平台

    当下软件发展趋势 当今 IT 行业发展中比较流行的几个技术,首先是微服务化,将原有的一个系统拆分成多个,意味着有多个系统需要构建.测试.部署和运维. 第二个是敏捷开发模式,需求粒度更细化,要求一个可独 ...

  6. FFmpeg编译:mac下编译iOS平台的FFmpeg库(支持armv7, arm64, i386, x86_64)

    环境:FFmpeg 3.4.6Xcode 10.3macOS 10.14.6iOS SDK 12.4 一.准备工作 1. 下载FFmpeg我这里使用的是3.4.6版本的FFmpeg,可以从FFmpeg ...

  7. xmake v2.3.7 发布, 新增 tinyc 和 emscripten 工具链支持

    xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...

  8. Ubuntu安装ARM架构GCC工具链(ubuntu install ARM toolchain)最简单办法

    一.安装ARM-Linux-GCC工具链 只需要一句命令: sudo apt-get install gcc-arm-linux-gnueabi 前提是你的Ubuntu系统版本是官网支持的最新的版本, ...

  9. 【转】Ubuntu安装ARM架构GCC工具链(ubuntu install ARM toolchain)最简单办法

    原文网址:http://www.cnblogs.com/muyun/p/3370996.html 一.安装ARM-Linux-GCC工具链 只需要一句命令: sudo apt-get install ...

随机推荐

  1. Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)

    MAX 查找组中表达式的最大值 MAX(COL ) OVER ( [ <partition_by_clause> ] < order_by_clause > )MIN 查找组中 ...

  2. Java OCR tesseract 图像智能字符识别技术 Java实现

    Java OCR tesseract 图像智能字符识别技术 Java代码实现 接着上一篇OCR所说的,上一篇给大家介绍了tesseract 在命令行的简单用法,当然了要继承到我们的程序中,还是需要代码 ...

  3. Centos 6.4上面用Shell脚本一键安装vsftpd

    Centos 6.4上面用Shell脚本一键安装vsftpd install.sh #!/bin/bash if [ `uname -m` == "x86_64" ];then m ...

  4. PHP漏洞全解(四)-xss跨站脚本攻击

    本文主要介绍针对PHP网站的xss跨站脚本攻击.跨站脚本攻击是通过在网页中加入恶意代码,当访问者浏览网页时恶意代码会被执行或者通过给管理员发信息 的方式诱使管理员浏览,从而获得管理员权限,控制整个网站 ...

  5. XSS脚本攻击漫谈

    XSS跨站脚本攻击一直都被认为是客户端  Web安全中最主流的攻击方式.因为  Web环境的复杂性以及 XSS跨站脚本攻击的多变性,使得该类型攻击很难彻底解决.那么,XSS跨站脚本攻击具体攻击行为是什 ...

  6. Layer 1: Single Objects

    Layer 1: 单一对象 粗略的说, 在javascript中所有对象都是maps的键值对. 键值对的实体在对象中称为属性( property).属性的key经常为 string类型,而他的valu ...

  7. MongoDB实战指南(三):MongoDB的锁机制

    与关系数据库一样,MongoDB也是通过锁机制来保证数据的完整性和一致性,MongoDB利用读写锁来支持并发操作,读锁可以共享写锁具有排他性.当一个读锁存在时,其他读操作也可以用这个读锁:但当一个写锁 ...

  8. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  9. vnc执行,报xauth could not run

    /usr/bin/gnome-terminal [INFO-10%]生成随机密码 [INFO-20%]生成密码文件 [INFO-30%]完成初始化,准备打开连接 Error: could not ru ...

  10. Bat 中特殊符号

    批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @\\隐藏命令的回显. ~\\在for中表示使用增强的变量扩展:在set中表示使用扩展环境变量指定位置的字符串:在set ...