2016-07-22 901 views
0

我想在我的C++应用程序中使用boost库。我试图使用不同的选项使用g ++编译它,例如g++ -I /usr/include/boost/filesystem/ -o test.out test.cpp但是它总是提示error: 'boost' has not been declared错误:'boost'还没有被声明

这里是我的代码:

#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; 
} 

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

P.S.我编译我的应用程序在Centos的4.7,它包含根据/usr/include/boost/version.hpp

更新升压版本1.32:

我还评论提振指令,但有一些问题,包括:boost/filesystem.hpp: No such file or directory

+0

@drescherjm更新。感谢您的通知。 – VSB

回答

0

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

我有同样的问题

让我知道,如果是利国利民的

最好的问候,

+0

我使用的是gcc版本3.4.6。它不支持'-std = C++ 11'选项。 – VSB

+0

你试过没有-std = C++ 11吗? –

+0

没有它,它也没有工作。 – VSB

1

听起来好像你还没有安装升压您需要的头文件包括。既然你是在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,因此除非将include更改为path.hpp,否则不需要-I /usr/include/boost/filesystem。但是,这会很危险,因为另一个库可能具有相同的头文件名,然后可能包含错误的头。

+0

我安装了boost-devel软件包。但感谢细节 – VSB

+0

哦,是的,我完全错过了'boost :: filesystem :: path'这意味着你引用了一个不同的include。更新答案。 –

0

Accodring在我的Centos的Linux头文件,我改变

#include <boost/filesystem.hpp> 

#include <boost/filesystem/path.hpp> 

而且还编译了一个程序有特殊的链接选项:

g++ test.cpp -o test.out -lboost_filesystem 
相关问题