2012-12-17 123 views
0

我想遍歷目錄並打印從根開始的所有文件的名稱。遞歸掃描文件的目錄

這是我在程序中使用Boost::Filesystem(1.52.0)寫的一小段代碼。

void testApp::getNames(const string& dirPath, string& fileExtension) 
{ 
    namespace fs = boost::filesystem; 
    namespace sys = boost::system; 
    fs::path filePath(dirPath); 
    for(fs::recursive_directory_iterator dir(filePath), dir_end; dir!=dir_end ;++dir) 
    { 
     cout<<*dir; 
    } 
} 

在嘗試編譯這個,奇怪的是我正在建立一個指向上下面的代碼片段path.hpp文件錯誤:

static const codecvt_type& codecvt() 
    { 
     return *wchar_t_codecvt_facet(); 
    } 

,我得到的錯誤是undefined reference to boost::filesystem3::path::wchar_t_codecvt_facet()'|

我在Ubuntu 12.10爲我的項目使用Codeblocks IDE。

+1

這是一個鏈接器錯誤 - 文件系統庫包含必須編譯和鏈接的組件 - 它不是僅包含頭文件的庫。 –

+0

您可以指向必須使用的鏈接器標誌/ – user1240679

+1

是不是隻要添加-lboost_filesystem? – daramarak

回答

1

這是一個鏈接器錯誤。您需要鏈接到Boost文件系統庫。

在IDE中,應該有一個設置用於在項目設置中的某處添加庫。 (我不知道確切的位置,因爲我從來沒有使用過代碼塊。)

+0

我已經在我的makefile'USER_LDFLAGS = -lboost_system'中使用了以下鏈接器標誌。 Boost :: Filesytem鏈接器是不同的? – user1240679

+1

@ user1240679是的,您需要'boost_filesystem'庫,因此請將'-lboost_filesystem'添加到您的'USER_LDFLAGS'變量中。 –

+0

唷!在這件事上狠狠地抨擊了我的頭,結果只是連接器標誌。謝謝 – user1240679