【发布时间】:2020-04-29 13:50:06
【问题描述】:
我正在尝试使用 ARM 交叉编译器 arm-linux-gnueabi-gcc 将我的 c 程序链接到共享对象库 (libfoo.so)。我正在 Ubuntu 系统上编译,我想在 Android 设备上运行该程序。编译工作正常,但是当我尝试在我的 android 设备上运行程序时出现错误。
我创建了一个包含以下文件的简单测试程序:
foo.c:
#include <stdio.h>
void foo(void){ puts("Hello, I am a shared library"); }
foo.h:
#ifndef foo_h__
#define foo_h__
extern void foo(void);
#endif
main.c:
#include <stdio.h>
#include "foo.h"
int main(void)
{
puts("This is a shared library test...");
foo();
return 0;
}
然后,我使用以下命令创建了共享对象库:arm-linux-gnueabi-gcc -c -fPIC foo.carm-linux-gnueabi-gcc -shared -o libfoo.so foo.o
然后我使用以下代码编译我的程序:arm-linux-gnueabi-gcc -L/home/foo -o test main.c -lfoo
使用 adb 将测试程序上传到 Android 设备后,我无法运行它。相反,我得到了错误:/system/bin/sh: ./test: No such file or directory
我在正确的目录中并且存在测试文件,所以我假设它是找不到的共享库。我也尝试将 libfoo.so 上传到 android 设备(与编译时指定的相同路径),但仍然无法运行程序。
我已经使用 arm-linux-gnueabi-gcc -static -o test main.c foo.o 让它与静态库 (foo.o) 一起工作,但不是共享库。
在交叉编译时如何正确链接共享库,以确保程序随后可以在 Android 设备上运行?
【问题讨论】:
-
我尝试使用 -fPIC 而不是 -fpic。仍然无法运行./test。我会更新问题以避免混淆。
-
请务必尝试以下命令:
file test和ldd test了解更多详细信息(其中 test 是您编译的可执行文件)。
标签: android c linux shared-libraries cross-compiling