Android std and stl support

25,920

From NDK r5's docs/CPLUSPLUS-SUPPORT.html:

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.
gnustl_static -> Use GNU libstdc++ as a static library.

Which NDK are you using? Have you tried compiling one of the sample applications that utilize the STL such as test-libstdc++?

Share:
25,920
Luke
Author by

Luke

Updated on July 13, 2022

Comments

  • Luke
    Luke almost 2 years

    I am playing with android ndk. I am using Window Vista with cygwin (latest version). I compiled and launched the hello world jni sample on my phone. It is working. The code is (is a .cpp file):

    #include <string.h>
    #include <jni.h>
    
    extern "C" {
    JNIEXPORT jstring JNICALL     Java_org_android_helloworld_HelloworldActivity_invokeNativeFunction(JNIEnv* env, jobject     javaThis);
    };
    
    
    jstring Java_org_android_helloworld_HelloworldActivity_invokeNativeFunction(JNIEnv*     env, jobject javaThis)
    {
        return  env->NewStringUTF("Hello from native code!");
    } 
    

    I wanted to add some modifications, just to play with it a bit:

    #include <algorithm>
    

    and then, in the function above, i added:

    int a;
    a=std::min<int>(10, 5);
    

    but the compiler says that it cannot find the file 'algorithm' and that min() is not part of std.

    After a bit of searching, i have found that the android ndk has a gnu-libstdc++ directory with all the std files needed. Reading the NDK docs, i have learned that usint std::* should work without any modification to the code (if one include the proper header files). But it seems that gcc on cygwin is not able to find the needed files.

    What are the steps to do in order to be able to use std and stl within a .cpp file in an android ndk app?

  • Luke
    Luke over 12 years
    I can compile succesfully with gcc under cygwin the sample app test-libstdc++. It seems that gcc can found <cerrno> and <cstddef> header files, but if i include <algorithm> or <vector>, it can't. What's happening?? They are under the same directory!
  • Luke
    Luke over 12 years
    However, i am using the r6 release. I think that gcc uses the files in the directory android-ndk-r6/sources/cxx-stl/system/include. In that directory there are some stdc++ header files, but a lot of them are missing (vector, algorithm and so on)...
  • Luke
    Luke over 12 years
    When i compile the sample, the manifest file can't be found (that is right, since the sample don't have one). Second, he defaulted the APP_PLATFORM to 3. So i added a file in the root of the prj called default.properties with the target set to 8 (gcc read it and switch to target 8). An important thing gcc says is: "Adding import directory: /cygdrive/d/android/android-ndk-r6/sources" and then "Looking for imported module with tag 'cxx-stl/system'". Then:"Probing (..)/system/Android.mk". The last line i want to show is:"Found in (..)/system". After that, gcc tries to compile the .cpp.
  • Luke
    Luke over 12 years
    Ok, i solved it. I was adding APP_STL := stlport_static to the Android.mk of my application instead that to a file named Application.mk (in the /jni directory). I have another question: how can i use pre-compiled stlport lib? I ask this because it recompiles it for every project (that is, if i start a new prj using stl_port, the first time i run ndk-build, it will compile the stl port).