我正在尝试在我的 C++ 应用程序中使用 boost 库。我正在尝试使用具有不同选项的 g++ 对其进行编译。例如 g++ -I /usr/include/boost/filesystem/ -o test.out test.cpp
但是它总是提示 error: 'boost' has not been declared
。
这是我的代码:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
using namespace std;
int main (){
string line;
string fileName = "Read.txt";
ifstream file;
string str;
file.open(fileName.c_str());
cout << "Hello, world!\n";
vector<string> fileLines;
fileLines.clear();
while (getline(file, str))
{
fileLines.push_back(line);
}
cout << "Total Line count:"<<fileLines.size()<<endl;
fileLines.clear();
cout << "Total Line count:"<<fileLines.size()<<endl;
boost::filesystem::path p("/tmp/foo.txt");
return 0;
}
如果你能帮我解决这个问题,我会很高兴。
PS我正在Centos 4.7中编译我的应用程序,它包含根据 /usr/include/boost/version.hpp
的Boost版本1.32
更新:
我还评论了 boost 指令,但是包括一些问题: boost/filesystem.hpp: No such file or directory
。
原文由 VSB 发布,翻译遵循 CC BY-SA 4.0 许可协议
听起来您还没有安装包含所需的 boost 头文件。由于您使用的是 CentOS,因此您需要:
这将放置你想要的头文件:
由于您使用的是
boost::filesystem::path
,因此您应该将#include <boost/filesystem.hpp>
更改为#include <boost/filesystem/path.hpp>
。由于-I /usr/include
默认传递给 gcc,因此您不需要-I /usr/include/boost/filesystem
,除非您将包含更改为path.hpp
。但是,这会很危险,因为另一个库可能具有相同的头文件名,然后您可能会包含错误的头文件。