地址: 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. 如何编译各平台使用的库-以编译tolua为例

    转载注明出处: https://www.jianshu.com/p/5a35602adef8?appinstall=0 做U3D手机游戏,最热门的技术组合是c#+lua,使用lua是因为可以热更新,而 ...

  2. 乐鑫WIFI芯片开发流程梳理-工具链、编译和下载

    最近开始基于乐鑫ESP32的WIFI模组的软件开发,首先要做整套流程的梳理,乐鑫官网以及安信可上已经有了较为系统的资料,但是在实现过程中也遇到了一些问题,花了几天去解决,所以为了总结,也为了给其他人提 ...

  3. 交叉工具编译ARM平台Opus音频库

    最近项目中使用到opus音频库,这是一款开源的高效的音频库.其优点相对于AAC编码来说,编码效率高,据说相同带宽下,音频质量完爆AAC,而高带宽下音质接近原始音频(不喜勿喷^_^).详情及源码见官网h ...

  4. mysql windows编译_Windows平台下编译Mysql源码 | 学步园

    最近由于项目的关系,需要使用到Mysql数据库,而我的工作任务与数据库有很大的关系,所以,决定好好学学Mysql,同时,也把Mysql的源码下载了,希望能有利于对它的学习.这里记录一下windows平 ...

  5. linux下编译libuv,linux下libuv库安装教程

    下载并编译libuv libuv需要自己手动下载源码,并手动编译. 当前目录为:/home/xlz/test/github/,在后面,会用$PATH来代替,我的系统的Debian8,64bit. $g ...

  6. Linux C 编程开发环境(工具链,编译,汇编,链接,库)基础知识与实践

    前言 本博文包括对下面书籍的学习笔记,以及实际上机编程练习,程序运行分析等的总结,作为日后工作的参考: <UNIX 环境高级编程(第三版)> <深度探索 Linux 操作系统:系统构 ...

  7. AUTOSAR从入门到精通-【应用篇】基于Vector工具链车载总线自动化测试平台的研究与开发

    目录 车载总线发展现状 自动化测试平台的发展趋势 国内外研究现状

  8. linux 脚本自动编制工具,全自动工具链编译脚本

    GCC 工具链自动编译脚本 本工具用于自动编译指定的工具链,可以同时生成运行于本机系统 (Build system) 和 Windows 系统(可以修改)的两套工具链 具体编译规则指定于 .confi ...

  9. Linux 之八 完整嵌入式 Linux 环境、(交叉)编译工具链、CPU 体系架构、嵌入式系统构建工具

      最近,工作重心要从裸机开发转移到嵌入式 Linux 系统开发,由于之前对嵌入式 Linux 环境并不是很了解,因此,第一步就是需要了解如何搭建一个完整的嵌入式 Linux 环境.现在将学习心得记录 ...

最新文章

  1. C++编译器Qt Creator下载地址
  2. CNN+CTC语音识别
  3. 水平分库分表的关键步骤和技术难点,分库分表的几种常见玩法及如何解决跨库查询等问题...
  4. python--微信小程序获取手机号码报错
  5. 钢琴块2电脑版_云上钢琴学生端电脑版|云上钢琴学生端 V2.3.1 最新PC版 下载_当下软件园...
  6. sudo -u hdfs hdfs balancer出现异常 No lease on /system/balancer.id
  7. vue本地静态图片的路径问题解决方案
  8. 【Qt5】评标专家库随机选5人小软件
  9. 人体姿态识别OpenPose
  10. Vue中数字(金额)大小写实时转换
  11. 钉钉开放文档——JSAPI鉴权失败
  12. 高薪程序员面试题精讲系列74之你熟悉cookie、session吗?有哪些请求方式?请求转发与重定向有什么区别?
  13. CentOS 7下配置hadoop 2.8 分布式集群
  14. WIN7、WIN10下更换磁盘图标
  15. 1+X Web前端(初级)理论题考试样题及答案(建议收藏)
  16. 视频教程-系统集成项目管理工程师5天修炼-软考
  17. cesium 指南针、图例插件
  18. 7款减肥食谱 润肺排毒抗雾霾
  19. 诚之和:身边的虚拟人你留意过吗?它们已经开始闯入人类生活
  20. 5G+4G双模双卡助力5G专网监测

热门文章

  1. 2016和2017的区别就是昨晚和今早
  2. MVC框架详解--Servlet+JSP+JavaBean模式(MVC)开发复杂的web应用
  3. UVA - 11732 strcmp() Anyone?左兄弟右儿子trie
  4. C/C++中善用大括号
  5. 线程的属性 —— 分离的状态(detached state)、栈地址(stack address)、栈大小(stack size)
  6. Linux stmac网卡代码分析 -- open
  7. MFC+GDI+绘制出雷达余晖效果
  8. BigDecimal类型一定不会失真吗?不一定。参数类型为double的构造方法的结果有一定的不可预知性,是有可能产生失真的。
  9. java 新区 老区_优学院《土地资源学》答案在线查题2020高校邦《Java核心开发技术【实境编程】》章测试答案...
  10. 数据库设计基础:数据字典相关知识笔记