在Linux中链接Boost库

34

我正在尝试使用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;
}

可以在Boost网站上找到它。

所以,一开始我只有:

-I /usr/include/boost_1_40_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中的库文件,其中包括libboost_system.a。因此,我尝试使用以下命令进行编译:

-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.

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

3个回答

38

-llibboost_system改为-lboost_system

在Linux中,当引用库时,不需要在库名前加上"lib"前缀。


1
哇,真是微妙啊!我在指南中没注意到这一点。 - Scott
顺便提一下,请确保将标签从eclipse更改为linux。老实说,这与eclipse无关 :) - jameszhao00
7
一般而言,在Linux中,/usr/lib/目录下的库文件看起来是libname.so,但在连接时,您只需去掉'lib'和'.so'部分,使其变成-lname。 - initzero

25

在这种情况下,詹姆斯的回答是正确的,但如果其他人像我一样偶然看到了这篇文章,则应该注意,如果将旧版Boost头文件链接到较新的库,则可能会收到此消息。特别是get_system_category()已被弃用。当我意外地包含发行版提供的头文件但链接到我自己内部副本的Boost时,我遇到了这个问题。


23

如果你仍然遇到问题,你可能需要通过添加链接器标志来包含posix-threads:

-lpthread

2
正确构建多线程代码的方法不是链接到libptread,而是在编译和链接gcc调用时添加-pthread开关。-pthread 添加对使用pthread库进行多线程支持的功能。此选项为预处理器和链接器设置标志。 - pal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接