您好,有以下示例代码:
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);
}
main.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/