【问题标题】:Compiling on linux with c++ standard libraries使用 c++ 标准库在 linux 上编译
【发布时间】:2016-12-30 04:26:30
【问题描述】:

您好有以下示例代码:

func.h - 函数的头文件

#include <vector>
#include <tuple>
using std::vector;
using std::tuple;
tuple <double,double> A(vector<int>& n);

func.cpp - 函数 cpp 文件

#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
tuple <double,double> A(vector<int>& n)
{
  double a1=n.size();
  double a2=a1+0.5;
  return make_tuple(a1,a2);
}

ma​​in.cpp - 主 cpp 文件

#include <iostream>
#include <vector>
#include <tuple>
#include "func.h"
using namespace std;
int main()
{
   double a1,a2;
   vector<int> n;
   n.push_back(1);
   n.push_back(2);
   tie(a1,a2)=A(n);
   return 0;
}

这在 Visual Studio 中编译得很好。

我在 Linux(gcc 版本 4.4.7 20120313 Red Hat 4.4.7-11)上编译它时遇到问题:

g++ -03 -std=c++0x main.cpp func.cpp -lm

它无法编译,我收到以下错误:

1. In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/array:35,from main.cpp:5:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/c++0x_warning.h:31:2: error: #error This file requires compiler and library suppcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

2. ‘std::tuple’ has not been declared

3. expected constructor, destructor, or type conversion before ‘<’ token

任何有关如何处理此问题的指导都会有所帮助!

【问题讨论】:

  • 4.4 版的 GCC 不能很好地支持 C++11,实际上几乎没有。如果可能,您应该将 GCC 更新到更高版本(现在升级到 6.2 版,完全支持 C++11 和 C++14,以及即将推出的大部分 C++17)。
  • 然后使用 C++11 之前的功能,例如 std::pair 或数组或结构。并使用其他环境来学习现代 C++。
  • 在您自己 PC 的虚拟机中安装 modern GNU/Linux 发行版,例如 CentOS6 minimum(相当于 RedHat6)并放弃它在 1997 年(发布 RHEL4.x/5 年)继续被困的“服务器人”,恕我直言,这对您不利。
  • 我编辑了您的原始命令 (g++ -o -std=c++0x b main.cpp func.cpp -03 -lm),因为它有一些错误(错误的优化选项、未知的 b 文件、缺少 a 输出)
  • @jarhead 我建议你学习使用 gcc。如果您留在软件中并且它是免费的,您可能会经常遇到它。考虑直接从源代码编译您自己的 gcc 版本...如果您有时间 ;)

标签: c++ linux gcc compilation


【解决方案1】:

令人惊讶的是,错误似乎告诉您 std=c++0x 未设置。 仔细检查您的编译命令。应该是

g++ -std=c++0x -o b main.cpp func.cpp -O3 -lm

而不是

g++ -o -std=c++0x b main.cpp func.cpp -03 -lm

和原来的问题一样。

【讨论】:

    【解决方案2】:

    您告诉 GCC 输出到名为“-std=c++0x”的文件,因此根本没有设置该选项,从而导致此错误。之后它对“b”做了什么,我不知道。但是您应该始终使用“-o outputfilename”,而不是在“-o”选项及其参数之间放置其他选项。

    【讨论】:

      【解决方案3】:

      我剪切并粘贴了您的三个文件(func.hfunc.cppmain.cpp),我可以向您保证,在我的 Linux 机器(CentOS 7.2)上使用 g++ (GCC) 4.8.5 20150623 (Red Hat 4.8 .5-4) 一切正常(你原来的命令有一些错误):

      g++ -o myProg -O3 -std=c++0x main.cpp func.cpp -lm

      更新你的 GCC(如果你有几个小时,即使是从源代码;))。

      【讨论】:

        【解决方案4】:

        由于您想在具有旧版本 Linux 和 GCC 的服务器上运行可执行文件(从最近的 C++11C++14 源代码编译)-您有不支持最新 C++ 的 GCC 4.4标准,因为它出现在 C++11 发布日期(2011 年)之前的 2009 年-您可以尝试以下方法:

        • 在您自己的笔记本电脑(或计算机)上安装最新的 Linux 发行版,并通过运行 g++ --version(您可能需要使用g++-5 而不是g++ 等...)

        • 编译和链接静态你的程序在笔记本电脑上使用g++ -static -std=c++14 -Wall func.cpp main.cpp -lm -o mybinprog(如果你想优化也可能还有-O3和/或-g进行调试 - 最好这样做本地调试-)

        • 将可执行文件复制(例如使用scp mybinprog remotehost:)到远程服务器并在那里运行

        很有可能(但不确定)在较新的 Linux(笔记本电脑)上构建的静态链接可执行文件可以在某些较旧的 Linux 服务器上运行。

        顺便说一句,要编译一个多源文件程序,最好学习如何使用GNU make

        请注意g++ 的程序参数顺序非常重要,因此请阅读有关Invoking GCC 的文档。

        PS。从技术上讲,您甚至可以尝试动态链接 C 库和静态链接 C++ 标准库。

        【讨论】:

        • OP 显示链接器错误,您解释了甚至可能与 OP 的情况无关的运行时错误(尽管我同意这些建议很好,但不值得回答)。
        • OP 确实显示链接器错误,而是显示有关语言标准的错误。
        • 对,编译错误,不是链接器错误。但是您的回答与错误消息无关。
        猜你喜欢
        • 2013-07-19
        • 1970-01-01
        • 2017-03-12
        • 2022-09-26
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多