【Android】NDK的使用常见问题
16lz
2021-12-04
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)
更多相关文章
- Android内核编译
- Ubuntu上编译Android过程
- android编译过程详解(二)
- 二维码的生成细节和原理
- 为Android加入busybox工具
- Android属性之build.prop生成过程分析
- Android编译过程详解(二)
- 为Android加入busybox工具
- android内核字符驱动设备实战之----------内置C语言测试程序篇