1.為啥需要交叉編譯楷掉?
因?yàn)閘inux 系統(tǒng)上的gcc 在android 系統(tǒng)上沒法正常調(diào)用驼卖,所有需要使用android ndk 中的 gcc
2
2.1 android ndk 使用ndk 17,
2.2 在Linux系統(tǒng)中:
wget https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip?hl=zh_cn
mv xxxx.zip?hl==zh_cn xxx.zip
# 使用mv 將上面的文件修改為 .zip 文件可免,然后 進(jìn)行解壓
unzip xxxx.zip
在linux 系統(tǒng)中 編寫一個(gè)簡單輸出 hello world
[root@sue download]# cat hello.c
#include <stdio.h>
int main(){
printf("Hello world\n");
}
[root@sue download]#
[root@sue download]# gcc hello.c -o mainEXE
[root@sue download]# ./mainEXE
Hello world
[root@sue download]#
雖然上面的 mainEXE 在liux系統(tǒng)上可以運(yùn)行耕漱,但是沒法在 android 系統(tǒng)上運(yùn)行
因?yàn)槲业氖謾C(jī)系統(tǒng)是arm64位的系統(tǒng),所以選擇aarch64-linux-android-4.9
查看手機(jī)平臺的架構(gòu)的方式
adb shell
1|HWBMH:/proc $ cat cpuinfo
Processor : AArch64 Processor rev 0 (aarch64)
processor : 0
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x41
CPU architecture: 8
[root@sue bin]# pwd
/opt/download/android-ndk-r17c/toolchains/aarch64-linux-android4.9/prebuilt/linux-x86_64/bin
使用上面的目錄下的
aarch64-linux-android-gcc
[root@sue bin]#
export NDK_GCC="/opt/download/android-ndk-r17c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc "
$NDK_GCC hello.c -o mainNDKEXE
[root@sue download]# export NDK_GCC="/opt/download/android-ndk-r17c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc"
[root@sue download]# $NDK_GCC hello.c -o mainNDKEXE
hello.c:1:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
[root@sue download]#
尋找不到頭文件和庫文件
那就在platforms 中找
/opt/download/android-ndk-r17c/platforms
# --sysroot=路徑尽爆, (自動(dòng)尋找頭文件和庫文件怎顾,.h/ .a/.so)
exprot AAA="--sysroot=/opt/download/android-ndk-r17c/platforms/android-21/arch-arm64";
但是上面是頭文件有了,庫文件還沒有漱贱,于是還需要找?guī)煳募?
/opt/download/android-ndk-r17c/sysroot/usr/include/
exprot AAA="--sysroot=/opt/download/android-ndk-r17c/platforms/android-21/arch-arm64 -isystem /opt/download/android-ndk-r17c/sysroot/usr/include";
[root@sue download]# echo $NDK_GCC
/opt/download/android-ndk-r17c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc
[root@sue download]# echo $AAA
--sysroot=/opt/download/android-ndk-r17c/platforms/android-21/arch-arm64 -isystem /opt/download/android-ndk-r17c/sysroot/usr/include
[root@sue download]#
[root@sue download]# $NDK_GCC $AAA hello.c -o mainNDKEXE
In file included from /opt/download/android-ndk-r17c/sysroot/usr/include/sys/types.h:36:0,
from /opt/download/android-ndk-r17c/sysroot/usr/include/stdio.h:42,
from hello.c:1:
/opt/download/android-ndk-r17c/sysroot/usr/include/linux/types.h:21:23: fatal error: asm/types.h: No such file or directory
#include <asm/types.h>
^
compilation terminated.
看來還需要配置 asm/types.h
/opt/download/android-ndk-r17c/sysroot/usr/include/aarch64-linux-android
于是乎:
export AAA="--sysroot=/opt/download/android-ndk-r17c/platforms/android-21/arch-arm64 -isystem /opt/download/android-ndk-r17c/sysroot/usr/include -isystem /opt/download/android-ndk-r17c/sysroot/usr/include/aarch64-linux-android ";
[root@sue download]# echo $NDK_GCC
[root@sue download]# export NDK_GCC="/opt/download/android-ndk-r17c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc"
[root@sue download]# $NDK_GCC $AAA hello.c -o mainNDKEXE
[root@sue download]#
[root@sue download]# ls -la
上面還有問題槐雾,
需要增加-pie
[root@sue download]# $NDK_GCC -pie $AAA hello.c -o mainNDKEXE
完畢。
分析一下:
export NDK_GCC="/opt/download/android-ndk-r17c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc" //需要gcc
export AAA="--sysroot=/opt/download/android-ndk-r17c/platforms/android-21/arch-arm64 【尋找文件頭文件】
-isystem /opt/download/android-ndk-r17c/sysroot/usr/include【尋找文件頭文件】
-isystem /opt/download/android-ndk-r17c/sysroot/usr/include/aarch64-linux-android ";【運(yùn)行期 尋找asm】
# --sysroot=路徑幅狮, (自動(dòng)尋找頭文件和庫文件募强,.h/ .a/.so)
# -isystem 路徑, 自動(dòng)尋找頭文件彪笼。