g++ 中不存在 std::stoi 4.6.1 关于 MinGW
我尝试在 IdeOne(使用 gcc 4.5.1)和我的 Linux 计算机(使用 4.6.4 之类的东西)上编译这个简单的程序:
#include <string>
#include <iostream>
int main() {
std::cout << std::stoi("32") << std::endl;
}
它完美编译并输出 32
。但是,当我尝试使用 MinGW 和 gcc 4.6.1 在 Windows 计算机上编译它时,出现此错误:
test.cpp: In function 'int main()':
test.cpp:5:19: error: 'stoi' is not a member of 'std'
std::stoul
等也会发生同样的情况。 std::由于某种原因,stoi
和 family 不存在于 MinGW 中?我认为 MinGW (sh|w) 上的 gcc 的行为与 Linux 上的行为相同。
I tried compiling this simple program on IdeOne (which uses gcc 4.5.1) and on my Linux computer (which uses something like 4.6.4):
#include <string>
#include <iostream>
int main() {
std::cout << std::stoi("32") << std::endl;
}
And it compiles perfectly and outputs 32
. However, when I try to compile it on my windows computer with MinGW and gcc 4.6.1, I get this error:
test.cpp: In function 'int main()':
test.cpp:5:19: error: 'stoi' is not a member of 'std'
The same happens with std::stoul
, etc. Does std::stoi
and family not exist in MinGW for some reason? I thought gcc on MinGW (sh|w)ould behave the same as on Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 Windows 上
vswprintf
非标准声明的结果。 GNU 标准库在此平台上定义了_GLIBCXX_HAVE_BROKEN_VSWPRINTF
,这反过来会禁用您尝试使用的转换函数。您可以在此处阅读有关此问题和宏的更多信息:http://gcc.gnu。 org/bugzilla/show_bug.cgi?id=37522。如果您愿意修改随 MinGW 分发的头文件,则可以通过删除
.../lib 第 2754 行上的
,并将其添加回第 2905 行左右2965(引用 std::vswprintf 的行)。您将无法使用!define(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
宏来解决此问题/gcc/mingw32/4.6.1/include/c++/bits/basic_string.hstd::to_wstring
函数,但许多其他转换函数应该可用。This is a result of a non-standard declaration of
vswprintf
on Windows. The GNU Standard Library defines_GLIBCXX_HAVE_BROKEN_VSWPRINTF
on this platform, which in turn disables the conversion functions you're attempting to use. You can read more about this issue and macro here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.If you're willing to modify the header files distributed with MinGW, you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
macro on line 2754 of.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h
, and adding it back around lines 2905 to 2965 (the lines that referencestd::vswprintf
). You won't be able to use thestd::to_wstring
functions, but many of the other conversion functions should be available.这个问题在 MinGW-w64 中得到了修复,它是原始 MinGW 项目的一个分支,实际上对修复像这样的错误。它从 g++ 4.9.2 甚至更早的版本开始得到修复。
注意:对于已经完成 CodeBlocks 默认安装(随旧的、损坏的 MinGW 一起提供)并想要升级编译器的人,
您可以使用 MinGW-w64 的任何版本:我使用 mingw-builds.org 的自安装程序,而该答案使用 TDM-GCC-64。如果您想要 64 位和 32 位编译,则需要安装并添加 2 个新编译器:mingw-w64 64 位和 mingw-w64 32 位。它不支持使用一次安装的 g++ 并通过
-m32
或-m64
开关进行切换。This is fixed in MinGW-w64, a fork of the original MinGW project that actually is interested in fixing bugs like this. It was fixed as of g++ 4.9.2, and maybe earlier.
Note: for people coming here who have done a default install of CodeBlocks (which comes with the old, broken MinGW), and want to upgrade the compiler, see this answer.
You can use any build of MinGW-w64: I use the self-installer from mingw-builds.org, whereas that answer uses TDM-GCC-64. If you want both 64bit and 32bit compilation you need to install and add 2 new compilers: mingw-w64 64-bit, and mingw-w64 32-bit. It does NOT support using one installation of g++ with the
-m32
or-m64
switch to toggle.我正在使用 MinGW 4.9.3-1。这个问题似乎仍然存在。
作为解决方法,我使用了另一种从字符串中获取整数的方法。
I am using MinGW 4.9.3-1. This problem seems to be still there.
As a workaround, I used the another way of getting integers from strings.
使用 Mingw-w64。我遇到了同样的问题,使用 Mingw-w64 对我有用。
Use Mingw-w64. I had this same issue and using Mingw-w64 worked for me.