1

I'm experimenting with native libs cross compiled from ubuntu. What I really want is to be able to compile my c++ libraries and use them in a Xamarin.Android app.

First, I have the arm gcc compiler: arm-linux-gnueabi-gcc. I have a simple lib (libmr.so) that has one function void Print(); that prints something to the console. I'm compiling with:

arm-linux-gnueabi-gcc -Wall -shared -o libmr.so mr.c

When inspecting it using file libmr.so everything seems to be good. However when I'm including it with my android app and try to load it, it is as if it doesn't exist. I'm certain it is there, the path is absolutely correct as I tried to load another lib (libmonodroid.so) from the same folder and it worked.

I tried inspecting both libs to find some kind of a difference:

$ objdump -x libmr.so | grep NEEDED
      NEEDED               libc.so.6

$ objdump -x libmonodroid.so | grep NEEDED
      NEEDED               libc.so
      ... (in addition to other libs)

This is the only difference I'm finding between the two. libmonodroid.so loads properly but libmr.so acts as if it doesn't exist. (I'm using dlopen to load a lib)

EDIT:

I built an executable using the same toolchain, gave me a clue:

  • Static linking with libc: arm-linux-gnueabi-gcc -Wall -o hi source.c -static. Pushed hi to my android devices and executed it with adb. Result: SUCCESS!
  • Dynamic linking with libc: arm-linux-gnueabi-gcc -Wall -o hi source.c. Result: it's not even there! Meaning ./hi gives /system/bin/sh: ./hi: not found although it's absolutely there.

So, looks like libc is really the culprit? Maybe I need to link dynamically with not libc.so.6 but with libc.so just like libmonodroid.so is doing?

auselen
  • 27,577
  • 7
  • 73
  • 114
mrahhal
  • 3,322
  • 1
  • 22
  • 41

2 Answers2

0

Check this out for anyone having the same problem. Download the ndk, there's a standalone toolchain for building native libs that run on android that you can extract (you won't need the whole ndk).

mrahhal
  • 3,322
  • 1
  • 22
  • 41
0

I was able to run a basic app on ubuntu 15.04 with this Makefile in the same dir as my hi.c:

$ cat hi.c               # create hi.c with favorite editor
#include <stdio.h>

int main(int argc, char** argv) {
  int uid = getuid();
  int eid = geteuid();
  printf("Hello world\n");
  printf("You are uid: %d and eid; %d",  uid, eid);
  return 0;
}

$ cat Makefile                      # create two line Makefile
CC=arm-linux-gnueabi-gcc
LDFLAGS=-static

$ make hi                           # build arm based hi executable file
arm-linux-gnueabi-gcc   -static  hi.c   -o hi

$ file hi                           # check file type
hi: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically   linked, for GNU/Linux 2.6.32, BuildID[sha1]=17b65e60cdd32449ac237bfd1b8238bfa1d416a0, not stripped

$ adb push hi /data/local/tmp          # copy to droid fon
4403 KB/s (593252 bytes in 0.131s)

$ adb shell /data/local/tmp/hi         # run hi executable
adb shell /data/local/tmp/hi
Hello world
You are uid: 2000 and eid; 2000



$ uname -a
Linux lenny 3.19.0-28-generic #30-Ubuntu SMP Mon Aug 31 15:52:51 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Note that I do not have any NDK installed.