【问题标题】:Can't compile Hello World with "-std=c++11"无法使用“-std=c++11”编译 Hello World
【发布时间】:2018-09-12 16:40:00
【问题描述】:

我的 g++ 编译器似乎有问题。它无法在带有 g++ 5.4.0 的 Ubuntu 16.04 上使用“-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 function ‘constexpr _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 安装在我的系统上,这不是我在这台 PC 上使用“-std=c++11”的第一个 cpp 程序(以前可以正常工作)。

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

谢谢!

【问题讨论】:

  • 尝试-std=gnu++11 代替-std=c++11
  • 这些是您遇到的第一个错误吗?如果不是,请编辑您的问题以仅包含前几个错误,因为这些错误很可能是最相关的。
  • GCC 5 需要 libstdc++ 6。你安装了哪些版本?
  • 为我工作(相同的编译器,相同的操作系统)...
  • @VictorGubin:不可能发布这么大的错误。

标签: c++ c++11 ubuntu g++


【解决方案1】:

您对计算机的包依赖项做了一些奇怪的事情。

GCC 5 需要 libstdc++ 6,但您有 libstdc++ 5(如路径所示)。

这意味着您的编译器和标准库实现不同步。

重新安装您的工具链。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2021-02-21
    • 2014-11-29
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    相关资源
    最近更新 更多