android ndk build

android_ndk_build

Developer NDK

https://developer.android.com/ndk/

Building Your Project

NDK推荐两种编译方式:

  • ndk-build
  • CMake

对于一些传统的C/C++工程,如libpng、libzmq、bzip2等。由于它们不是专门针对Android平台的,大多使用autoconf或者make方式来编译。
如果要把这类软件包移植到Android设备上运行,则需按照https://developer.android.com/ndk/guides/other_build_systems里描述的方式,使用r19 NDK对其进行交叉编译。

参考:

  • Building Your Project
  • Using the NDK with other build systems
  • Build System Maintainers Guide
  • Build System Maintainers Guide - NDK r19

注意:

  • 只有使用NDK工具链编译出的二进制bin或so,才能在Android上正确运行;
  • 使用Linaro工具链动态编译出的bin或so,在Android设备上无法运行,会报No such file or directory的错误;但Linaro静态编译出的bin,能够运行;

这是由于libc库不一致导致的,Android采用bionic库,而Linaro使用glibc库,故动态链接的bin跑不起来;但静态链接的bin,由于运行时不再依赖Android设备上的libc库,只要其ABI指令集与实际设备兼容就能跑起来。

Hello World 实例

首先,hello.c源码如下:

#include 

int main()
{
    printf("hi, i'm on AArch64\n");
    return 0;
}

然后, Makefile如下:

NDK=/opt/android-ndk-linux/android-ndk-r19
HOST_TAG=linux-x86_64

TOOLCHAIN=$(NDK)/toolchains/llvm/prebuilt/$(HOST_TAG)

CC=$(TOOLCHAIN)/bin/aarch64-linux-android27-clang
AR=$(TOOLCHAIN)/bin/aarch64-linux-android-ar
RANLIB=$(TOOLCHAIN)/bin/aarch64-linux-android-ranlib

CFLAGS := -v -Wall
#CFLAGS += -static

#
#DEBUG=-D_MACRO
#
#SOURCE=$(wildcard *.c)
#TARGET=$(patsubst %.c, %, $(SOURCE))
##OBJS=$(SOURCE:.c=.o)
#OBJS=$(patsubst %.c, %.o, $(SOURCE))
#
#all:$(TARGET)
#
##$(TARGET):$(OBJS)
##	$(CC) $(LDFLAGS) -o $@ $^
##.c.o:
##	$(CC) -c $(CFLAGS) $< -o $@
#
#$(TARGET):%:%.c
#	$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@
#
SOURCE= \
    hello.c

TARGET=hello

all:$(TARGET)

$(TARGET):$(SOURCE)
	$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@

.PHONY:clean all
clean:
	rm -fr $(TARGET) $(OBJS)

接着,make编译过程:

