Android ndk std::to_string support

albciff picture albciff · Apr 1, 2014 · Viewed 36.8k times · Source

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.

Answer

yushulx picture yushulx · Apr 1, 2014

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) ;
}