我正在尝试使用 ARM 交叉编译器 将我的 c 程序链接到共享对象库 (libfoo.so) arm-linux-gnueabi-gcc .我在 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.c
arm-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 设备上运行?
最佳答案
链接器无法动态链接到库。
定义 LD_LIBRARY_PATH 环境变量以包含库:
export LD_LIBRARY_PATH=/home/foo
关于android - 交叉编译时链接到共享对象库 (.so),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61503395/