Linking external libraries to Android native shared library

3,379 views
Skip to first unread message

purvi

unread,
Jan 5, 2010, 5:33:02 PM1/5/10
to android-ndk
Hi,

I have a question. To include any external library whether a static
library (*.a) or shared library (*.so) and link to the current native
shared library I have to inlcude the below code in my Andriod.mk

include $(CLEAR_VARS)

LOCAL_PREBUILT_LIBS:= libexternal1.a

include $(BUILD_MULTI_PREBUILT)

include $(CLEAR_VARS)

LOCAL_MODULE:=mynativeexample

LOCAL_SRC_FILES:=myexample.c

LOCAL_STATIC_LIBRARIES:=libexternal1

include $(BUILD_SHARED_LIBRARY)

The library libexternal1.a is compiled and build outside android. and
resides in the directory same as the path of my Andriod.mk
The path is \android-ndk-1.6_r1\apps\MyApplication\project\jni\.

And I am getting an error : No rule to make target 'out/apps/
MyApplication//libexternal1.a' needed to make 'out/apps/
MyApplication//
mynativeexample.so'
If I copy the libexternal1.a to 'out/apps/MyApplication//' it gives me
error
'out/apps/MyApplication//libexternal1.a' : file not recognized: File
format not recognized
As per few android-ndk threads I went thru it is improper to copy the
libexternal1.a to the out directory as the library should be
automatically created in the 'out' directory.
Something is missing in my Android.mk or something related to path of
my external static library.

The same error occurs when I use a shared library instead of static
library.

Can anyone please help me why is this happening and how to solve
this.?

Thanks.
Regards,
Purvi

northfork

unread,
Jan 6, 2010, 4:45:19 AM1/6/10
to android-ndk
how was your external library compiled?
it should be made with android tools to match the platform you are
making for.

purvi

unread,
Jan 6, 2010, 9:38:39 AM1/6/10
to android-ndk
I used gcc to compile and build external library.

> > Purvi- Hide quoted text -
>
> - Show quoted text -

purvi

unread,
Jan 6, 2010, 10:52:27 AM1/6/10
to android-ndk
Hi,

So is it that I have to use arm-eabi-gcc compiler to compile a C
source code and build a static or shared library out of it and use it
in my Android.mk. Or can I include the C source code (not the native
code) in my Android.mk as a module?

Thanks.
Regards,
Purvi

> > - Show quoted text -- Hide quoted text -

David Turner

unread,
Jan 6, 2010, 5:31:49 PM1/6/10
to andro...@googlegroups.com
LOCAL_PREBUILT_LIBS is not supported by the NDK build system.
It is a feature of the full Android build system which is very differently implemented.

Try adding the path to your prebuilt static library to your LOCAL_LDLIBS instead.

--
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.




purvi

unread,
Jan 7, 2010, 10:01:23 AM1/7/10
to android-ndk
Ok. Earlier I did try with LOCAL_LDLIBS:= -lexternall to add my
precompiled library. The externall.a is compiled using gcc and the
path to this library is \android-ndk-1.6_r1\build\platforms
\android-4\arch-arm\usr\lib. I came across that LOCAL_LDLIBS is only
used to access the libraries provided by Android NDK not any other
external precompiled library. So When I place my externall.a at the
above path it gives me error : externall.a : file not recognized:File
format not recognized.

Is there a different way to deal with this?

> Try adding the path to your prebuilt static library to your LOCAL_LDLIBS
> instead.

I retied modifying my Android.mk for LOCAL_LDLIBS. The problem still
persists.
Can anyone please help me out with this?

Thanks.
Regards,
Purvi

David Turner

unread,
Jan 7, 2010, 12:30:26 PM1/7/10
to andro...@googlegroups.com
On Thu, Jan 7, 2010 at 7:01 AM, purvi <purvi...@gmail.com> wrote:
Ok. Earlier I did try with LOCAL_LDLIBS:= -lexternall to add my
precompiled library. The externall.a is compiled using gcc and the
path to this library is \android-ndk-1.6_r1\build\platforms
\android-4\arch-arm\usr\lib. I came across that LOCAL_LDLIBS is only
used to access the libraries provided by Android NDK not any other
external precompiled library. So When I place my externall.a at the
above path it gives me error : externall.a : file not recognized:File
format not recognized.


