Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ndk std::to_string support

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.

like image 968
albciff Avatar asked Mar 31 '14 23:03

albciff


4 Answers

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) ;
}
like image 164
yushulx Avatar answered Nov 11 '22 05:11

yushulx


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
like image 42
thursdaysDove Avatar answered Nov 11 '22 03:11

thursdaysDove


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"
      }
    }
  }
  ...
}
like image 13
kyb Avatar answered Nov 11 '22 04:11

kyb


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"
    }
  }
}
like image 1
stephenspann Avatar answered Nov 11 '22 05:11

stephenspann