2013-07-30 79 views
0

好吧现在我试图在Linux(Ubuntu 12.04)中使用boost C++库,就像我以前在Windows中使用它们一样。因此,使用一些示例代码从Boost的网站/usr/bin/ld:找不到-llibboost

testfile.cpp

#include <boost/filesystem/convenience.hpp> 
#include <boost/foreach.hpp> 
#include <boost/range.hpp> 
#include <iostream> 

int main(int, char**) 
{ 
    namespace bf = boost::filesystem; 
    BOOST_FOREACH(bf::path path, 
     boost::make_iterator_range(
      bf::recursive_directory_iterator(bf::path("/home")), 
      bf::recursive_directory_iterator())) { 
    std::cout << path.string() << std::endl; 
} 
return 0; 
} 

应该很容易编译使用此命令

g++ -L/usr/local/lib -o "testfile" -llibboost_filesystem 

我的问题我收到链接器错误

/usr/bin/ld: cannot find -llibboost_filesystem 

并且看起来似乎没有图剔除我缺少的东西。请帮忙。

+11

难道不该'-lboost_filesystem'? – arne

+0

是的,你的权利。我没有意识到文件之间的命名约定在那里改变了。谢谢 – Marstang

+0

[/ usr/bin/ld:找不到-llibboost \ _filesystem]的可能重复(http://stackoverflow.com/questions/11526546/usr-bin-ld-cannot-find-llibboost-filesystem) –

回答

0

按照惯例,库名在大多数Linux发行版上使用前缀lib。指示链接程序搜索哪些库时,应删除此前缀。假设GNU ld链接,文档说

-l namespec 
--library=namespec 

    Add the archive or object file specified by namespec to the list of files to 
    link. This option may be used any number of times. If namespec is of the 
    form :filename, ld will search the library path for a file called filename, 
    otherwise it will search the library path for a file called libnamespec.a. 

让你无论是想

g++ -L/usr/local/lib -o "testfile" -lboost_filesystem 

g++ -L/usr/local/lib -o "testfile" -l :libboost_filesystem.so