Linking different object files created from crosstool with the current shared library created from ndk-build

1,113 views
Skip to first unread message

s.rawat

unread,
Jun 30, 2011, 7:15:46 AM6/30/11
to andro...@googlegroups.com, android-d...@googlegroups.com, android...@googlegroups.com
NOTE: Changed the subject from
OLD :Cross compiling the android application using the jni call to invoke a c method failing
NEW : Linking different object files created from crosstool with the current shared library created from ndk-build


No, No success in this , I am not sure if I had written the correct make file(Android.mk) ,
I have boiled down the problem to this :

(1)I havce three files file1.c file2.c file3.c , out of this I am making a shared file .so using the ndk-build utility , I tried with file1.c and started adding the file2 and 3.
With file1.c I get the shared library happily, as it doesnt have any archtecture specific code, which i can load and call the c functionalities of file1.c  using JNI calls
here is the Android.mk for that :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#LOCAL_LDLIBS    := -llog

LOCAL_MODULE    := testEngine
LOCAL_SRC_FILES := file1.c
# No special compiler flags.
LOCAL_CFLAGS += MY_FLAGS
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)

But I need the fucntionalities of other two files as well but these two files 1 and 2 contains the archtecture specifi code which are not gwtting compiled by ndk, so what I did I seperately created the object files for them file1.o and file2.o
using these commands :
my-tool-chain-gcc -fPIC -g -c -Wall file1.c and
my-tool-chain-gcc -fPIC -g -c -Wall file2.c

but now I dont know how to create a final shared library with these two .o files and the file1.c code.
Do you have any idea.??
I am trying this for two days continously but havent got any satisfcatory solution for this(in plain terms - No solution for this ), once get I am sure will post this with full description
PLz help !!!.




Rgds,
-J



 



"..pain is temporary.....quitting lasts forever......"



On Thu, Jun 30, 2011 at 10:43 AM, s.rawat <imsaura...@gmail.com> wrote:
HI Thanks for the reply..
here is my Android.mk :


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#LOCAL_LDLIBS    := -llog

LOCAL_MODULE    := testEngine
LOCAL_SRC_FILES := engine.c cpu.c IO.c memory.c
# No special compiler flags.
LOCAL_CFLAGS += -fno-builtin-printf -static -O3 -DCORE_FREQ=800 -DBAREMETAL -DPMU_CCNT_V7
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)

So you mean to say instead of putting engine.c IO.c memory.c i should mention the objct file for each and the LOCAL_MODULE equal to the final library like this :


LOCAL_MODULE    := testEngine
LOCAL_SRC_FILES := engine.o cpu.o IO.o memory.o

I will try and post the results ...!!

Thanks again for the reply !!
Rgds,
-J



"..pain is temporary.....quitting lasts forever......"




2011/6/29 Enrique Ocaña González <eoc...@igalia.com>
On Martes, 28 de Junio de 2011 23:27:31 Jessica escribió:

> and placed it under the same folder where ndk-build was creating
> the .so file that is libs/armeabi> but when i am i trying to load the
> libary i am getting this error by
>
> doing the logcat
>
> D/dalvikvm(17719): Trying to load lib /data/data/<package name>/lib/
> libTestengine.so 0x43e45c48
>
> I/dalvikvm(17719): Unable to dlopen(<package name>/lib/
> libTestengine.so): Cannot load library: link_image[1995]: failed to
> link libTestengine.so

