我正在尝试将可执行文件从Linux计算机(Ubuntu)复制到Android应用作为资产。我已成功地将可执行文件移至Android文件系统,但是当我尝试执行时,出现错误'sh: not executable: 64-bit ELF file.'
,我在可执行文件上运行了file命令,这是输出:
'ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so, for GNU/Linux 2.6.32 not stripped.'
当我在可在Android设备上运行的可执行文件上运行文件时:
'ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, stripped.'
为了添加一点上下文,我正在尝试编译this程序。
我下载了Android NDK,然后运行以下命令来编译程序:
sudo ./configure --cc=/home/username/arm/bin/arm-linux-androideabi-gcc
sudo make
该编译工作直到到达链接,然后吐出这些错误:
/home/username/arm/bin/../lib/gcc/arm-Linux-androideabi/4.9.x/../../../../arm-Linux-androideabi/bin/ld: error: fio.o: incompatible target
/home/username/arm/bin/../sysroot/usr/lib/crtbegin_dynamic.o:crtbrand.c:function _start: error: undefined reference to 'main'
engines/net.c:318: error: undefined reference to 'inet_network'
engines/net.c:322: error: undefined reference to 'inet_network'
engines/net.c:323: error: undefined reference to 'inet_network'
engines/net.c:318: error: undefined reference to 'inet_network'
cgroup.c:28: error: undefined reference to 'setmntent'
cgroup.c:34: error: undefined reference to 'getmntent_r'
cgroup.c:45: error: undefined reference to 'endmntent'
collect2: error: ld return 1 exit status
Makefile:433: recipe for target 'fio' failed
make *** [fio] Error 1
我需要指定特定的链接程序吗?
我无法提供完整的答案,但希望以下帮助。
您已经正确地确定您的第一个二进制文件是针对错误的体系结构(x86-64与ARM aarch64)编译的。解决方案是使用交叉编译器,就像使用arm-linux-androideabi-gcc
一样。
要解决“未定义的引用”错误,您还必须链接到提供这些符号的库的交叉编译版本。 -L
标志可用于指向已编译的库。您必须自行找到路径。
对于Android,C标准库由名为Bionic的库提供。https://android.googlesource.com/platform/bionic/
我对NDK没有任何经验,所以我不知道它是否包含Bionic。我认为NDK仅用于从Java应用程序中使用C / C ++。这可能不足以构建独立的本机程序。
我强烈推荐Karim Yaghmour的“嵌入式Android”,以获取有关编译Android OS以及向构建版本添加本机应用程序的更多信息。如果您拥有一个完整的AOSP构建系统(非常庞大),那么该书将帮助您将所需的本机程序添加到构建中。从那里您可以将程序侧面加载到设备。
[根据邮件列表中有关fio Android cross compilation的帖子,假设您拥有运行良好的Android工具链,则您应该能够执行以下操作:
UNAME=Android CROSS_COMPILE=<path/to/toolchain>/bin/arm-linux-androideabi-
CC=<path/to/toolchain>/bin/arm-linux-androideabi-clang ./configure
(请注意,以上均为一行)
然后执行make
。