Android ndk std :: to_string支持

alb*_*iff 48 c++ android std c++-standard-library android-ndk

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

 error: 'to_string' is not a member of 'std'
Run Code Online (Sandbox Code Playgroud)

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

yus*_*ulx 61

您可以尝试LOCAL_CFLAGS := -std=c++11,但请注意,并非所有C++ 11 API都可用于NDK的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) ;
}
Run Code Online (Sandbox Code Playgroud)

  • 我做了一些没有运气的尝试(LOCAL_CPPFLAGS + = -std = c ++ 11,LOCAL_CPPFLAGS + = -std = gnu + 11等),我也找到了一些答案(http://stackoverflow.com/questions/17950814/how-to-use-stdstoul-and-stdstoull-in-android/18124627#18124627)解释说ndk不支持新的c ++ 11字符串函数,所以最后我按照你的建议实现to_string方法.谢谢 :). (3认同)

thu*_*ove 26

使用NDK r9 +,您可以使用llvm-libc ++,它提供对cpp11的完全支持.

在您的Application.mk中,您必须添加:

APP_STL:=c++_static 
Run Code Online (Sandbox Code Playgroud)

要么

APP_STL:=c++_shared
Run Code Online (Sandbox Code Playgroud)

  • 使用cocos2d-x这可以解决std :: to_string编译问题.但是它引起了下游的其他构建问题:"错误:'pthread_key_t'没有命名类型static pthread_key_t s_threadKey"如果有人尝试这个.我从来没能解决这个问题.正如这里提到的http://stackoverflow.com/questions/22164564/cannot-build-local-shared-library-in-android-ndk更改编译器导致其他编译问题......接受的答案绕过了这个问题术语... (2认同)

kyb*_*kyb 12

摇篮

如果您正在寻找Gradle构建系统的解决方案.看看这个答案.

简短的回答.

添加字符串

arguments "-DANDROID_STL=c++_shared"
Run Code Online (Sandbox Code Playgroud)

在你的build.gradle.喜欢

android {
  ...
  defaultConfig {
    ...
    externalNativeBuild {
      cmake {
        ...
        arguments "-DANDROID_STL=c++_shared"
      }
    }
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)