You don't need to use -lexternall and modify anything under build/platforms,
just use the path to the static library as in some/directory/libexternall.a instead on your LOCAL_LDLIBS line.

On the other handm the "File format not recognized" probably means that there is something wrong with your
static library. How was it generated exactly ? Did you use a cross-compiler that targets ARMv5TE for example ?
 
Is there a different way to deal with this?

> Try adding the path to your prebuilt static library to your LOCAL_LDLIBS
> instead.

I retied modifying my Android.mk for LOCAL_LDLIBS. The problem still
persists.
Can anyone please help me out with this?

Thanks.
Regards,
Purvi

purvi

unread,
Jan 7, 2010, 1:30:26 PM1/7/10
to android-ndk
ok. I get the idea how to include the external library.

I used gcc to generate the library. As a suggestion given to me by a
group member, the reason for the file not recognized error is that it
is not compatible with the toolchain. So I tried to use the arm-eabi-
gcc to compile the static libraries but the library was not created.
Can you please tell me how can I compile it to be compatible?

Thanks.
Regards,
Purvi

On Jan 7, 12:30 pm, David Turner <di...@android.com> wrote:

> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>


> > .
> > For more options, visit this group at

> >http://groups.google.com/group/android-ndk?hl=en.- Hide quoted text -

David Turner

unread,
Jan 7, 2010, 1:47:34 PM1/7/10
to andro...@googlegroups.com
On Thu, Jan 7, 2010 at 10:30 AM, purvi <purvi...@gmail.com> wrote:
ok. I get the idea how to include the external library.

I used gcc to generate the library. As a suggestion given to me by a
group member, the reason for the file not recognized error is that it
is not compatible with the toolchain. So I tried to use the arm-eabi-
gcc to compile the static libraries but the library was not created.
Can you please tell me how can I compile it to be compatible?


There really is too much detail to give if you don't know the difference between a compiler and cross-compiler.
I instead recommend you to write an Android.mk file to build your sources instead and use the NDK to handle
all of the nasty details instead.

Hope this helps

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

purvi

unread,
Jan 7, 2010, 4:46:16 PM1/7/10
to android-ndk
Thank you for the guidance it was quite helpful.

> There really is too much detail to give if you don't know the difference
> between a compiler and cross-compiler.

I was able to make out the diff between compiling and cross
compiling.
Can you please tell me how can we crosscompile any assembly source
code.

> I instead recommend you to write an Android.mk file to build your sources
> instead and use the NDK to handle
> all of the nasty details instead.

Actually yes I tried this option at the very first time, but the C
source code have some assembly code as well.
So when I include the assembly source in Android.mk it is giving me
error Bad instructions on each line of my assembly code.
The same is true when I tried to cross compile the assembly code with
arm-none-eabi-gcc.

Thanks.
Regards,
Purvi

David Turner

unread,
Jan 7, 2010, 5:26:11 PM1/7/10
to andro...@googlegroups.com
Make the assembly source names end with .S instead of .c
This will instruct the compiler to pre-process them and later run through the result directly through the assembler.

If you use the NDK, simply do something like:

LOCAL_SRC_FILES := foo.c bar.S

to build foo.c as a C source file and bar.S as an assembler file.

If you have inlined assembler within a C source code, it probably is compiler-specific and something tells me it's not
compatible with gcc anyway.

purvi

unread,
Jan 7, 2010, 5:54:16 PM1/7/10
to android-ndk
> If you use the NDK, simply do something like:
>
> LOCAL_SRC_FILES := foo.c bar.S
>
> to build foo.c as a C source file and bar.S as an assembler file.
>

Yes, that is exactly what I am doing in my Android.mk. I placed all my
c source and assembly source in LOCAL_SRC_FILES in the order of their
dependencies. it is giving me error Bad instructions on each line of
my assembly code. If you can please help to understand the reason for
it and any solution to this error.

> If you have inlined assembler within a C source code, it probably is
> compiler-specific and something tells me it's not
> compatible with gcc anyway.
>

There is no inline assembly code in my C code.

Thank you for the guidance. It is really helpful.

Regards,
Purvi

David Turner

unread,
Jan 7, 2010, 6:39:55 PM1/7/10
to andro...@googlegroups.com
You will have to provide at least a sample assembly source file so we can have a look at it.
Are you sure it's ARM assembly ?

Reply all
Reply to author
Forward
0 new messages