NDK with STL and shared library issue

76 views
Skip to first unread message

C++ RTMP Server

unread,
May 2, 2010, 1:39:54 PM5/2/10
to andro...@googlegroups.com
Hello list,

I'm trying to make the NDK working with STL and shared libraries. For this purpose I'm using the custom made NDK from here:

http://www.crystax.net/android/ndk-r3.php

(Very nice job!!!)

However, I have few problems. I want to be able to use shared libraries in combination with STL.
For this purpose I created a library exporting 2 functions:

string sample_function_working()
{
string result="Hello world from shared lib!!!";
return result;
}

string sample_function_not_working()
{
string result="";
return result;
}

And an executable calling those 2 functions:

int main(void)
{
string test=sample_function_working();
printf("Calling sample_function_working... ");
printf("returned `%s`\n",test.c_str());
test=sample_function_not_working();
printf("Calling sample_function_not_working... ");
printf("returned `%s`\n",test.c_str());
return 0;
}


If I compile the library as static library, all works as expected:

$ adb shell /data/executable
Calling sample_function_working... returned `Hello world from shared lib!!!`
Calling sample_function_not_working... returned ``

However, if I compile the library as a dynamic library, I get a segmentation fault:

$ adb shell /data/executable
Calling sample_function_working... returned `Hello world from shared lib!!!`
Calling sample_function_not_working... returned ``
[1] Segmentation fault /data/executable

Also executed with strace and put the result here:
http://pastebin.com/8FbL22L3

The problem is with the sample_function_not_working function. Somehow, android gets confused by returning an empty string.

If you want the complete project to test it, you can find it here:

http://dl.dropbox.com/u/2918563/stests.zip

Cheers,
Andrei

------
Eugen-Andrei Gavriloaie
Web: http://www.rtmpd.com

alan

unread,
May 4, 2010, 11:47:22 AM5/4/10
to android-ndk
I think what is happening is that when you compile into two dynamic
libraries both libraries have a version of the run time library
statically compiled in. This means that the string class in one
library is not the same as the string class in the other library which
means that you cant pass a string from one library to the other.
When you compile the library as static there is only one copy of the
run time library so everything works
>  smime.p7s
> 5KViewDownload

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

Dmitry Moskalchuk

unread,
May 4, 2010, 3:36:47 PM5/4/10
to andro...@googlegroups.com
Yes, probably you are right. I'll look if I could turn out
libstdc++/libsupc++ to be shared library. This is the only way to deal
with shared linking (as in case of topic starter).

Dmitry Moskalchuk


04.05.2010 19:47, alan пишет:

C++ RTMP Server

unread,
May 4, 2010, 4:22:52 PM5/4/10
to andro...@googlegroups.com
You might be right. But this doesn't explain why sample_function_working is working while sample_function_not_working is crashing. Maybe I'm missing something.

Anyways, Dmitry, thank you so much for the time and effort invested into that custom NDK!

Cheers,
Andrei

Reply all
Reply to author
Forward
0 new messages