无法使用“-std=c++11”编译Hello World

3

我的g++编译器似乎有问题。在Ubuntu 16.04上,使用g++ 5.4.0时,它无法使用“-std=c++11”选项来编译代码。

因此,我写了一个非常简单的“Hello World”程序:

//Hello.cpp
#include <iostream>

using namespace std;

int main(){
    cout<<"Hello World"<<endl;
}

如果我使用g++编译它:

g++ Hello.cpp

它可以正常工作。
但是如果我添加了“-std=c++11”:

g++ -std=c++11 Hello.cpp

g++会给我大量的错误信息,超过1000行。
以下是其中的一些内容:
In file included from /usr/include/c++/5/bits/stl_pair.h:59:0,
             from /usr/include/c++/5/bits/stl_algobase.h:64,
             from /usr/include/c++/5/bits/char_traits.h:39,
             from /usr/include/c++/5/ios:40,
             from /usr/include/c++/5/ostream:38,
             from /usr/include/c++/5/iostream:39,
             from Hello.cpp:1:
/usr/include/c++/5/bits/move.h:76:27: error: ‘remove_reference’ in namespace ‘std’ does not name a template type
 forward(typename std::remove_reference<_Tp>::type& __t) noexcept
                       ^
/usr/include/c++/5/bits/move.h:76:43: error: expected ‘,’ or ‘...’ before ‘<’ token
 forward(typename std::remove_reference<_Tp>::type& __t) noexcept
                                       ^
/usr/include/c++/5/bits/move.h: In functionconstexpr _Tp&& std::forward(int)’:
/usr/include/c++/5/bits/move.h:77:33: error: ‘__t’ was not declared in this scope
 { return static_cast<_Tp&&>(__t); }
                             ^

同样的代码在其他电脑上可以正常运行。

你有什么想法吗?

上面的错误信息不是第一行,第一行是:

In file included from 
/usr/include/c++/5/experimental/type_traits:39:0,
             from /usr/include/c++/5/bits/move.h:57,
             from /usr/include/c++/5/bits/stl_pair.h:59,
             from /usr/include/c++/5/bits/stl_algobase.h:64,
             from /usr/include/c++/5/bits/char_traits.h:39,
             from /usr/include/c++/5/ios:40,
             from /usr/include/c++/5/ostream:38,
             from /usr/include/c++/5/iostream:39,
             from Hello.cc:1:
/usr/include/c++/5/bits/c++14_warning.h:32:2: error: #error This file requires compiler and library support for the forthcoming ISO C++ 2014 standard. 
This support is currently experimental, and must be enabled with the -std=c++1y or -std=gnu++1y compiler options.

即使使用了“-std=c++1y”或“-std=gnu++1y”,g++仍然给我大量的错误信息。

我的系统上安装了libstdc++6,并且这不是我在这台电脑上第一个使用“-std=c++11”的cpp程序(之前都能正常工作)。

我不记得我修改了什么,但我会尝试重新安装我的工具链。

谢谢!


请尝试使用-std=gnu++11代替-std=c++11 - Victor Gubin
4
这是你收到的第一个错误吗?如果不是,请编辑你的问题,只包括前几个错误,因为它们很可能是最相关的。 - Some programmer dude
这个static_cast<_Tp&&>(__t)表示移动语义没有起作用,即iostream头文件没有使用std::movestd::forward。所以我认为C++ 11模式根本无法工作,这可能是GCC的一个bug。使用 -std=gnu++11应该可以解决。 - Victor Gubin
2
GCC 5需要libstdc++ 6。您安装了哪些版本? - rustyx
1
对我来说可以运行(同样的编译器,同样的操作系统)... - moooeeeep
6
没有任何理由会发现这样程度的漏洞并且被发布。 - Lightness Races in Orbit
1个回答

9
你的电脑软件包依赖关系出现了问题。
GCC 5 需要 libstdc++ 6,但你安装的是 libstdc++ 5(路径显示),这意味着你的编译器和标准库实现不同步。
重新安装你的工具链。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接