Error while cross-compiling C code with dynamic link libraries

I have 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 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 tried to create 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 really well. Then I tried to export my program to 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

Execution result. / main in the correct folder on my smartphone:

./main: not found

even if my file is here!

Am I missing something during the cross-compilation process? Any help? I am using a cross compiler from CodeSourcery and it works well for static programs without .so libraries. Thanks to

EDIT : As Igor states, it 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 case, I need other libraries because there are not many .so files in / system / lib /.

+3


source to share


1 answer


The "not found" message is not related 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 is using with readelf -l

, for example:

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 the linker to be used with ld --dynamic-linker

, but there are likely to be other differences. For example, Android uses a preempted libc implementation called bionic, and may lack functionality that your program relies on or have different behavior.

When compiling Android programs, you must use the NDK or other Android target binding. Although it is based on the Linux kernel, the differences are large enough that Linux-centric programming chains are not sufficient.

+6


source







All Articles