Thursday, May 7, 2009

android notes - build and test shared library

1. download system.tar.gz from benno and extract it to ~/myspace/tmp.
2. make a link.

cd ~/myspace/tmp/system/lib
ln -s libc.so libc.so.6

Is it necessary? but it fixed the problem to looking for libc.so.6.
3. Here is the Makefile.

CC=arm-none-linux-gnueabi-gcc
LD=arm-none-linux-gnueabi-ld
AR=arm-none-linux-gnueabi-ar
LD_OPTS= --entry=_start \
--dynamic-linker /system/bin/linker -nostdlib \
-rpath /system/lib -rpath ~/myspace/tmp/system/lib \
-L ~/myspace/tmp/system/lib -L .
foo2.a : foo2.c
$(CC) -g -fpic -c foo2.c
$(CC) -g -shared -nostdlib -o libfoo2.so foo2.o
$(AR) -rcs $@ foo2.o
foo2: main.c start.c foo2.a
$(CC) -g -c main.c
$(CC) -g -c start.c
$(LD) $(LD_OPTS) -lc -lfoo2 -o foo2 main.o start.o



when build the library, should set "-nostdlib"

4. run the commands

$ make foo2
$ adb push libfoo2.so /data
$ adb push foo2 /data
$ adb shell
# cd data
# ./trace ./foo2