C++ Shared Library

10 views
Skip to first unread message

zenoname

unread,
Nov 5, 2009, 3:16:00 AM11/5/09
to android-ndk
Hello,

(first sorry I posted the same message in the android developer group
but as it doesn't appear I post it in the NDK group as it is more
related to it)

I'm trying to create a simple project with the Android NDK.
I created a jni library which is calling a function from a shared C*
library.
I make it work with a C library but as soon as I try with a C++
library it crashes (when calling the c++ function)
As many posts I read I suspect a name mangling problem but whatever I
tried it doesn't change the problem

Here is a summary of what I've done : I created a jni library linking
two simple libs (jnilib my jni interface with java, libtoto.so my c
library, libtotocpp.so my cpp lib). The c* libs simply export a
function returning a int. If I call GetNumber() form the C lib no
problem. If I call the GetNumberCPP() from my C++ lib it crashes

Here are the code and compilation stuffs.

If anyone could tell me what's wrong that would be very very cool :)

Thanks.


- jnilib.c (arm-eabi-gcc -shared jnilib.c -o libjnilib.so libtoto.so
libtotocpp.so -nostdlib -Bdynamic)
Here if I don't call GetNumberCPP() all works

#include "toto.h"
#include "toto.hpp"

#ifdef _cplusplus
extern "C"
{
#endif
jstring Java_amob_jnscapi_jnscapi_initserver (JNIEnv* env,
jobject thiz )
{
char version[50];
sprintf(version,"%d %d",GetNumberC(),GetNumberCPP());
return (*env)->NewStringUTF(env, version);
}
#ifdef _cplusplus
}


- libtoto.so (arm-eabi-gcc -shared toto.c -o libtoto.so -nostdlib -
Bdynamic)

#include "toto.h"
int GetNumberC()
{
return 33;
}

- libtotocpp.so (arm-eabi-g++ -shared toto.cpp -o libtotocpp.so -
nostdlib -Bdynamic)

#include "toto.hpp"
#ifdef _cplusplus
extern "C"
{
#endif
int GetNumberCPP()
{
return 56;
}
#ifdef _cplusplus
}
#endif

- In my java class I tried to load all libs ...

System.loadLibrary("totocpp");
System.loadLibrary("toto");
System.loadLibrary("jnilib");

fadden

unread,
Nov 6, 2009, 3:42:57 PM11/6/09
to android-ndk
On Nov 5, 12:16 am, zenoname <zenonamem...@gmail.com> wrote:
>      #ifdef _cplusplus
>      extern "C"
>      {
>      #endif

Should be "__cplusplus", i.e. two underscores. Might help with the
name mangling.

zenoname

unread,
Nov 8, 2009, 4:05:51 AM11/8/09
to android-ndk
Thanks for your input.
Unfortunately it doesn't avoid the crash when I call the C++ function :
(
According to someone else advice I will try to restart a project from
scratch
But if anyone has any idea other idea about what could be wrong ...

zenoname

unread,
Nov 9, 2009, 4:29:46 PM11/9/09
to android-ndk
I started again from scratch.
I modified the hello-jni from the NDK app folder, linking my
libtoto.so
It seems to work when I add my simple lib

I added these lines to the Android.mk

LOCAL_PRELINK_MODULE:=false
LOCAL_LDLIBS += -L/usr/local/android-ndk-1.6_r1/apps/hello-jni/project/
jni -ltoto

Now will try with my 'full' lib
Reply all
Reply to author
Forward
0 new messages