error: ‘to_string’ is not a member of ‘std’———已解决

现象¶

cocos2d-x 2.2.6 项目的源码中使用了 std::to_string() 方法,使用 NDK r9d 编译的时候,报如下错误:

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

Application.mk 文件的部分内容如下:

APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION := 4.8
APP_CPPFLAGS := -frtti -std=c++11 -fexceptions -Wno-error=format-security -Wno-literal-suffix -Wno-deprecated-declarations -fsigned-char -Os $(CPPFLAGS)

换用 NDK 10e ,错误依然。

快速解决¶

想快速解决这个问题,可以自己写一个 std::to_string() 替换标准库中的。

首先写一个 stdtostring.h 文件:

#ifndef STDTOSTRING_H
#define STDTOSTRING_H
#include <string>
#include <sstream>

using namespace std;
namespace std
{
    template < typename T > std::string to_string( const T& n )
    {
        std::ostringstream stm ;
        stm << n ;
        return stm.str() ;
    }
}
#endif

然后在需要使用 std::to_stirng() 方法的源文件中包含它:

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "stdtostring.h"
#endif

追根溯源¶

Why gnustl_static 中介绍了采用 c++_static 和 clang 编译器来实现std::to_string() 支持,于是我尝试了下:

# APP_STL := gnustl_static
# NDK_TOOLCHAIN_VERSION := 4.8
APP_STL := c++_static
NDK_TOOLCHAIN_VERSION := clang

根据 C++ Library Support 的说明, c++_static 对应的是 The LLVM libc++ runtime (static) ,因此应该采用 clang 的工具链进行编译。

结果是: std::to_string() 编译正常,但出现下面的错误:

In file included from src/Utilities.cpp:1:0:
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1498:46: error: expected unqualified-id before 'float'
inline _LIBCPP_INLINE_VISIBILITY float       remainder(float __x, float __y) _NOEXCEPT             {return remainderf(__x, __y);}
                                             ^
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1498:46: error: expected ')' before 'float'
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1499:46: error: expected unqualified-id before 'long'
inline _LIBCPP_INLINE_VISIBILITY long double remainder(long double __x, long double __y) _NOEXCEPT {return remainderl(__x, __y);}
                                             ^
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1499:46: error: expected ')' before 'long'
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1509:1: error: expected ')' before '__x'
remainder(_A1 __x, _A2 __y) _NOEXCEPT
^
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1559:46: error: expected unqualified-id before 'float'
inline _LIBCPP_INLINE_VISIBILITY float       round(float __x) _NOEXCEPT       {return roundf(__x);}
                                             ^
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1559:46: error: expected ')' before 'float'
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1560:46: error: expected unqualified-id before 'long'
inline _LIBCPP_INLINE_VISIBILITY long double round(long double __x) _NOEXCEPT {return roundl(__x);}
                                             ^
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1560:46: error: expected ')' before 'long'
android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1565:1: error: expected ')' before '__x'
round(_A1 __x) _NOEXCEPT {return round((double)__x);}

minggo 在 Cocos can’t compile Android with APP_STL := c++_static 中提到:

A prebuilt version can only support one type of c++ lib. Did you mean we should provide two kinds of prebuilt libcocos2d that using two different c++ libs?

I don’t know if cocos2d can compile with c++_static or not. I don’t remember if there is a good reason to choose one instead of other one. But cocos2d-x uses gnustl_static from very early version, so we know it works well. You can replace it with c++_static if you like, but it is not fully tested.

因此我怀疑在 cocos2d-x 的旧版本中, c++_static 是没有经过完整测试的。

也有人说 在新版本(>3.7)中, gnustl_static 会导致 lambda 和std::to_string 不能使用,已经切换到使用 c++_static 和 clange 组合。

I never made it to work with gnustl_static and gcc with all the c++11 features. I was getting crashes in simple lambdas, as mentioned std::to_string was missing and much more, so i’ve switched to c++_static and clang long time ago.

本文转自:http://www.tuicool.com/articles/RBBjMz

posted @ 2016-05-21 01:57  Anzhongliu  阅读(1627)  评论(0编辑  收藏  举报