33

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

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

4

3 回答 3

35

更改-llibboost_system-lboost_system

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

于 2009-09-11T01:57:21.930 回答
25

在这种情况下,詹姆斯的回答是正确的,但是如果其他人碰巧像我一样偶然发现了这篇文章,那么请注意,如果您将旧的 bo​​ost 标头链接到较新的库,您就会收到此消息。get_system_category()具体已被弃用。我在不小心包含发行版提供的标头但链接到我自己的内部 boost 副本时遇到了这个问题。

于 2011-08-15T15:21:51.117 回答
23

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

-lpthread
于 2012-02-23T14:12:59.100 回答