NDK r5b: std::string link errors with gnustl and stlport

3,208 views
Skip to first unread message

Olivier Guilyardi

unread,
May 2, 2011, 10:46:08 AM5/2/11
to andro...@googlegroups.com
Hi,

I'm getting link errors, using ndk-build from the ND5 r5b, whether I set APP_STL
to gnustl_static or stlport_static in Application.mk.

I'm just doing "string s; s += "aa";", in a static library, which is being
linked into the main shared library. All this is performed with Android.mk
files, I'm not playing with compiler flags, etc..

Attached are the logs of the link errors.

Anyone know what's going on?

TIA

--
Olivier

stlport.txt
gnustl.txt

Olivier Guilyardi

unread,
May 2, 2011, 11:09:44 AM5/2/11
to andro...@googlegroups.com
Actually, I'm also using a std::list, which seems to cause some of the link
errors. Here's the test code:

string s;
s += "aa";

list<string> l;
l.push_back(s);

Olivier

Jocelyn Houle

unread,
May 2, 2011, 11:45:06 AM5/2/11
to andro...@googlegroups.com
[snip]
> /home/olivier/local/android-ndk/sources/cxx-stl/stlport/stlport/stl/_string.c:600: undefined reference to `std::__stl_throw_length_error(char const*)'

Sounds like an exception situation.

From the NDK documentation:
---
AT THE MOMENT, OUR STLPORT IMPLEMENTATION DOES NOT SUPPORT EXCEPTIONS
AND RTTI. PLEASE BE SURE TO NOT USE -fexceptions OR -frtti IN ALL
MODULES THAT USE IT.

IF YOU NEED THESE, PLEASE USE "gnustl_static".
---

But I have to admit I tried it out in a quick test, and had some weird trouble getting basic_string<> routines found.
It sounds like wiping my 'obj' directory fixed things up (sounds like 'ndk-build clean' wasn't enough for me). The 'libs' directory is another one that might be a culprit (I wish I could reproduce it to give a clearer answer...).

Olivier Guilyardi

unread,
May 2, 2011, 12:06:00 PM5/2/11
to andro...@googlegroups.com
On 05/02/2011 05:45 PM, Jocelyn Houle wrote:

> [snip]
>> /home/olivier/local/android-ndk/sources/cxx-stl/stlport/stlport/stl/_string.c:600: undefined reference to `std::__stl_throw_length_error(char const*)'
>
> Sounds like an exception situation.
>
>>From the NDK documentation:
> ---
> AT THE MOMENT, OUR STLPORT IMPLEMENTATION DOES NOT SUPPORT EXCEPTIONS
> AND RTTI. PLEASE BE SURE TO NOT USE -fexceptions OR -frtti IN ALL
> MODULES THAT USE IT.

I didn't catch that it could render stlport string and list unusable... Is it
really the case?

> IF YOU NEED THESE, PLEASE USE "gnustl_static".

Using gnustl_static and setting LOCAL_CPPFLAGS to -fexceptions and/or -frtti
doesn't help.

> ---
>
> But I have to admit I tried it out in a quick test, and had some weird trouble getting basic_string<> routines found.
> It sounds like wiping my 'obj' directory fixed things up (sounds like 'ndk-build clean' wasn't enough for me). The 'libs' directory is another one that might be a culprit (I wish I could reproduce it to give a clearer answer...).

$ ndk-build clean
$ rm -rf libs
$ rm -rf obj

Doesn't help.

--
Olivier

Jocelyn Houle

unread,
May 2, 2011, 12:58:02 PM5/2/11
to andro...@googlegroups.com, Olivier Guilyardi
Attached is a simple project showing that C++ strings can work properly.
hello-jni-cpp.tar.bz2

Olivier Guilyardi

unread,
May 2, 2011, 2:05:31 PM5/2/11
to andro...@googlegroups.com
Okay, got it. It's a bit subtle. Attached is a patch which causes the link error
to occur.

What happens is that if a shared library is in C only, and links to a static
library which contains some C++ files, then libstdc++.a is not included during
link time although it should.

In my real project, adding an empty test.cpp in the shared library does the trick.

I think it's a bug in the NDK "heuristics".

Olivier

On 05/02/2011 06:58 PM, Jocelyn Houle wrote:
> Attached is a simple project showing that C++ strings can work properly.
>
>

> ------------------------------------------------------------------------
>
>
>
> To run, the following should work:
> shell> android update project --target 2 -p .
> shell> ndk-build && ant install && ndk-gdb --start --force
>
>
> Hopefully, you will be able to figure things out on your own...
>
>
> On 2011-05-02, at 12:28, Olivier Guilyardi wrote:
>
>> No, sorry, I can't send you my project.
>>
>> Please, let's discuss on-list.
>>
>> Thanks for your help
>> Olivier
>>
>> On 05/02/2011 06:15 PM, Jocelyn Houle wrote:
>>> [Replying off-list...]
>>>
>>> Can you send me your project so that I can try it on my side?
>>>
>>> I've successfully compiled the following C++ source:
>>> ---
>>> #include <cstdio>
>>> #include <string>
>>> #include <list>
>>>
>>> using namespace std;
>>>
>>> int main( int argc, char** argv )
>>> {


>>> string s;
>>> s += "aa";

>>> list<string> l;
>>> l.push_back(s);
>>> printf("String: %s\n", l.back().c_str());
>>> return 0;
>>> }
>>> ---
>>> so I suspect it's something related to your setup somewhere... But I have no clue what it could be.
>>>
>>> I'm just convinced it *can* work... ;-)
>>>
>>> BWT, what platform are you running on? Win+MinGW? Linux? OSX?

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

hello-jni-cpp-link-error.diff

Jocelyn Houle

unread,
May 2, 2011, 2:57:29 PM5/2/11
to andro...@googlegroups.com
> I think it's a bug in the NDK "heuristics".

Indeed. Looking at $NDK/build/core/definitions.mk@474:

module-has-c++ = $(strip $(call module-get-c++-sources,$1))

I'd say it would have to check for C++ usage in the dependencies as well (like LOCAL_STATIC_LIBRARIES in your modified example).

Also, introducing a cpp file seems to be the only workaround possible with the current version.

Nice catch. Do you intend to report it?

Olivier Guilyardi

unread,
May 2, 2011, 3:43:10 PM5/2/11
to andro...@googlegroups.com

Thanks! I will try and report it later unless you do.

--
Olivier

Olivier Guilyardi

unread,
May 6, 2011, 6:46:59 AM5/6/11
to andro...@googlegroups.com

I just reported this bug in issue #16627:
http://code.google.com/p/android/issues/detail?id=16627

--
Olivier

Reply all
Reply to author
Forward
0 new messages