Search code examples
androidcarmcross-compilingcodesourcery

Error during Cross-compiling C code with Dynamic libraries


I've two files:

lib.c

#include<stdio.h>

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

and 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:

export LD_LIBRARY_PATH=pwd

gcc -c -fpic lib.c

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

gcc main.c -ldl

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:

export LD_LIBRARY_PATH=pwd

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

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

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

adb push main /system/app

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

./main: not found

even if my file is right there!

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:

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

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


Solution

  • 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]
    

    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.

    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.