Are you sure that the library is being included in the APK? (unzip -l
bin/*.apk)

If you use Eclipse, clean your project, rebuild it and look into the APK
again. If it's still not there, maybe you need to explicitly create some rules
to include the library as an external precompiled library:

jni/Application.mk ------

...
APP_MODULES := testengine
...

jni/Android.mk ----------

...
include $(CLEAR_VARS)
LOCAL_MODULE := testengine
LOCAL_SRC_FILES := relative/path/to/libTestengine.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/relative/path/to/include/dir
include $(PREBUILT_SHARED_LIBRARY)
...

With these declarations, ndk-build will copy the library from its original
place to libs and obj and the Eclipse build scripts will include it in the
APK.

--
Enrique Ocaña

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



361.gif

David Turner

unread,
Jun 30, 2011, 9:54:54 AM6/30/11
to andro...@googlegroups.com, android-d...@googlegroups.com, android...@googlegroups.com
Archive your object files into a library (e.g. libfoo.a)

Then declare a prebuilt static library in your Android.mk, as in
....
include $(CLEAR_VARS)
LOCAL_MODULE := libfoo_prebuilt
LOCAL_SRC_FILES := /path/to/libfoo.a
include $(PREBUILT_STATIC_LIBRARY)
...

Then later reference it in the LOCAL_STATIC_LIBRARIES line of your main module

....
LOCAL_STATIC_LIBRARIES := libfoo_prebuilt
include $(BUILD_SHARED_LIBRARY)

Read docs/PREBUILT.html for more details.

Of course, the link is likely to fail if you use a cross-toolchain that doesn't support Android properly.
361.gif

s.rawat

unread,
Jun 30, 2011, 2:03:06 PM6/30/11
to andro...@googlegroups.com, android-d...@googlegroups.com, android...@googlegroups.com
HI David ,
Thanks for the reply
>
Of course, the link is likely to fail if you use a cross-toolchain that doesn't support Android properly.
- Does this means object files being created from the cross compiler may or may not get linked with the shared library created with the ndk-build(ndk5rc) always.What kind of proper support is needed from the tool chain?
The reason why I am using the cross tool is that certain code (assembly routines) needs to be necessarily compiled by the tool chain but since file1 functionality above is arch independent , we are partially using android ndk-build(for shared library) and partially via tool chain (file2 and 3)but both the shared libarary(from ndk) and the object files created from the tool chain needs to be linked together to make one single .so file which can be loaded and called via jni calls.UNfortunately this is not happening.(dont know how to link them, until you proposed the above way, will check on this)
Rgds,
--Saurabh
"..pain is temporary.....quitting lasts forever......"



--Saurabh




 



"..pain is temporary.....quitting lasts forever......"



On Thu, Jun 30, 2011 at 10:43 AM, s.rawat <imsaura...@gmail.com> wrote:
HI Thanks for the reply..
here is my Android.mk :


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#LOCAL_LDLIBS    := -llog

LOCAL_MODULE    := testEngine
LOCAL_SRC_FILES := engine.c cpu.c IO.c memory.c
# No special compiler flags.
LOCAL_CFLAGS += -fno-builtin-printf -static -O3 -DCORE_FREQ=800 -DBAREMETAL -DPMU_CCNT_V7
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)

So you mean to say instead of putting engine.c IO.c memory.c i should mention the objct file for each and the LOCAL_MODULE equal to the final library like this :


LOCAL_MODULE    := testEngine
LOCAL_SRC_FILES := engine.o cpu.o IO.o memory.o

I will try and post the results ...!!

Thanks again for the reply !!
Rgds,
-Saurabh
361.gif

s.rawat

unread,
Jul 1, 2011, 6:37:45 AM7/1/11
to andro...@googlegroups.com, android...@googlegroups.com
I am able to get the shared library by editing the Android.mk as you have suggested but when I am tryign to open the it is using System.Load(lib name) ,
I am getting this error :

D/dalvikvm( 4757): Trying to load lib /data/data/mypackageName/lib/libtestEngine.so 0x43ec1f78
I/dalvikvm( 4757): Unable to dlopen(/data/datamypackageName/lib/libtestEngine.so): Cannot load library: link_image[1995]: failed to link libtestEngine.so


libtestEngine is the final shared library containing the other shared libraries.I havent used any cross tool, but modified the Android.mk and added Application.mk--> APP_ABI := armeabi-v7a
but still the resulting library is not getting loaded and throwing the above eerror..

Any idea on this..!
Rgds,
Saurabh



On Thu, Jun 30, 2011 at 11:36 PM, s.rawat <imsaura...@gmail.com> wrote:

"..pain is temporary.....quitting lasts forever......"



Forwarded conversation

Subject: Linking different object files created from crosstool with the current shared library created from ndk-build
------------------------
----------
From: David Turner <di...@android.com>
Date: Thu, Jun 30, 2011 at 7:24 PM


Archive your object files into a library (e.g. libfoo.a)

Then declare a prebuilt static library in your Android.mk, as in
....
include $(CLEAR_VARS)
LOCAL_MODULE := libfoo_prebuilt
LOCAL_SRC_FILES := /path/to/libfoo.a
include $(PREBUILT_STATIC_LIBRARY)
...

Then later reference it in the LOCAL_STATIC_LIBRARIES line of your main module

....
LOCAL_STATIC_LIBRARIES := libfoo_prebuilt
include $(BUILD_SHARED_LIBRARY)

Read docs/PREBUILT.html for more details.

Of course, the link is likely to fail if you use a cross-toolchain that doesn't support Android properly.


--

--

----------
From: s.rawat <imsaura...@gmail.com>
Date: Thu, Jun 30, 2011 at 11:33 PM


HI David ,
Thanks for the reply
>
- Does this means object files being created from the cross compiler may or may not get linked with the shared library created with the ndk-build(ndk5rc) always.What kind of proper support is needed from the tool chain?
The reason why I am using the cross tool is that certain code (assembly routines) needs to be necessarily compiled by the tool chain but since file1 functionality above is arch independent , we are partially using android ndk-build(for shared library) and partially via tool chain (file2 and 3)but both the shared libarary(from ndk) and the object files created from the tool chain needs to be linked together to make one single .so file which can be loaded and called via jni calls.UNfortunately this is not happening.(dont know how to link them, until you proposed the above way, will check on this)
Rgds,
--Saurabh


--Saurabh
-Saurabh




----------
From: s.rawat <imsaura...@gmail.com>
Date: Thu, Jun 30, 2011 at 11:33 PM
To: "saurabh.rawat" <saurab...@nexbits.com>





361.gif

s.rawat

unread,
Jul 1, 2011, 7:30:23 AM7/1/11
to andro...@googlegroups.com, android...@googlegroups.com
ALso when I read the elf information i can see this :
 
--> libsqlite3.so.0 instead of libsqlite3.so , a 0(zero) gets appended to its name though ndk-build doesnt provides the .s0.0 post fix to the library.
 

my-tool-chain-readelf libs/armeabi-v7a/libtestEngine.so -ld



Elf file type is DYN (Shared object file)
Entry point 0x10e0
There are 5 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  EXIDX          0x0038c8 0x000038c8 0x000038c8 0x000b8 0x000b8 R   0x4
  LOAD           0x000000 0x00000000 0x00000000 0x03980 0x03980 R E 0x1000
  LOAD           0x003980 0x00004980 0x00004980 0x00140 0x0014c RW  0x1000
  DYNAMIC        0x003980 0x00004980 0x00004980 0x000d0 0x000d0 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4

 Section to Segment mapping:
  Segment Sections...
   00     .ARM.exidx
   01     .hash .dynsym .dynstr .rel.dyn .rel.plt .plt .text .rodata .ARM.extab .ARM.exidx
   02     .dynamic .got .bss
   03     .dynamic
   04    

Dynamic section at offset 0x3980 contains 22 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libsqlite3.so.0]
 0x00000001 (NEEDED)                     Shared library: [libc.so]
 0x00000001 (NEEDED)                     Shared library: [libstdc++.so]
 0x00000001 (NEEDED)                     Shared library: [libm.so]
 0x00000001 (NEEDED)                     Shared library: [liblog.so]
 0x00000001 (NEEDED)                     Shared library: [libdl.so]
 0x0000000e (SONAME)                     Library soname: [libtestEngine.so]
 0x00000010 (SYMBOLIC)                   0x0
 0x00000004 (HASH)                       0xd4
 0x00000005 (STRTAB)                     0x9a4
 0x00000006 (SYMTAB)                     0x374
 0x0000000a (STRSZ)                      1378 (bytes)
 0x0000000b (SYMENT)                     16 (bytes)
 0x00000003 (PLTGOT)                     0x4a50
 0x00000002 (PLTRELSZ)                   168 (bytes)
 0x00000014 (PLTREL)                     REL
 0x00000017 (JMPREL)                     0xf28
 0x00000011 (REL)                        0xf08
 0x00000012 (RELSZ)                      32 (bytes)
 0x00000013 (RELENT)                     8 (bytes)
 0x6ffffffa (RELCOUNT)                   2
 0x00000000 (NULL)                       0x0

 

Rgds,

Saurabh


"..pain is temporary.....quitting lasts forever......"



361.gif
Reply all
Reply to author
Forward
0 new messages