Android ndk std::to_string 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22774009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ndk std::to_string support
提问by albciff
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:
我正在使用 android NDK r9d 和工具链 4.8,但我无法使用 std::to_string 函数,编译器抛出此错误:
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.
android ndk 不支持这个功能吗?我尝试APP_CPPFLAGS := -std=c++11
没有运气。
回答by yushulx
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
).
您可以尝试LOCAL_CFLAGS := -std=c++11
,但请注意,并非所有 C++11 API 都可用于 NDK 的 gnustl。libc++ ( APP_STL := c++_shared
)提供完整的 C++14 支持。
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) ;
}
回答by thursdaysDove
With NDK r9+ you can use llvm-libc++which offers full support for cpp11.
使用 NDK r9+,您可以使用llvm-libc++,它提供对 cpp11 的完全支持。
In your Application.mkyou have to add:
在您的Application.mk 中,您必须添加:
APP_STL:=c++_static
or
或者
APP_STL:=c++_shared
回答by kyb
Gradle
摇篮
If you looking for solution for Gradle build system. Look at this answer.
如果您正在寻找 Gradle 构建系统的解决方案。看看这个答案。
Short answer.
简短的回答。
Add the string
添加字符串
arguments "-DANDROID_STL=c++_shared"
in your build.gradle
. Like
在您的build.gradle
. 喜欢
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
...
arguments "-DANDROID_STL=c++_shared"
}
}
}
...
}
回答by stephenspann
Experimental Gradle Plugin
实验性 Gradle 插件
If you're looking for a solution for the Experimental Gradle plugin, this worked for me...
如果您正在寻找实验 Gradle 插件的解决方案,这对我有用...
Tested with com.android.tools.build:gradle-experimental:0.9.1
用 com.android.tools.build:gradle-experimental:0.9.1 测试
model {
...
android {
...
ndk {
...
stl = "c++_shared"
}
}
}
回答by mortalis
I could not use c++_static
, it gave some errors about undefined exceptions. So I returned to the gnustl_static
.
我无法使用c++_static
,它给出了一些关于未定义异常的错误。于是我回到了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.
但是在 NDK 源代码中sources/cxx-stl/llvm-libc++/src/string.cpp
,我找到了 的实现to_string(int)
并尝试将其复制到我的代码中。经过一些修正后,它起作用了。
So the final piece of code I had:
所以我的最后一段代码:
#include <string>
#include <algorithm>
using namespace std;
template<typename S, typename P, typename V >
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<size_type>(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 <class S, class V, bool = is_floating_point<V>::value>
struct initial_string;
template <class V, bool b>
struct initial_string<string, V, b>
{
string
operator()() const
{
string s;
s.resize(s.capacity());
return s;
}
};
template <class V>
struct initial_string<wstring, V, false>
{
wstring
operator()() const
{
const size_t n = (numeric_limits<unsigned long long>::digits / 3)
+ ((numeric_limits<unsigned long long>::digits % 3) != 0)
+ 1;
wstring s(n, wchar_t());
s.resize(s.capacity());
return s;
}
};
template <class V>
struct initial_string<wstring, V, true>
{
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<string, int>()(), "%d", val);
}
回答by Expert Ngobeni
For Android Studio, add this in build.gradle (Mobile App)
对于 Android Studio,在 build.gradle(移动应用)中添加这个
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments "-DANDROID_STL=c++_static"
}
}