在make.sh中给cmake命令添加编译参数:
#!/bin/bash
cd build
rm -rf *
export ANDROID_NDK=~/Android/Sdk/ndk/21.4.7075529
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="x86" \
-DANDROID_NDK=$ANDROID_NDK \
-DANDROID_PLATFORM=android-23 \
..
make
编译参数的解释参考文章https://blog.csdn.net/qq_38410730/article/details/103622813
然后获取模拟器的cpu架构:
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb shell getprop ro.product.cpu.abi
x86
然后将可执行文件main导入到手机,看下能不能执行
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb push /home/sunshuo/Desktop/ndk/build/main /data/data/com.example.ndk_2_3_2
/home/sunshuo/Desktop/ndk/build/main: 1 file pushed. 4.2 MB/s (7972 bytes in 0.002s)
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb shell
generic_x86:/ # cd /data/data/com.example.ndk_2_3_2
generic_x86:/data/data/com.example.ndk_2_3_2 # ls -l
total 36
drwxrws--x 2 u0_a149 u0_a149_cache 4096 2023-12-25 17:39 cache
drwxrws--x 2 u0_a149 u0_a149_cache 4096 2023-12-25 17:39 code_cache
lrwxrwxrwx 1 root root 66 2023-12-25 17:39 lib -> /data/app/com.example.ndk_2_3_2-S_248Q3VBMZInCbG1iiF7w==/lib/x86
-rwxrwxrwx 1 root root 7972 2024-02-18 20:18 main
generic_x86:/data/data/com.example.ndk_2_3_2 # ./main
PI:3.140000
hello world:6
debug:1
可以看到执行成功
那么这个可执行程序在x86架构的手机上可以运行,可以在其他架构的手机上运行吗?
下面新增一个armeabi-v7a的模拟器,重复上面的步骤,发现不能运行。
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb root
adbd is already running as root
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb shell getprop ro.product.cpu.abi
armeabi-v7a
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb push /home/sunshuo/Desktop/ndk/build/main /data/data/com.example.ndk_2_3_2
/home/sunshuo/Desktop/ndk/build/main: 1 file pushed. 0.6 MB/s (8040 bytes in 0.012s)
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb shell
generic:/ # cd /data/data/com.example.ndk_2_3_2
generic:/data/data/com.example.ndk_2_3_2 # ls -l
total 24
drwxrwx--x 2 u0_a79 u0_a79 4096 2024-02-18 21:49 cache
lrwxrwxrwx 1 root root 41 2024-02-18 21:49 lib -> /data/app/com.example.ndk_2_3_2-1/lib/arm
-rwxrwxrwx 1 root root 8040 2024-02-18 21:18 main
generic:/data/data/com.example.ndk_2_3_2 # ./main
/system/bin/sh: ./main: not executable: 32-bit ELF file
1|generic:/data/data/com.example.ndk_2_3_2 #
修改下cmake编译选项,改为armeabi-v7a,重新打包main,导进来试试
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb push /home/sunshuo/Desktop/ndk/build/main /data/data/com.example.ndk_2_3_2
/home/sunshuo/Desktop/ndk/build/main: 1 file pushed. 10.5 MB/s (81556 bytes in 0.007s)
sunshuo@sunshuo-virtual-machine:~/AndroidStudioProjects/NDK_2_3_22$ adb shell
generic:/ # cd /data/data/com.example.ndk_2_3_2
generic:/data/data/com.example.ndk_2_3_2 # ls -l
total 168
drwxrwx--x 2 u0_a79 u0_a79 4096 2024-02-18 21:49 cache
lrwxrwxrwx 1 root root 41 2024-02-18 21:49 lib -> /data/app/com.example.ndk_2_3_2-1/lib/arm
-rwxrwxrwx 1 root root 81556 2024-02-18 22:00 main
generic:/data/data/com.example.ndk_2_3_2 # ./main
PI:3.140000
hello world:6
debug:1
可以看到,成功运行,说明不同cup架构只能运行对应指令集的可执行程序或者加载库文件。