【Android】NDK的使用常见问题
16lz
2021-01-23
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 调试桥(adb)是多种用途的工具
- xamarin之 安装工具介绍
- AndroidUI自动化测试工具-UIautomator
- 10 个 Android开发人员必备的开发工具
- android测试工具大全
- Android 开发小工具之:Custom Tabs
- Android 实用工具Hierarchy Viewer实战
- Android内核开发:理解和掌握repo工具(含被墙后的下载方法)