Android提供了NDK工具用以编译能够Android设备上运行的C/C++程序,本质上就是交叉编译,下面给出几篇关于NDK相关的文章。

教程

生成设备专用的toolchain

${NDKROOT}/build/tools 下有两个脚本:make_standalone_toolchain.py 和 make-standalone-toolchain.sh,这两个脚本的功能相似,下面是脚本的使用方式(–help):

usage: make_standalone_toolchain.py [-h] --arch                                    {arm,arm64,mips,mips64,x86,x86_64}                                    [--api API]                                    [--stl {gnustl,libc++,stlport}]                                    [--unified-headers] [--force] [-v]                                    [--package-dir PACKAGE_DIR | --install-dir INSTALL_DIR]Creates a toolchain installation for a given Android target. The output ofthis tool is a more typical cross-compiling toolchain. It is indended to beused with existing build systems such as autotools.optional arguments:  -h, --help            show this help message and exit  --arch {arm,arm64,mips,mips64,x86,x86_64}  --api API             Target the given API version (example: "--api 24").  --stl {gnustl,libc++,stlport}                        C++ STL to use.  --unified-headers     Use unified headers.  --force               Remove existing installation directory if it exists.  -v, --verbose         Increase output verbosity.  --package-dir PACKAGE_DIR                        Build a tarball and install it to the given directory.                        If neither --package-dir nor --install-dir is                        specified, a tarball will be created and installed to                        the current directory.  --install-dir INSTALL_DIR                        Install toolchain to the given directory instead of                        packaging.
Usage: make-standalone-toolchain.sh [options] Generate a customized Android toolchain installation that includesa working sysroot. The result is something that can more easily beused as a standalone cross-compiler, e.g. to run configure andmake scripts.Valid options (defaults are in brackets):  --help                   Print this help.  --verbose                Enable verbose mode.  --dryrun                 Unsupported.  --toolchain=       Specify toolchain name  --use-llvm               No-op. Clang is always available.  --stl=             Specify C++ STL [gnustl]  --arch=            Specify target architecture  --abis=            No-op. Derived from --arch or --toolchain.  --ndk-dir=         Unsupported.  --package-dir=     Place package file in  [/tmp/ndk-zonkin]  --install-dir=     Don't create package, install files to  instead.  --dryrun                 Unsupported.  --platform=        Specify target Android platform/API level. [android-9]  --force                  Remove existing install directory.

例如,我的平台是麒麟970,他是arm64的架构,Android版本是6.0(API 23),将生成的toolchain保存到 /tmp/ndk ,那么我只需要执行如下命令

./make_standalone_toolchain.py --arch=arm64 --api=23 --install-dir=/tmp/ndk

然后只需要设置好环境变量,即可开始使用交叉编译工具链:

export PATH=/tmp/ndk/bin:$PATHexport CC=aarch64-linux-android-gccexport CXX=aarch64-linux-android-g++

常见问题

error: only position independent executables (PIE) are supported.

在编译时加入下面的选项

-pie -fPIE

参考:Android NDK 工具链的使用方法(Standalone Toolchain)

更多相关文章

  1. Android内核编译
  2. Ubuntu上编译Android过程
  3. android编译过程详解(二)
  4. 二维码的生成细节和原理
  5. 为Android加入busybox工具
  6. Android属性之build.prop生成过程分析
  7. Android编译过程详解(二)
  8. 为Android加入busybox工具
  9. android内核字符驱动设备实战之----------内置C语言测试程序篇

随机推荐

  1. Android总结篇系列:Android(安卓)Intent
  2. Android四大组建
  3. android:windowSoftInputMode属性的使用
  4. 在android framework中添加自定义系统服
  5. 修改Android(安卓)程序的icon快捷方式图
  6. Android多媒体播放器源码解析(stagefrigh
  7. Android(安卓)编辑框(EditText)属性学习
  8. Android之属性动画、值动画
  9. Android(安卓)MediaProvider数据库模式
  10. 横竖屏切换时候activity的生命周期,重要参