【问题标题】:error: `boost' has not been declared错误:尚未声明“升压”
【发布时间】:2016-11-26 21:54:03
【问题描述】:

我正在尝试在我的 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 指令,但包含有一些问题:boost/filesystem.hpp: No such file or directory

【问题讨论】:

  • @drescherjm 已更新。感谢您的通知。

标签: c++ linux boost centos


【解决方案1】:

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

我遇到了同样的问题

让我知道是否有效

最好的问候,

【讨论】:

  • 我使用的是 gcc 版本 3.4.6。它不支持-std=c++11 选项。
  • 你试过没有 -std=c++11 吗?
  • 它没有编译,添加链接器命令不会帮助解决这个问题。
【解决方案2】:

听起来您还没有安装包含所需的 boost 头文件。由于您使用的是 CentOS,因此您需要:

yum install boost-devel

这将放置你想要的头文件:

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

由于您使用的是boost::filesystem::path,您应该将您的#include &lt;boost/filesystem.hpp&gt; 更改为#include &lt;boost/filesystem/path.hpp&gt;。因为-I /usr/include默认传递给gcc,所以你不需要-I /usr/include/boost/filesystem,除非你把include改成path.hpp。但是,这会很危险,因为另一个库可能具有相同的头文件名,然后您可能包含错误的头文件。

【讨论】:

  • 我安装了 boost-devel 包。但感谢您提供详细信息
  • 哦,是的,我完全错过了boost::filesystem::path,这意味着您引用了不同的包含。正在更新答案。
【解决方案3】:

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

#include <boost/filesystem.hpp>

#include <boost/filesystem/path.hpp>

并且还用特殊的链接选项编译了我的程序:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多