通过build_native.py返回编译 cocos2d-x:"to_string"未在此范围内声明



我试图通过build_native.py脚本为Android构建一个cocos2d-x 3.0(稳定)项目,但当一个类使用std::to_string(或std::stoi)函数时,它会挂起。在Xcode下构建项目完全没有问题,只是命令行编译失败了。

我已经在所有使用这些函数的类中导入了<string>,但是没有成功。我还像这样修改了Application.mk文件:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=0 -std=c++11 -Wno-literal-suffix -fsigned-char

添加-std=c++11标志,以确保使用c++ 11版本编译项目。

我还需要做些什么吗?

在这个线程之后,我决定包括这个:

#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
string to_string(int t) {
    ostringstream os;
    os << t;
    return os.str();
}
#endif

在我的头,因为我只是使用to_string与整数输入。这不是一个好的解决方案,但效果很好……但是当它找到stoi函数时,编译器会挂起。

我最终使用了这段代码:

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
string to_string(int t) {
  ostringstream os;
  os << t;
  return os.str();
}
int stoi(const string myString) {
  return atoi(myString.c_str());
}
#endif

尝试使用atoi代替stoi。虽然atoi在错误时返回零,但它可以与命令行编译一起工作

你可以通过使用sstream库来完成int到STR的转换过程,它有些长,但它有效:

#include <sstream>
std::stringstream myStringStream ; 
myStringStream << myInteger;
myString = myStringStream.str();

最新更新