31

I'm working with the Android NDK, and since it does not currently support the STL, I was wondering if there are any brilliant people out there who have had success with this, or know which is better suited for the Android platform: uSTL or STLPort.

EDIT: Looks like another option may be CrystaX .NET.

From their website:

...customized distribution of Android NDK r3 which I have rebuilt from official sources. Support of C++ exceptions, RTTI and Standard C++ Library added.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Andrew Garrison
  • 6,977
  • 11
  • 50
  • 77

6 Answers6

33

Ports of STL are officially available in the Android NDK from version r5 on. It can be used either as a static or shared library. The following implementations are available prebuilt with the NDK :

  • STLport, based on v5.2.0 :
    • static stlport_static : use if you have only one dynamic library in your project.
    • dynamic stlport_shared : to use if you have more than one dynamic library in your project.
  • GNU libstdc++ system (static library)

The recommended, easy way to use it at build time is by defining APP_STL in the Application.mk, like this :

APP_STL := stlport_static

And if you want to rebuild it (this is not necessary), define STLPORT_FORCE_REBUILD in your Application.mk :

STLPORT_FORCE_REBUILD := true

The unit test framework for STLport is also available.

Current limitations for STLport :

  • C++ Exceptions not supported
  • RTTI not supported
  • "Probable bugs" in support for wchar_t and locales

Various Links :

Documentation is available in the NDK packages at the following locations (there may be more) :

  • docsCPLUSPLUS-SUPPORT.html
  • sources/cxx-stl/stlport
  • sources/cxx-stl/gnu-libstdc++

Download NDK + docs here ; file bugs here


Below is an excerpt from docs/CPLUSPLUS-SUPPORT.html (from NDK docs, r5)

III. Selecting the C++ Standard Library Implementation:

By default, the headers and libraries for the minimal C++ runtime system library (/system/lib/libstdc++.so) are used when building C++ sources.

You can however select a different implementation by setting the variable APP_STL to something else in your Application.mk, for example:

APP_STL := stlport_static

To select the static STLport implementation provided with this NDK. Value APP_STL values are the following:

system -> Use the default minimal C++ runtime library. stlport_static -> Use STLport built as a static library. stlport_shared -> Use STLport built as a shared library.

WARNING: IMPORTANT CAVEAT

 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.

WARNING: END OF IMPORTANT CAVEAT

"stlport_shared" is preferred if you have several shared libraries in your project that use the C++ STL, because it avoids duplication of functions and more importantly of global variables (e.g. std::cout) in each one of them, which can have surprising results.

On the other hand, you will have to load it explicitely when starting your application, as in the following example:

 static {
     System.loadLibrary("stlport_shared");
     System.loadLibrary("foo");
     System.loadLibrary("bar");
 }

Where both "libfoo.so" and "libbar.so" depend on "libstlport_shared.so".

Note that the shared library's name if "libstlport_shared.so" to avoid naming conflicts with certain Android system images which include a system-level libstlport.so (which happens to not be ABI-stable and cannot be used from NDK-generated machine code).

"stlport_static" is preferred if you have only one shared library in your project: only the STL functions and variables you actually need will be linked to your machine code, reducing its code size, and you won't need to load the dynamic stlport_shared at startup.

IV. STLport-specific issues:

This NDK provides prebuilt static and shared libraries for STLport, but you can force it to be rebuilt from sources by defining the following in your environment or your Application.mk before building:

STLPORT_FORCE_REBUILD := true

STLport is licensed under a BSD-style open-source license. See sources/cxx-stl/stlport/README for more details about the library.

V. Future Plans:

  • Make STLport compatible with C++ exceptions and RTTI
  • Full GNU libstdc++ support
  • uSTL support?
nullspace
  • 1,279
  • 13
  • 13
Stéphane
  • 6,920
  • 2
  • 43
  • 53
9

Just note that uSTL deviates from the standard quite a bit. For instance, it assumes UTF-8 encoding for std::string. Still looks interesting, though...

Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88
  • 2
    Now that's not really a deviation; the encoding of `char` (and even `wchar_t`) is implementation defined. Choosing UTF-8 on a platform where `cHAR_BIT==8` is not just allowed, it's reasonable. – MSalters Nov 02 '09 at 10:57
  • 2
    Take a look at the actual library code (or even documentation) and you'll see what I mean. – Nemanja Trifunovic Nov 02 '09 at 15:24
  • 7
    The uSTL does deviate in significant ways from the standard. It's really not a stdlib implementation, but an entire reimagining; check out how deque is "implemented": `#define deque list`. –  Nov 23 '09 at 03:25
9

I recently came across some helper scripts and a port of STLport for Android, by John Ripley.

There is also a related blog post with instructions of how to set it up.

I suppose that might make it easier to go with STLport.

Stjepan Rajko
  • 876
  • 8
  • 9
9

STLport supported since Android2.3 now!!!

jsding
  • 106
  • 1
  • 1
  • 1
    It's here: http://developer.android.com/sdk/ndk/index.html -- look for "Provides a default C++ STL implementation (based on STLport) as a helper module..." Really it's that you can use STLPort in NDK r5; it doesn't appear to be guaranteed to ship on devices, even in 2.3. But it is part of the NDK now. – SomeCallMeTim Dec 16 '10 at 06:49
3

this is how I configured STLPort to work with Android Froyo.

// The code
// The set of definitions and includes for STLPort
// They used defined() instead of #ifdef.
#define _STLP_HAS_INCLUDE_NEXT  1
#define _STLP_USE_MALLOC   1
#define _STLP_USE_NO_IOSTREAMS  1
#include <stl/config/_android.h>
#include <map>
#include <string>

// Android.mk
# For Android STL support
LOCAL_C_INCLUDES += external/stlport/stlport
LOCAL_SHARED_LIBRARIES += libstlport

Andrew

Andrew
  • 31
  • 1
0

Note that the git repository mention in the link from Stjepan Rajko's answer no longer exists. Alternative sources are on anddev and, via git, at git://stlport.git.sourceforge.net/gitroot/stlport/stlport. I found the latter at a longer discussion of using both stlport and boost under Android.

Since first answering this question, I have gotten the anddev STLPort to work with my library that also calls boost, including the problematic shared_ptr. For details of my solution see this question.

Community
  • 1
  • 1
Gamma Draconis
  • 1,166
  • 1
  • 9
  • 11