Linux中Boost库的链接

Linux中Boost库的链接,linux,boost,boost-asio,linker-errors,deadline-timer,Linux,Boost,Boost Asio,Linker Errors,Deadline Timer,我正在尝试使用Boost的Asio构建一个项目,我遇到了一些麻烦。最初,我尝试在没有任何附加库的情况下构建项目,因为所有内容都应该在头文件中 我试图构建的程序如下所示: #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::asio::io_service io;

我正在尝试使用Boost的Asio构建一个项目,我遇到了一些麻烦。最初,我尝试在没有任何附加库的情况下构建项目,因为所有内容都应该在头文件中

我试图构建的程序如下所示:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));

    t.wait();

    std::cout << "Hello, world!" << std::endl;

    return 0;
}
这导致了以下错误:

make -k all
Building target: HelloWorld
Invoking: GCC C++ Linker
g++  -o"HelloWorld"  ./main.o  
./main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost_1_40_0/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:206: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:211: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:212: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:213: undefined reference to `boost::system::get_system_category()'
./main.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost_1_40_0/boost/asio/error.hpp:218: undefined reference to `boost::system::get_system_category()'
./main.o: In function `error_code':
/usr/include/boost_1_40_0/boost/system/error_code.hpp:312: undefined reference to `boost::system::get_system_category()'
./main.o: In function `posix_tss_ptr':
/usr/include/boost_1_40_0/boost/asio/detail/posix_tss_ptr.hpp:47: undefined reference to `pthread_key_create'
./main.o: In function `~posix_tss_ptr':
/usr/include/boost_1_40_0/boost/asio/detail/posix_tss_ptr.hpp:61: undefined reference to `pthread_key_delete'
./main.o: In function `boost::asio::detail::posix_thread::join()':
/usr/include/boost_1_40_0/boost/asio/detail/posix_thread.hpp:77: undefined reference to `pthread_join'
./main.o: In function `~posix_thread':
/usr/include/boost_1_40_0/boost/asio/detail/posix_thread.hpp:69: undefined reference to `pthread_detach'
collect2: ld returned 1 exit status
make: *** [HelloWorld] Error 1
make: Target `all' not remade because of errors.
看来我需要系统库。因此,我按照《入门指南》中的说明进行了操作,该指南提供了位于/usr/include/boost_1_40_0/stage/lib中的一组库。其中包括Libriu系统。因此,我尝试使用以下工具进行编译:

-I /usr/include/boost_1_40_0
-L /usr/include/boost_1_40_0/stage/lib
-l libboost_system
然而,我得到了这个:

make -k all
Building target: HelloWorld
Invoking: GCC C++ Linker
g++ -L/usr/lib -L/usr/include/boost_1_40_0/stage/lib -o"HelloWorld"  ./main.o   -llibboost_system
/usr/bin/ld: cannot find -llibboost_system
collect2: ld returned 1 exit status
make: *** [HelloWorld] Error 1
make: Target `all' not remade because of errors.

我不知道为什么,但它似乎无法识别我尝试的库或任何其他库。我可能做错了什么?提前谢谢

-llibboost\u系统
更改为
-lboost\u系统


在linux中,引用所述库时不使用库前面的“lib”前缀。

在本例中,james的回答是正确的,但是如果其他人碰巧像我一样偶然发现了这篇文章,那么请注意,如果将旧的boost头与新的库链接,则可以得到此消息
get\u system\u category()
已被明确弃用。我在无意中包含发行版提供的头文件时遇到了这个问题,但链接到了我自己的boost内部副本。

如果仍然遇到问题,您可能希望通过添加链接器标志来包含posix线程:

-lpthread

哇,太微妙了!我在指南中遗漏了这一点。顺便说一下,请确保将标记从eclipse更改为linux。老实说,这与eclipse无关:)一般来说,在Linux中,/usr/lib/中的lib看起来像libname.so,但在链接它们时,只需去掉“lib”和“.so”部分,使其看起来像-lname。构建多线程代码的正确方法不是链接到libptread,但是通过在编译和链接gcc调用期间添加-pthread开关
,pthread增加了对pthreads库中多线程的支持。此选项设置预处理器和链接器的标志。
-lpthread