Android ndk std::to_string support

38,773

Solution 1

You can try LOCAL_CFLAGS := -std=c++11, but note that not all C++11 APIs are available with the NDK's gnustl. Full C++14 support is available with libc++ (APP_STL := c++_shared).

The alternative is to implement it yourself.

#include <string>
#include <sstream>

template <typename T>
std::string to_string(T value)
{
    std::ostringstream os ;
    os << value ;
    return os.str() ;
}

int main()
{
    std::string perfect = to_string(5) ;
}

Solution 2

With NDK r9+ you can use llvm-libc++ which offers full support for cpp11.

In your Application.mk you have to add:

APP_STL:=c++_static 

or

APP_STL:=c++_shared

Solution 3

Gradle

If you looking for solution for Gradle build system. Look at this answer.

Short answer.

Add the string

arguments "-DANDROID_STL=c++_shared"

in your build.gradle. Like

android {
  ...
  defaultConfig {
    ...
    externalNativeBuild {
      cmake {
        ...
        arguments "-DANDROID_STL=c++_shared"
      }
    }
  }
  ...
}

Solution 4

Experimental Gradle Plugin

If you're looking for a solution for the Experimental Gradle plugin, this worked for me...

Tested with com.android.tools.build:gradle-experimental:0.9.1

model {
  ...
  android {
    ...
    ndk {
      ...
      stl = "c++_shared"
    }
  }
}
Share:
38,773

Related videos on Youtube

albciff
Author by

albciff

Updated on July 31, 2020

Comments

  • albciff
    albciff almost 4 years

    I'm using android NDK r9d and toolchain 4.8 but I'm not able to use std::to_string function, compiler throws this error:

     error: 'to_string' is not a member of 'std'
    

    Is this function not supported on android ndk? I try APP_CPPFLAGS := -std=c++11 with no luck.

  • albciff
    albciff about 10 years
    I make some tries with no luck (LOCAL_CPPFLAGS += -std=c++11, LOCAL_CPPFLAGS += -std=gnu+11 etc.), and also I found some answers (stackoverflow.com/questions/17950814/…) which explains that ndk not supports new c++11 string function, so finally I implement to_string method as you suggests. Thanks :).
  • Hunter-Orionnoir
    Hunter-Orionnoir about 9 years
    using cocos2d-x this worked to get around the std::to_string compile issue. However it caused other build problems down stream: "error: 'pthread_key_t' does not name a type static pthread_key_t s_threadKey" In case some tries this. I was never able to solve that. As mentioned here stackoverflow.com/questions/22164564/… changing compilers led to other compile issues... The accepted answer got around the issue in the short term...
  • albciff
    albciff about 9 years
    @Hunter I didn't mention that on my question however I was using cocos2d, and the accepted answer is simply an clean and as you said got around the issue so this is why I did accept it :)
  • Hunter-Orionnoir
    Hunter-Orionnoir about 9 years
    Yes I too went with the accepted answer, I was not able to figure out exactly how to use this approach correctly. I was missing other threading libs. Probably could have done it if I spent more time... I just wanted that specific error here so if I ever run into this again I will have a hit right away :)
  • Kyone
    Kyone almost 9 years
    I was looking for this one for hours, so thank you thursdaysDove!
  • Jonny
    Jonny almost 9 years
    I also get other errors down stream with this answer.
  • Jeet
    Jeet about 8 years
    @thursdaysDove, +1. Working fine when the flag added to the application.mk file. As expected i am able to compile from the terminal window using ndk-build. But i am not able to do this from eclipse (enable with android). There i am getting error on the to_string function. Do you have any idea which file need to be included?
  • void.pointer
    void.pointer almost 8 years
    This breaks ABI compatibility with any static libraries compiled (e.g. third party libs). So you'll have to rebuild them again. Would be nice to avoid that.
  • Onur Tuna
    Onur Tuna almost 8 years
    I have tried it but gives error on std::ostringstream. The issue might be about Android Studio 2.2 alpha 6. Is there any easy way to convert?
  • Wolf
    Wolf over 6 years
    Extremely helpful all for compilers that don't support C++11. BTW: Is there a way (a macro definition) to check if C++11 is present, to skip this?
  • Wolf
    Wolf over 6 years
    I just found the char template specializations missing, you need 3 of them, for: char, signed char, unsigned char
  • Om Infowave Developers
    Om Infowave Developers over 6 years
    I have used the same function but application get crash for some devices with below log libloglib-jni.so 0xdd7bdacf std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_ostringstream() (basic_ios.h:276) 36 libart.so 0xf4832812 (Missing) 37 libloglib-jni.so 0xdd7a5851 std::string patch::to_string<int>(int const&) can you please provide any solution if you have.
  • Michael IV
    Michael IV about 6 years
    Actually this is the only valid answer ,given that today's native development for Android is mostly done with Android Studio which uses Gradle.
  • Dan Albert
    Dan Albert almost 6 years
    I've updated the answer to account for modern NDKs, and also clarified the situations where the C++11 APIs were missing.
  • Dmytro
    Dmytro almost 5 years
    Better to pass value by const reference: std::string to_string(const T& value)