2016-07-22 899 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 
相關問題