编译通过build_native cocos2d-x。py返回:在此范围中没有声明'to_string'

时间:2023-01-03 06:58:00

I am trying to build a cocos2d-x 3.0 (stable) project for Android via the build_native.py script but it hangs when a class is using the std::to_string (or the std::stoi) function. Building the project under Xcode gives no problem at all, it's just the command line compilation that fails.

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

I am already importing <string> in all the classes that make use of those functions, but with no success. I also modified the Application.mk file like this:

我已经在所有使用这些函数的类中导入了 ,但是没有成功。我还修改了应用程序。可这样的文件:

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

adding the -std=c++11 flag to make sure the project is compiled using the C++11 version.

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

Is there anything else I should do here?

我还需要做什么吗?

More

Following this thread I decided to include this:

按照这个思路,我决定包括以下内容:

#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

in my headers, since I'm just using to_string with integer inputs. It is not a good solution, but works fine ... but then the compiler hangs when it finds the stoi function, again.

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

3 个解决方案

#1


1  

I ended up using this piece of code:

最后我用了这段代码:

#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

#2


0  

Try using atoi instead of stoi. Altough atoi returns zero on error, but it works with command line compilation

尝试使用atoi而不是stoi。Altough atoi在错误时返回零,但是它可以使用命令行编译

#3


0  

you can do int to str conversion process by using sstream library, its some long but it works :

您可以使用sstream库进行int到str转换过程,它有些长,但它可以工作:

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

#1


1  

I ended up using this piece of code:

最后我用了这段代码:

#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

#2


0  

Try using atoi instead of stoi. Altough atoi returns zero on error, but it works with command line compilation

尝试使用atoi而不是stoi。Altough atoi在错误时返回零,但是它可以使用命令行编译

#3


0  

you can do int to str conversion process by using sstream library, its some long but it works :

您可以使用sstream库进行int到str转换过程,它有些长,但它可以工作:

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