错误:"提升"尚未声明

error: `boost' has not been declared

本文关键字:未声明 提升 错误      更新时间:2023-10-16

我正试图在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;
}

如果你帮我解决这个问题,我会很高兴的。

附言:我正在Centos 4.7中编译我的应用程序,它包含根据/usr/include/boost/version.hpp 的Boost 1.32版本

更新:

我也评论了boost指令,但include:boost/filesystem.hpp: No such file or directory有一些问题。

听起来您还没有安装include所需的boost头文件。由于您在CentOS上,您需要:

yum install boost-devel

这将把你想要的头文件放在:

/usr/include/boost/filesystem/path.hpp

由于您使用的是boost::filesystem::path,因此应将#include <boost/filesystem.hpp>更改为#include <boost/filesystem/path.hpp>。由于默认情况下-I /usr/include传递给gcc,因此不需要-I /usr/include/boost/filesystem,除非将include更改为path.hpp。然而,这将是危险的,因为另一个库可能具有相同的头文件名,然后您可能包含错误的头。

您可以尝试:g++ -std=c++11 -Os -Wall -pedantic test.cpp -lboost_system -lboost_filesystem -o test

我遇到了同样的问题

让我知道它是否有效

问候,

根据Centos Linux中的头文件,我更改了

#include <boost/filesystem.hpp>

#include <boost/filesystem/path.hpp>

还编译了我的程序与特殊的链接选项:

g++ test.cpp -o test.out  -lboost_filesystem