android ndk std::string,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.

回答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

#include

template

std::string to_string(T value)

{

std::ostringstream os ;

os << value ;

return os.str() ;

}

int main()

{

std::string perfect = to_string(5) ;

}

回答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

回答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"

}

}

}

...

}

回答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"

}

}

}

回答5:

I could not use c++_static, it gave some errors about undefined exceptions. So I returned to the gnustl_static.

But in NDK sources, in sources/cxx-stl/llvm-libc++/src/string.cpp, I found implementation of to_string(int) and tried to copy it to my code. After some corrections it worked.

So the final piece of code I had:

#include

#include

using namespace std;

template

inline

S

as_string(P sprintf_like, S s, const typename S::value_type* fmt, V a)

{

typedef typename S::size_type size_type;

size_type available = s.size();

while (true)

{

int status = sprintf_like(&s[0], available + 1, fmt, a);

if ( status >= 0 )

{

size_type used = static_cast(status);

if ( used <= available )

{

s.resize( used );

break;

}

available = used; // Assume this is advice of how much space we need.

}

else

available = available * 2 + 1;

s.resize(available);

}

return s;

}

template ::value>

struct initial_string;

template

struct initial_string

{

string

operator()() const

{

string s;

s.resize(s.capacity());

return s;

}

};

template

struct initial_string

{

wstring

operator()() const

{

const size_t n = (numeric_limits::digits / 3)

+ ((numeric_limits::digits % 3) != 0)

+ 1;

wstring s(n, wchar_t());

s.resize(s.capacity());

return s;

}

};

template

struct initial_string

{

wstring

operator()() const

{

wstring s(20, wchar_t());

s.resize(s.capacity());

return s;

}

};

string to_string(int val)

{

return as_string(snprintf, initial_string()(), "%d", val);

}

回答6:

For Android Studio, add this in build.gradle (Mobile App)

externalNativeBuild {

cmake {

cppFlags "-std=c++11"

arguments "-DANDROID_STL=c++_static"

}

}

来源:https://stackoverflow.com/questions/22774009/android-ndk-stdto-string-support

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值