交叉编译C code。与动态库时出错 [英] Error during Cross-compiling C code with Dynamic libraries

查看:127
本文介绍了交叉编译C code。与动态库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:

lib.c

#include<stdio.h>

void hi() {
  printf("Hi i'm a library function in lib.so\n");
} 

和的main.c

#include<stdio.h>
#include<dlfcn.h>
/* based on Jeff Scudder's code */
int main() {
  void *SharedObjectFile;
  void (*hi)();

  // Load the shared libary;
  SharedObjectFile = dlopen("./lib.so", RTLD_LAZY);

  // Obtain the address of a function in the shared library.
  ciao = dlsym(SharedObjectFile, "hi");

  // Use the dynamically loaded function.
  (*hi)();

  dlclose(SharedObjectFile);
}

和我尝试使用以下命令建立一个可执行文件:

And I've tried to build an executables using the following commands:

导出LD_LIBRARY_PATH = PWD

export LD_LIBRARY_PATH=pwd

GCC -c -fpic lib.c

gcc -c -fpic lib.c

-shared的gcc -o -lc lib.so lib.o

gcc -shared -lc -o lib.so lib.o

GCC的main.c -ldl

gcc main.c -ldl

和它的作品pretty好。
然后我试着用下面的命令导出在Android上我的节目(的Nexus One,采用ARM-v7-0a弓):

And it works pretty well. Then I've tried to export my program on Android (Nexus One, with ARM-v7-0a arch) using the following commands:

导出LD_LIBRARY_PATH = PWD

export LD_LIBRARY_PATH=pwd

臂无-Linux的gnueabi-GCC -c -fpic lib.c

arm-none-linux-gnueabi-gcc -c -fpic lib.c

臂无-Linux的gnueabi-GCC -shared -o -lc lib.so lib.o

arm-none-linux-gnueabi-gcc -shared -lc -o lib.so lib.o

臂无-Linux的gnueabi-GCC的main.c -ldl -o主

arm-none-linux-gnueabi-gcc main.c -ldl -o main

亚行主推/系统/应用程序

adb push main /system/app

在智能手机上的正确的文件夹执行./main的结果就是:

The result of executing ./main on the correct folder on my smartphone is just:

./主:找不到

即使我的文件就在那里!

even if my file is right there!

我是不是在交叉编译过程中缺少什么?任何帮助吗?
我利用codeSourcery的交叉编译器,它可以很好地用于没有。所以静态库的程序。
谢谢

Am I missing anything during the cross-compile process? Any help? I'm using the cross-compiler from CodeSourcery and it works well for static programs without .so libraries. Thanks

修改:作为伊戈尔以下规定,这是一个连接问题。此命令修复它:

EDIT: as Igor states below, that was a linker issue. This command fixes it:

臂无-Linux的gnueabi-gcc的-o测试的main.c轮候册, - 动态链接= /系统/斌/连接-ldl

arm-none-linux-gnueabi-gcc -o test main.c -Wl,--dynamic-linker=/system/bin/linker -ldl

在我的非常情况下,我需要其他库因为在/系统/ lib中/有没有很多.so文件。

in my very case I need other libraries because in /system/lib/ there are no many .so files.

推荐答案

在未找到消息指的不是共享对象,而是动态连接器。 Linux使用 /lib/ld-linux.so.2 (或 /lib64/ld-linux-x86-64.so.2 用于基于x64),而Android使用 /斌/链接。您可以检查哪些动态加载程序使用 readelf -l <​​/ code>使用,例如:

The "not found" message refers not to the shared object but to the dynamic linker. Linux uses /lib/ld-linux.so.2 (or /lib64/ld-linux-x86-64.so.2 for x64) while Android uses /bin/linker. You can check which dynamic loader your program uses with readelf -l, e.g.:

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4
  INTERP         0x000134 0x08048134 0x08048134 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.2]

您可以指定一个链接器LD的使用 - 动态链接开关,但也有可能是其他方面的差异。例如,Android使用一个精简的libc实现所谓的仿生,它可能会丢失,你的程序所需的功能,或有不同的行为。

You can specify a linker to use with ld's --dynamic-linker switch, but there are likely to be other differences. For example, Android uses a stripped-down libc implementation called bionic, and it may be missing functionality that your program relies on, or have different behavior.

为Android编译程序时,您应该使用NDK或其他Android系统的有针对性的工具链。虽然它基于Linux内核,差异足够大认为Linux针对性的工具链是不够的。

You should use NDK or another Android-targeted toolchain when compiling programs for Android. Even though it's based on Linux kernel, the differences are large enough that Linux-targeted toolchains are not sufficient.

这篇关于交叉编译C code。与动态库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