c++ - 使用 C++ 标准库在 Linux 上编译

标签 c++ linux gcc compilation

您好,有以下示例代码:

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

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

最佳答案

令人惊讶的是,该错误似乎告诉您未设置 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

如原问题。

关于c++ - 使用 C++ 标准库在 Linux 上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099526/

相关文章:

c++ - 为什么在使用复制分配运算符时需要删除资源?

c++ - 如果出现警告,如何立即使自动测试失败

使用 .po 和 .mo 文件进行 PHP 语言翻译

gcc - 对 'malloc' 等的 undefined reference

c++ - 编译时出错

c++ - 构造函数不能采用非常量引用

c++ - 为什么在 Main 中工作正常的代码在函数内工作不正确? (C++)

c++:为什么整数前面的减号会给出一个疯狂的数字?

c - 在 Linux 上将命令行参数存储在字符数组中

linux - 从以一天为增量的日期 block 中提取第一行