android - 交叉编译时链接到共享对象库 (.so)

标签 android c linux shared-libraries cross-compiling

我正在尝试使用 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.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 设备上运行?

最佳答案

链接器无法动态链接到库。

定义 LD_LIBRARY_PATH 环境变量以包含库:

export LD_LIBRARY_PATH=/home/foo

关于android - 交叉编译时链接到共享对象库 (.so),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61503395/

相关文章:

android - 填充表格布局android

android - Firestore 重新连接到 Internet 后无法检索数据

c++ - switch 语句中的逗号运算符是什么意思?

c - IP作为数字没有通常的功能?

c - 使用 strchr 和 strtol 在一行中读取可变数量的 long int

Linux调度程序修改

android - 更改android键盘中的字体样式

android - 如何在 Android (Nexus 7) 上显示不可缩放的网页

c# - Mono 编译器生成的 .exe 直接从 linux 命令行运行,为什么?

linux - AllJoyn Router 是否支持发现在有线本地网络上运行的远程服务