android 交叉编译需要使用到NDK交叉编译工具链,首先需要下载ndk开发工具包,下载地址:中文下载地址,或者通过android studio自动下载ndk工具包,进入ndk工具包根目录,通过 make-standalone-toolchain.sh 工具生成一个独立的交叉编译工具链,或者使用ndk内自带的工具链。
hfcai@hfcai:~/Android/Sdk/ndk-bundle$
hfcai@hfcai:~/Android/Sdk/ndk-bundle/build/tools$ ll
总用量 224
drwxr-xr-x 3 hfcai hfcai 4096 11月 27 14:50 ./
drwxr-xr-x 7 hfcai hfcai 4096 10月 13 09:01 ../
-rw-r--r-- 1 hfcai hfcai 17639 8月 2 11:58 builder-funcs.sh
-rwxr-xr-x 1 hfcai hfcai 2086 8月 2 11:58 build-gdbserver.py*
-rwxrwxr-x 1 hfcai hfcai 7880 9月 19 09:09 build-gdbserver.sh*
-rwxr-xr-x 1 hfcai hfcai 3014 8月 2 11:58 build-gdb-stub.sh*
-rwxr-xr-x 1 hfcai hfcai 2020 8月 2 11:58 build-renderscript.py*
-rwxrwxr-x 1 hfcai hfcai 5604 9月 19 09:09 build-shader-tools.py*
-rw-rw-r-- 1 hfcai hfcai 26575 9月 19 09:09 common-build-host-funcs.sh
-rwxr-xr-x 1 hfcai hfcai 2131 8月 2 11:58 dev-cleanup.sh*
-rw-rw-r-- 1 hfcai hfcai 9029 9月 19 09:09 dev-defaults.sh
-rwxr-xr-x 1 hfcai hfcai 5547 8月 2 11:58 gen-toolchain-wrapper.sh*
-rwxrwxr-x 1 hfcai hfcai 20037 10月 13 09:01 make_standalone_toolchain.py*
-rwxr-xr-x 1 hfcai hfcai 4580 8月 2 11:58 make-standalone-toolchain.sh*
-rwxr-xr-x 1 hfcai hfcai 42 8月 2 11:58 ndk-ccache-gcc.sh*
-rwxr-xr-x 1 hfcai hfcai 43 8月 2 11:58 ndk-ccache-g++.sh*
-rw-r--r-- 1 hfcai hfcai 19411 8月 2 11:58 ndk-common.sh
-rw-rw-r-- 1 hfcai hfcai 41264 9月 19 09:09 prebuilt-common.sh
-rw-r--r-- 1 hfcai hfcai 8987 8月 2 11:58 pylintrc
drwxr-xr-x 2 hfcai hfcai 4096 8月 2 11:58 toolchain-licenses/
hfcai@hfcai:~/Android/Sdk/ndk-bundle/build/tools$ ./make-standalone-toolchain.sh --help
HOST_OS=linux
HOST_EXE=
HOST_ARCH=x86_64
HOST_TAG=linux-x86_64
HOST_NUM_CPUS=4
BUILD_NUM_CPUS=8
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-hfcai]
--install-dir=<path> Don't create package, install files to <path> instead.
--dryrun Unsupported.
--platform=<name> Specify target Android platform/API level. [android-14]
--force Remove existing install directory.
hfcai@hfcai:~/Android/Sdk/ndk-bundle/build/tools$ ./make-standalone-toolchain.sh --install-dir=/home/hfcai/toolchains
hfcai@hfcai:~/toolchains$ ll
总用量 104
drwxr-xr-x 12 hfcai hfcai 4096 11月 27 17:37 ./
drwxr-xr-x 47 hfcai hfcai 4096 11月 28 09:04 ../
-rw-r--r-- 1 hfcai hfcai 23 9月 19 09:09 AndroidVersion.txt
drwxr-xr-x 4 hfcai hfcai 4096 11月 27 17:24 arm-linux-androideabi/
drwxr-xr-x 2 hfcai hfcai 4096 11月 27 17:24 bin/
drwxr-xr-x 5 hfcai hfcai 4096 11月 27 17:24 include/
drwxr-xr-x 7 hfcai hfcai 4096 11月 27 17:24 lib/
drwxr-xr-x 4 hfcai hfcai 4096 11月 27 17:24 lib64/
drwxr-xr-x 2 hfcai hfcai 4096 11月 27 17:24 libexec/
-rw-r--r-- 1 hfcai hfcai 5804 9月 19 09:09 manifest_4751641.xml
-rw-r--r-- 1 hfcai hfcai 0 8月 2 11:58 MODULE_LICENSE_BSD_LIKE
-rw-r--r-- 1 hfcai hfcai 0 8月 2 11:58 MODULE_LICENSE_MIT
-rw-r--r-- 1 hfcai hfcai 32056 8月 2 11:58 NOTICE
drwxr-xr-x 3 hfcai hfcai 4096 11月 27 17:24 prebuilt_include/
-rw-r--r-- 1 hfcai hfcai 4445 10月 13 09:01 repo.prop
drwxr-xr-x 10 hfcai hfcai 4096 11月 27 17:24 share/
drwxr-xr-x 3 hfcai hfcai 4096 11月 27 17:24 sysroot/
drwxr-xr-x 5 hfcai hfcai 4096 11月 27 17:35 test/
-rw-r--r-- 1 hfcai hfcai 89 11月 27 17:37 test.c
如上使用–install-dir直接安装工具链到指定位置,否则会生成一个压缩包到指定位置,可通过 –packege-dir= 指定,默认为/tmp/ndk-username. 安装完成后可以通过:
hfcai@hfcai:~/toolchains/bin$ ./arm-linux-androideabi-gcc -v
Android (4751641 based on r328903) clang version 7.0.2 (https://android.googlesource.com/toolchain/clang 003100370607242ddd5815e4a043907ea9004281) (https://android.googlesource.com/toolchain/llvm 1d739ffb0366421d383e04ff80ec2ee591315116) (based on LLVM 7.0.2svn)
Target: armv7a-none-linux-android16
Thread model: posix
InstalledDir: /home/hfcai/toolchains/bin/.
Found candidate GCC installation: /home/hfcai/toolchains/bin/./../lib/gcc/arm-linux-androideabi/4.9.x
Selected GCC installation: /home/hfcai/toolchains/bin/./../lib/gcc/arm-linux-androideabi/4.9.x
Candidate multilib: thumb;@mthumb
Candidate multilib: armv7-a;@march=armv7-a
Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb
Candidate multilib: .;
Selected multilib: armv7-a;@march=armv7-a
查看版本,确认 arm-linux-androideabi-gcc 可用,然后通过一个简单的测试文件对其进行测试,最后确认正常,就可确保toolchains工具链正常使用.
// test.c
#include <stdio.h>
int main(){
printf("测试Android交叉编译!");
return 0;
}
hfcai@hfcai:~/toolchains$ bin/arm-linux-androideabi-gcc test.c -o test.o
如上成功生成test.o即可。
配置交叉编译环境变量
如上介绍,需要配置交叉编译所用到的几个编译工具为其添加环境变量,如下:
PREBUILT=/home/hfcai/toolchains //工具链根目录
SYSROOT=$PREBUILT/sysroot //工具链系统目录,用于后面连接头文件所用
export PATH=$PATH:${PREBUILT}/bin:${SYSROOT}/usr/include: //添加新的环境变量
export LDFLAGS="-L$SYSROOT/usr/lib -L$PREBUILT/arm-linux-androideabi/lib -march=armv7-a"
export CFLAGS="-I$SYSROOT/usr/include -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -ffast-math -O2"
export CPPFLAGS="$CFLAGS"
export CFLAGS="$CFLAGS"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="$LDFLAGS"
//如下为所需工具
export AS=${PREBUILT}/bin/arm-linux-androideabi-as
export LD=${PREBUILT}/bin/arm-linux-androideabi-ld
export CXX="$PREBUILT/bin/arm-linux-androideabi-g++ --sysroot=$SYSROOT"
export CC="$PREBUILT/bin/arm-linux-androideabi-gcc --sysroot=$SYSROOT -march=armv7-a "
export NM=${PREBUILT}/bin/arm-linux-androideabi-nm
export STRIP=${PREBUILT}/bin/arm-linux-androideabi-strip
export RANLIB=${PREBUILT}/bin/arm-linux-androideabi-ranlib
export AR=${PREBUILT}/bin/arm-linux-androideabi-ar
配置完成后就可通过命令完成交叉编译,首先需要完成工具本机的安装配置,然后对其进行交叉编译,如下:
//默认安装地址为/usr/local 为了防止用于安装地址导致的操作权限问题,建议通过 --prefix指定安装地址.
./configure --prefix=/home/hfcai/local
make clean
make
make install
#交叉编译,以lame为例.
./configure --host=arm-linux \
--disable-shared \
--disable-frontend \
--enable-static \
--prefix=/home/hfcai/Projects/github/Mp3Encoder/out/lame/armv7a
make clean
make -j8
make install
交叉编译常见编译选项
--help 显示此帮助信息|print this message
--log[=FILE|yes|no] 记录测试并输出到config.err文件|log tests and output to FILE [config.err]
--prefix=PREFIX 安装程序到指定目录(默认/usr/local)|install in PREFIX [/usr/local]
--libdir=DIR 安装库到指定目录(默认prefix/lib)|install libs in DIR [PREFIX/lib]
--shlibdir=DIR 指定共享库路径(默认prefix/lib)|install shared libs in DIR [PREFIX/lib]
--incdir=DIR 指定includes路径(默认prefix/include/ffmpeg)|install includes in DIR[PREFIX/include/ffmpeg]
--mandir=DIR 指定man page路径(默认prefix/man)install man page in DIR [PREFIX/man]
--enable-mp3lame 启用mp3编码libmp3lame(默认关闭)enable MP3 encoding via libmp3lame[default=no]
--enable-libogg 启用ogg支持libogg(默认关闭)enable Ogg support via libogg [default=no]
--enable-vorbis 启用Vorbis支持libvorbis(默认关闭)enable Vorbis support via libvorbis [default=no]
--enable-faad 启用faad支持libfaad(默认关闭)enable FAAD support via libfaad [default=no]
--enable-faadbin 启用faad运行时链接支持(默认关闭)build FAAD support with runtime linking[default=no]
--enable-faac 启用faac支持libfaac(默认关闭)enable FAAC support via libfaac [default=no]
--enable-libgsm 启用GSM支持libgsm(默认关闭)enable GSM support via libgsm [default=no]
--enable-xvid 启用xvid支持xvidcore(默认关闭)enable XviD support via xvidcore [default=no]
--enable-x264 启用H.264编码(默认关闭)enable H.264 encoding via x264 [default=no]
--enable-mingw32 启用MinGW本地/交叉win环境编译|enable MinGW native/cross Windows compile
--enable-mingwce 启用MinGW本地/交叉winCE环境编译enable MinGW native/cross WinCE compile
--enable-a52 启用A52支持(默认关闭)enable GPLed A52 support [default=no]
--enable-a52bin 启用运行时打开liba52.so.0(默认关闭)open liba52.so.0 at runtime [default=no]
--enable-dts 启用DTS支持(默认关闭)enable GPLed DTS support [default=no]
--enable-pp 启用后加工支持(默认关闭)enable GPLed postprocessing support [default=no]
--enable-static 构建静态库(默认启用)build static libraries [default=yes]
--disable-static 禁止构建静态库(默认关闭)do not build static libraries [default=no]
--enable-shared 构建共享库(默认关闭)build shared libraries [default=no]
--disable-shared 禁止构建共享库(默认启用)do not build shared libraries [default=yes]
--enable-amr_nb 启用amr_nb float音频编解码器|enable amr_nb float audio codec
--enable-amr_nb-fixed 启用fixed amr_nb codec | use fixed point for amr-nb codec
--enable-amr_wb 启用amr_wb float音频编解码器|enable amr_wb float audio codec
--enable-amr_if2 启用amr_wb IF2音频编解码器|enable amr_wb IF2 audio codec
--enable-sunmlib 启用Sun medialib(默认关闭) | use Sun medialib [default=no]
--enable-pthreads 启用pthreads(多线程)(默认关闭)use pthreads [default=no]
--enable-dc1394 启用libdc1394、libraw1394抓取IIDC-1394(默认关闭)enable IIDC-1394 grabbing using libdc1394 and libraw1394 [default=no]
--enable-swscaler 启用计数器支持?(默认关闭)software scaler support [default=no]
--enable-avisynth 允许读取AVISynth脚本本件(默认关闭)allow reading AVISynth script files [default=no]
--enable-gpl 允许使用GPL(默认关闭)allow use of GPL code, the resulting libav* and ffmpeg will be under GPL [default=no]
Advanced options (experts only): 高级选项参数(供专业人员使用)
--source-path=PATH 源码的路径(当前为/root/flv/ffmpeg)| path to source code [/root/flv/ffmpeg]
--cross-prefix=PREFIX 为编译工具指定路径 | use PREFIX for compilation tools []
--cross-compile 假定使用了交叉编译 | assume a cross-compiler is used
--cc=CC 指定使用何种C编译器(默认gcc)use C compiler CC [gcc]
--make=MAKE 使用特定的make | use specified make [make]
--extra-cflags=ECFLAGS 添加ECFLAGS到CFLAGS | add ECFLAGS to CFLAGS []
--extra-ldflags=ELDFLAGS 添加ELDFLAGS到LDFLAGS(默认-Wl,--as-needed)| add ELDFLAGS to LDFLAGS [ -Wl,--as-needed]
--extra-libs=ELIBS 添加ELIBS | add ELIBS []
--build-suffix=SUFFIX 为专用程序添加后缀 | suffix for application specific build []
--arch=ARCH 选择机器架构(默认x86)select architecture [x86]
--cpu=CPU 选用最低的cpu(影响指令的选择,可以在老CPU上出错) | selects the minimum cpu required (affects instruction selection, may crash on older CPUs)
--powerpc-perf-enable 启用PPC上面的性能报告(需要启用PMC)enable performance report on PPC
(requires enabling PMC)
--disable-mmx 禁用MMX | disable MMX usage
--disable-armv5te 禁用armv5te | disable armv5te usage
--disable-iwmmxt 禁用iwmmxt | disable iwmmxt usage
--disable-altivec 禁用AltiVec | disable AltiVec usage
--disable-audio-oss 禁用OSS音频支持(默认启用)disable OSS audio support [default=no]
--disable-audio-beos 禁用BeOS音频支持(默认启用)disable BeOS audio support [default=no]
--disable-v4l 禁用video4linux提取(默认启用)disable video4linux grabbing [default=no]
--disable-v4l2 禁用video4linux2提取(默认启用)disable video4linux2 grabbing [default=no]
--disable-bktr 禁用bktr视频提取(默认启用)disable bktr video grabbing [default=no]
--disable-dv1394 禁用DV1394提取(默认启用)disable DV1394 grabbing [default=no]
--disable-network 禁用网络支持(默认支持)disable network support [default=no]
--disable-ipv6 禁用ipv6支持(默认支持)disable ipv6 support [default=no]
--disable-zlib 禁用zlib(默认支持)disable zlib [default=no]
--disable-simple_idct 禁用simple IDCT例程(默认启用)disable simple IDCT routines [default=no]
--disable-vhook 禁用video hooking支持 | disable video hooking support
--enable-gprof enable profiling with gprof [no]
--disable-debug 禁用调试符号 | disable debugging symbols
--disable-opts 禁用编译器最优化 | disable compiler optimizations
--disable-mpegaudio-hp 启用更快的解码MPEG音频(但精确度较低)(默认禁用)faster (but less accurate) MPEG audio decoding [default=no]
--disable-protocols 禁用 I/O 协议支持(默认启用)disable I/O protocols support [default=no]
--disable-ffserver 禁用生成ffserver | disable ffserver build
--disable-ffplay 禁用生成ffplay | disable ffplay build
--enable-small 启用优化文件尺寸大小(牺牲速度)optimize for size instead of speed
--enable-memalign-hack 启用模拟内存排列,由内存调试器干涉? | emulate memalign, interferes with memory debuggers
--disable-strip 禁用剥离可执行程序和共享库 | disable stripping of executables and shared libraries
--disable-encoder=NAME 禁用XX编码器 | disables encoder NAME
--enable-encoder=NAME 启用XX编码器 | enables encoder NAME
--disable-decoder=NAME 禁用XX解码器 | disables decoder NAME
--enable-decoder=NAME 启用XX解码器 | enables decoder NAME
--disable-encoders 禁用所有编码器 | disables all encoders
--disable-decoders 禁用所有解码器 | disables all decoders
--disable-muxer=NAME 禁用XX混音器 | disables muxer NAME
--enable-muxer=NAME 启用XX混音器 | enables muxer NAME
--disable-muxers 禁用所有混音器 | disables all muxers
--disable-demuxer=NAME 禁用XX解轨器 | disables demuxer NAME
--enable-demuxer=NAME 启用XX解轨器 | enables demuxer NAME
--disable-demuxers 禁用所有解轨器 | disables all demuxers
--enable-parser=NAME 启用XX剖析器 | enables parser NAME
--disable-parser=NAME 禁用XX剖析器 | disables parser NAME
--disable-parsers 禁用所有剖析器 | disables all parsers