$ make
/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android27-clang hello.c -v -Wall  -o hello
Android (5058415 based on r339409) clang version 8.0.2 (https://android.googlesource.com/toolchain/clang 40173bab62ec746213857d083c0e8b0abb568790) (https://android.googlesource.com/toolchain/llvm 7a6618d69e7e8111e1d49dc9e7813767c5ca756a) (based on LLVM 8.0.2svn)
Target: aarch64-unknown-linux-android27
Thread model: posix
InstalledDir: /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x
Selected GCC installation: /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" -cc1 -triple aarch64-unknown-linux-android27 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu generic -target-feature +neon -target-abi aapcs -mllvm -aarch64-fix-cortex-a53-835769=1 -fallow-half-arguments-and-returns -dwarf-column-info -debugger-tuning=gdb -v -resource-dir /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.2 -internal-isystem /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/local/include -internal-isystem /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.2/include -internal-externc-isystem /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/aarch64-linux-android -internal-externc-isystem /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/include -internal-externc-isystem /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include -Wall -fdebug-compilation-dir /home/yuanyilong/me/zmq/zmqtest/ARMv8A -ferror-limit 19 -fmessage-length 212 -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/hello-58454c.o -x c hello.c
clang -cc1 version 8.0.2 based upon LLVM 8.0.2svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/local/include
 /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.2/include
 /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/aarch64-linux-android
 /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include
End of search list.
 "/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld" -pie --fix-cortex-a53-843419 -z relro --enable-new-dtags --eh-frame-hdr -m aarch64linux -dynamic-linker /system/bin/linker64 -o hello /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/27/crtbegin_dynamic.o -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/8.0.2/lib/linux/aarch64 -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib/../lib64 -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/27 -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib -L/opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib /tmp/hello-58454c.o -lgcc -ldl -lc -lgcc -ldl /opt/android-ndk-linux/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/27/crtend_android.o

最后,编译后的bin

$ file hello
hello: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, not stripped

把编译出的hello文件push到Android设备里,是能够跑起来的。

对比 Linaro编译

对上面的hello实例,源码不变,修改Makefile,使用Linaro AArch64工具链交叉编译试试

修改Makefile如下:

CROSS=aarch64-linux-gnu-
CC=$(CROSS)gcc
CFLAGS := -v -Wall
#CFLAGS += -static

#NDK=/opt/android-ndk-linux/android-ndk-r19
#HOST_TAG=linux-x86_64
#
#TOOLCHAIN=$(NDK)/toolchains/llvm/prebuilt/$(HOST_TAG)
#
#CC=$(TOOLCHAIN)/bin/aarch64-linux-android27-clang
#AR=$(TOOLCHAIN)/bin/aarch64-linux-android-ar
#RANLIB=$(TOOLCHAIN)/bin/aarch64-linux-android-ranlib

#
#DEBUG=-D_MACRO
#
#SOURCE=$(wildcard *.c)
#TARGET=$(patsubst %.c, %, $(SOURCE))
##OBJS=$(SOURCE:.c=.o)
#OBJS=$(patsubst %.c, %.o, $(SOURCE))
#
#all:$(TARGET)
#
##$(TARGET):$(OBJS)
##	$(CC) $(LDFLAGS) -o $@ $^
##.c.o:
##	$(CC) -c $(CFLAGS) $< -o $@
#
#$(TARGET):%:%.c
#	$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@
#

SOURCE= \
    hello.c

TARGET=hello

all:$(TARGET)

$(TARGET):$(SOURCE)
	$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@

.PHONY:clean all
clean:
	rm -fr $(TARGET) $(OBJS)

然后, make编译过程:

$ make
aarch64-linux-gnu-gcc hello.c -v -Wall  -o hello
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/lto-wrapper
Target: aarch64-linux-gnu
Configured with: '/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/snapshots/gcc.git~linaro-6.3-2017.05/configure' SHELL=/bin/bash --with-mpc=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-mpfr=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gmp=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gnu-as --with-gnu-ld --disable-libmudflap --enable-lto --enable-shared --without-included-gettext --enable-nls --disable-sjlj-exceptions --enable-gnu-unique-object --enable-linker-build-id --disable-libstdcxx-pch --enable-c99 --enable-clocale=gnu --enable-libstdcxx-debug --enable-long-long --with-cloog=no --with-ppl=no --with-isl=no --disable-multilib --enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419 --with-arch=armv8-a --enable-threads=posix --enable-multiarch --enable-libstdcxx-time=yes --enable-gnu-indirect-function --with-build-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/sysroots/aarch64-linux-gnu --with-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu/aarch64-linux-gnu/libc --enable-checking=release --disable-bootstrap --enable-languages=c,c++,fortran,lto --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=aarch64-linux-gnu --prefix=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu
Thread model: posix
gcc version 6.3.1 20170404 (Linaro GCC 6.3-2017.05) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/cc1 -quiet -v -imultiarch aarch64-linux-gnu -iprefix /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/ -isysroot /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc hello.c -quiet -dumpbase hello.c -march=armv8-a -mlittle-endian -mabi=lp64 -auxbase hello -Wall -version -o /tmp/cctLy9Z4.s
GNU C11 (Linaro GCC 6.3-2017.05) version 6.3.1 20170404 (aarch64-linux-gnu)
	compiled by GNU C version 4.8.4, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/../../lib/gcc/aarch64-linux-gnu/6.3.1/include"
ignoring nonexistent directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/local/include/aarch64-linux-gnu"
ignoring nonexistent directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/local/include"
ignoring duplicate directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/../../lib/gcc/aarch64-linux-gnu/6.3.1/include-fixed"
ignoring duplicate directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/../../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/include"
ignoring nonexistent directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/include/aarch64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/include
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/include-fixed
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/include
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/include
End of search list.
GNU C11 (Linaro GCC 6.3-2017.05) version 6.3.1 20170404 (aarch64-linux-gnu)
	compiled by GNU C version 4.8.4, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: c4df130d98804e26920c632548e1fa40
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/bin/as -v -EL -march=armv8-a -mabi=lp64 -o /tmp/ccY1cmUL.o /tmp/cctLy9Z4.s
GNU assembler version 2.27.0 (aarch64-linux-gnu) using BFD version (Linaro_Binutils-2017.05) 2.27.0.20161019
COMPILER_PATH=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/bin/
LIBRARY_PATH=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib/../lib64/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/lib/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/collect2 -plugin /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/liblto_plugin.so -plugin-opt=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/lto-wrapper -plugin-opt=-fresolution=/tmp/ccBphoQs.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc --build-id --eh-frame-hdr -dynamic-linker /lib/ld-linux-aarch64.so.1 -X -EL -maarch64linux --fix-cortex-a53-835769 --fix-cortex-a53-843419 -o hello /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/crt1.o /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/crti.o /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/crtbegin.o -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1 -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib/../lib64 -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/lib -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib /tmp/ccY1cmUL.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/crtend.o /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/crtn.o
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'

最后,编译后的bin:

$ file hello
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=335f332156479534f699cbb81279f3aa9a5ab201, not stripped

这么编译出的hello,在Androd设备上跑不起来:

# hello
/system/bin/sh: /system/bin/hello: No such file or directory

既然动态链接不行,那改为静态链接试一下,即把Makefile里面加上CFLAGS += -static,再去make:

$ make
aarch64-linux-gnu-gcc hello.c -v -Wall -static  -o hello
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/lto-wrapper
Target: aarch64-linux-gnu
Configured with: '/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/snapshots/gcc.git~linaro-6.3-2017.05/configure' SHELL=/bin/bash --with-mpc=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-mpfr=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gmp=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gnu-as --with-gnu-ld --disable-libmudflap --enable-lto --enable-shared --without-included-gettext --enable-nls --disable-sjlj-exceptions --enable-gnu-unique-object --enable-linker-build-id --disable-libstdcxx-pch --enable-c99 --enable-clocale=gnu --enable-libstdcxx-debug --enable-long-long --with-cloog=no --with-ppl=no --with-isl=no --disable-multilib --enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419 --with-arch=armv8-a --enable-threads=posix --enable-multiarch --enable-libstdcxx-time=yes --enable-gnu-indirect-function --with-build-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/sysroots/aarch64-linux-gnu --with-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu/aarch64-linux-gnu/libc --enable-checking=release --disable-bootstrap --enable-languages=c,c++,fortran,lto --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=aarch64-linux-gnu --prefix=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/aarch64-linux-gnu/_build/builds/destdir/x86_64-unknown-linux-gnu
Thread model: posix
gcc version 6.3.1 20170404 (Linaro GCC 6.3-2017.05) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-static' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/cc1 -quiet -v -imultiarch aarch64-linux-gnu -iprefix /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/ -isysroot /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc hello.c -quiet -dumpbase hello.c -march=armv8-a -mlittle-endian -mabi=lp64 -auxbase hello -Wall -version -o /tmp/ccGwY4bk.s
GNU C11 (Linaro GCC 6.3-2017.05) version 6.3.1 20170404 (aarch64-linux-gnu)
	compiled by GNU C version 4.8.4, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/../../lib/gcc/aarch64-linux-gnu/6.3.1/include"
ignoring nonexistent directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/local/include/aarch64-linux-gnu"
ignoring nonexistent directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/local/include"
ignoring duplicate directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/../../lib/gcc/aarch64-linux-gnu/6.3.1/include-fixed"
ignoring duplicate directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/../../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/include"
ignoring nonexistent directory "/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/include/aarch64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/include
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/include-fixed
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/include
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/include
End of search list.
GNU C11 (Linaro GCC 6.3-2017.05) version 6.3.1 20170404 (aarch64-linux-gnu)
	compiled by GNU C version 4.8.4, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: c4df130d98804e26920c632548e1fa40
COLLECT_GCC_OPTIONS='-v' '-Wall' '-static' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/bin/as -v -EL -march=armv8-a -mabi=lp64 -o /tmp/ccXmeYIy.o /tmp/ccGwY4bk.s
GNU assembler version 2.27.0 (aarch64-linux-gnu) using BFD version (Linaro_Binutils-2017.05) 2.27.0.20161019
COMPILER_PATH=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/bin/
LIBRARY_PATH=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib/../lib64/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/lib/:/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-static' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'
 /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/collect2 -plugin /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/liblto_plugin.so -plugin-opt=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../libexec/gcc/aarch64-linux-gnu/6.3.1/lto-wrapper -plugin-opt=-fresolution=/tmp/cc6stWgN.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lc --sysroot=/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc --build-id -Bstatic -X -EL -maarch64linux --fix-cortex-a53-835769 --fix-cortex-a53-843419 -o hello /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/crt1.o /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/crti.o /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/crtbeginT.o -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1 -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib/../lib64 -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/../../../../aarch64-linux-gnu/lib -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/lib -L/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib /tmp/ccXmeYIy.o --start-group -lgcc -lgcc_eh -lc --end-group /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/6.3.1/crtend.o /opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/../aarch64-linux-gnu/libc/usr/lib/crtn.o
COLLECT_GCC_OPTIONS='-v' '-Wall' '-static' '-o' 'hello' '-march=armv8-a' '-mlittle-endian' '-mabi=lp64'

静态链接的hello:

$ file hello
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, BuildID[sha1]=7ceb2387f1892b27918d21310e79bdf475adbb02, not stripped

adb push这个hello到Android设备里,能够跑得起来:

# hello
hi, i'm on AArch64

查看依赖库

  • readelf方式:
#!/bin/sh
aarch64-linux-android-readelf -a $1 | grep "Shared library"

注:aarch64-linux-android-readelfaarch64-linux-gnu-readelf均可;

  • objdump方式:
#!/bin/sh  
aarch64-linux-android-objdump -x $1 | grep NEEDED
  • ldd方式:

Ubuntu自带的ldd命令只能识别x86平台的二进制文件,不识别交叉编译的二进制文件,github有一个从crosstool-ng分离出来的ldd,请参考:
https://gist.github.com/jerome-pouiller/c403786c1394f53f44a3b61214489e6f

注:Crosstool-NG is a versatile (cross) toolchain generator. It supports many architectures and components and has a simple yet powerful menuconfig-style interface.

Android Studio

如何在Android Studio里使用NDK

Add C and C++ code to Your project

参考链接

  • NDK HOME
  • NDK DOWNLOADS
  • NDK Samples
  • CPUs and Architectures
  • Neural Networks API
  • Vulkan Graphics API
  • Building Your Project
  • Using the NDK with other build systems
  • Build System Maintainers Guide
  • Add C and C++ code to Your project

你可能感兴趣的:(Android)