您的位置:首页 > 移动开发 > Android开发

【Android】NDK的使用常见问题

2017-12-27 13:22 211 查看
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 of
this tool is a more typical cross-compiling toolchain. It is indended to be
used 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 includes
a working sysroot. The result is something that can more easily be
used as a standalone cross-compiler, e.g. to run configure and
make scripts.

Valid options (defaults are in brackets):

--help                   Print this help.
--verbose                Enable verbose mode.
--dryrun                 Unsupported.
--toolchain=<name>       Specify toolchain name
--use-llvm               No-op. Clang is always available.
--stl=<name>             Specify C++ STL [gnustl]
--arch=<name>            Specify target architecture
--abis=<list>            No-op. Derived from --arch or --toolchain.
--ndk-dir=<path>         Unsupported.
--package-dir=<path>     Place package file in <path> [/tmp/ndk-zonkin]
--install-dir=<path>     Don't create package, install files to <path> instead.
--dryrun                 Unsupported.
--platform=<name>        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:$PATH
export CC=aarch64-linux-android-gcc
export CXX=aarch64-linux-android-g++


常见问题

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

在编译时加入下面的选项

-pie -fPIE


参考:Android NDK 工具链的使用方法(Standalone Toolchain)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息