#include <iostream> 使用 -std=c++11 给出错误

#include <iostream> gives error using -std=c++11

本文关键字:c++11 出错 错误 使用 lt iostream gt #include -std      更新时间:2023-10-16

我在Windows上使用MinGW的gcc编译器。版本为4.9.3。当使用-std=c++98、-std=c++03或-std=c++11作为参数时,下面的代码会给出错误。

#include <iostream>
int main()
{
    std::cout << "Hello world!" << std::endl;
    return 0;
}

当-std=gnu++98, -std=gnu++03或std=gnu++11作为参数时,代码编译没有错误。此外,当不使用c++版本参数(g++ test.cpp -c)时,代码不会编译错误

进一步调查后,我发现这是#include引起的问题。以下代码在使用std=c++参数时不会产生任何错误:

int main()
{
    return 0;
}

然而,当寻找其他东西来测试我的代码时,下面的工作:

#include <cmath>
int main()
{
    return 0;
}

但这不是:

#include <string>
int main()
{
    return 0;
}

怎么回事?通过对gnu++的简短搜索,它说它提供了额外的扩展,但是像上面这样简单的代码肯定不应该依赖于任何扩展?

我已经粘贴了用g++ test.cpp编译第一段代码时发生的大错误-c -std=c++11。http://pastebin.com/k0RLtWQz

第一个消息是:

$ g++ test.cpp -c -std=c++11
In file included from c:mingwincludewchar.h:208:0,
                 from c:mingwlibgccmingw324.9.3includec++cwchar:44,
                 from c:mingwlibgccmingw324.9.3includec++bitspostypes.h:40,
                 from c:mingwlibgccmingw324.9.3includec++iosfwd:40,
                 from c:mingwlibgccmingw324.9.3includec++ios:38,
                 from c:mingwlibgccmingw324.9.3includec++ostream:38,
                 from c:mingwlibgccmingw324.9.3includec++iostream:39,
                 from test.cpp:1:
c:mingwincludesys/stat.h:173:14: error: '_dev_t' does not name a type
 struct _stat __struct_stat_defined( _off_t, time_t );
              ^
c:mingwincludesys/stat.h:173:14: error: '_ino_t' does not name a type
 struct _stat __struct_stat_defined( _off_t, time_t );
              ^
…

通过更改为mingw64(也使用较新版本的gcc)解决。似乎问题出在我的mingw32安装或发行版上(正如Jonathan Leffler指出的)。所有-std=c++xx参数现在都可以工作了