2011-11-01 321 views
14

当我尝试编译使用boost :: filesystem库的代码时,我一直运行错误。我不明白我得到的任何编译器输出。下面是从http://www.highscore.de/cpp/boost/dateisystem.html#dateisystem_pfadangaben复制我的代码:C++ boost :: filesystem未定义对`boost :: filesystem3 :: path :: root_name()const'的引用

#include <boost/filesystem.hpp> 
#include <iostream> 

int main(){ 
    boost::filesystem::path p("C:\\Windows\\System"); 
    std::cout << p.root_name() << std::endl; 
    std::cout << p.root_directory() << std::endl; 
    std::cout << p.root_path() << std::endl; 
    std::cout << p.relative_path() << std::endl; 
    std::cout << p.parent_path() << std::endl; 
    std::cout << p.filename() << std::endl; 
} 

我的Ubuntu 11.10和我已经安装了libbost-dev的和g ++。这就是终端的样子:

[email protected]:~/Dokumente/Programming/Projekte/FTP-abgleicher$ g++ -o pr3 pr3.cpp 
/tmp/ccrN7yHl.o: In function `main': 
pr3.cpp:(.text+0x3b): undefined reference to `boost::filesystem3::path::root_name() const' 
pr3.cpp:(.text+0x7e): undefined reference to `boost::filesystem3::path::root_directory() const' 
pr3.cpp:(.text+0xc1): undefined reference to `boost::filesystem3::path::root_path() const' 
pr3.cpp:(.text+0x104): undefined reference to `boost::filesystem3::path::relative_path() const' 
pr3.cpp:(.text+0x147): undefined reference to `boost::filesystem3::path::parent_path() const' 
pr3.cpp:(.text+0x18a): undefined reference to `boost::filesystem3::path::filename() const' 
pr3.cpp:(.text+0x1e8): undefined reference to `boost::filesystem3::path::stem() const' 
pr3.cpp:(.text+0x22b): undefined reference to `boost::filesystem3::path::extension() const' 

/tmp/ccrN7yHl.o: In function `__static_initialization_and_destruction_0(int, int)': 
pr3.cpp:(.text+0x364): undefined reference to `boost::system::generic_category()' 
pr3.cpp:(.text+0x36e): undefined reference to `boost::system::generic_category()' 
pr3.cpp:(.text+0x378): undefined reference to `boost::system::system_category()' 
/tmp/ccrN7yHl.o: In function `boost::filesystem3::path::codecvt()': 
pr3.cpp:(.text._ZN5boost11filesystem34path7codecvtEv[boost::filesystem3::path::codecvt()]+0x7): undefined reference to `boost::filesystem3::path::wchar_t_codecvt_facet()' 
collect2: ld gab 1 als Ende-Status zurück 

^Translation of last line: ld returned an end status of 1 

我在做什么错?

+1

下一次,请突出显示您的代码并点击编辑器中的Ctrl + K或使用编辑器中的{}按钮。 – Mat

+0

作为[** 2012清理**]的一部分,标记'german'被移除(http://meta.stackexchange.com/questions/128315/the-great-stack-overflow-tag-question-cleanup-of-2012 )。 –

回答

27

你必须连结-lboost_filesystem -lboost_system。 Boost文件系统不是仅包含头文件的库;相反,它取决于编译的组件。

+0

您的意思是我需要将我的命令行更改为:g ++ -lboost_filesystem -o pr3 pr3.cpp – KG6ZVP

+0

我在命令行的末尾添加了-lboost_filesystem:g ++ -o pr3 pr3.cpp -lboost_filesystem 但是,我现在得到这个输出:/ usr/bin/ld:/tmp/ccKa219G.o:未定义的符号'boost :: system :: system_category()'引用' /usr/bin/ld:note:'boost :: system: :system_category()'在DSO /usr/lib/libboost_system.so.1.46.1中定义,因此请尝试将其添加到链接器命令行中 /usr/lib/libboost_system.so.1.46.1:无法读取符号:无效操作 collect2:ld返回结束状态1 – KG6ZVP

+6

@ KG6ZVP:同时添加'-lboost_system'。 –

相关问题