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

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

我在 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:\mingw\include\wchar.h:208:0,
                 from c:\mingw\lib\gcc\mingw32.9.3\include\c++\cwchar:44,
                 from c:\mingw\lib\gcc\mingw32.9.3\include\c++\bits\postypes.h:40,
                 from c:\mingw\lib\gcc\mingw32.9.3\include\c++\iosfwd:40,
                 from c:\mingw\lib\gcc\mingw32.9.3\include\c++\ios:38,
                 from c:\mingw\lib\gcc\mingw32.9.3\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32.9.3\include\c++\iostream:39,
                 from test.cpp:1:
c:\mingw\include\sys/stat.h:173:14: error: '_dev_t' does not name a type
 struct _stat __struct_stat_defined( _off_t, time_t );
              ^
c:\mingw\include\sys/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 参数现在都有效。