【问题标题】:#include <iostream> gives error using -std=c++11#include <iostream> 使用 -std=c++11 给出错误
【发布时间】:2016-12-17 04:45:09
【问题描述】:

我在 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\4.9.3\include\c++\cwchar:44,
                 from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\postypes.h:40,
                 from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iosfwd:40,
                 from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\ios:38,
                 from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.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 );
              ^
…

【问题讨论】:

  • 这不应该发生,看起来您的安装已损坏。 (另外,它很旧。)您尝试重新安装吗?
  • 鉴于前两条错误消息,您的安装有问题。在 Unix 系统上,就好像 &lt;sys/types.h&gt; 标头不起作用。认真考虑是否重新安装——这可能是解决问题的最简单方法。否则,您需要跟踪哪个标头没有定义应该定义的 _ino_t_dev_t,以及为什么在使用严格的 C++ 编译时没有定义。这可能是系统中的错误。你能从 GCC 4.9.3 升级到 6.1.0 吗? (GCC 4.9.4 于 2016 年 8 月 3 日发布,作为最后一个 4.9.x 版本。同时发布了 GCC 5.4。)
  • @Baum mit Augen 我会尝试再次运行 mingw 更新,尽管我在尝试编译之前已经这样做了。如果我的安装被破坏了,它肯定永远不会编译吗?我的程序确实可以编译,只是在使用任何 -std=c++xx 参数时无法编译。
  • NathanOliver 建议的 SO 38538924 的一系列问题具有相同的症状。您的源代码没有问题;正如你所说,它是正确的。 MinGW 安装或分发存在某种错误(安装反映了分发的内容,但分发的内容本身有问题)。您可能需要查看 c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\postypes.h 文件以查看问题所在 - 或链中的其他文件或包含的其他文件(尝试 -H 查看标题)。

标签: c++ gcc mingw


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多