Android ndk std :: to_string支持

时间:2014-03-31 23:18:02

标签: android c++ android-ndk std c++-standard-library

我正在使用android NDK r9d和toolchain 4.8但我无法使用std :: to_string函数,编译器抛出此错误:

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

android ndk不支持此功能吗?我没有运气就试试APP_CPPFLAGS := -std=c++11

6 个答案:

答案 0 :(得分:60)

您可以尝试LOCAL_CFLAGS := -std=c++11,但请注意not all C++11 APIs are available with the NDK's gnustl。 libc ++(APP_STL := c++_shared)提供了完整的C ++ 14支持。

另一种方法是自己实施。

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

答案 1 :(得分:26)

使用NDK r9 +,您可以使用llvm-libc++,它可以完全支持cpp11。

Application.mk中,您必须添加:

APP_STL:=c++_static 

APP_STL:=c++_shared

答案 2 :(得分:10)

摇篮

如果您正在寻找Gradle构建系统的解决方案。请看this answer

简短回答。

添加字符串

arguments "-DANDROID_STL=c++_shared"

build.gradle中。像

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

答案 3 :(得分:1)

实验性Gradle插件

如果您正在寻找Experimental Gradle插件的解决方案,这对我有用......

使用com.android.tools.build:gradle-experimental:0.9.1

进行测试
model {
  ...
  android {
    ...
    ndk {
      ...
      stl = "c++_shared"
    }
  }
}

答案 4 :(得分:0)

我无法使用c++_static,它给出了一些有关未定义异常的错误。所以我回到了gnustl_static

但是在NDK来源中,在sources/cxx-stl/llvm-libc++/src/string.cpp中,我发现to_string(int)的实现并尝试将其复制到我的代码中。经过一些修正后,它起作用了。

所以我有最后一段代码:

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

答案 5 :(得分:0)

对于Android Studio,将其添加到build.gradle(移动应用程序)

externalNativeBuild {
    cmake {
        cppFlags "-std=c++11"

        arguments "-DANDROID_STL=c++_static"
    }
}
相关问题