简体   繁体   English

在Linux中链接Boost库

[英]Linking Boost Library in Linux

I am trying to build a project using Boost's Asio and I am having some trouble. 我正在尝试使用Boost的Asio构建一个项目,我遇到了一些麻烦。 Initially, I tried to build the project without any additional libraries since everything is supposedly in the header files. 最初,我试图在没有任何额外库的情况下构建项目,因为所有内容都应该在头文件中。

The program I am trying to build looks like this: 我正在尝试构建的程序如下所示:

#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;
}

It can be found here on Boost's website. 可以发现这里在加速的网站。

So, initially I just had: 所以,最初我只是:

-I /usr/include/boost_1_40_0

This resulted in the following errors: 这导致以下错误:

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.

It appeared that I needed the system library. 看来我需要系统库。 So, I followed the directions on the Getting Started guide found here , which gave me a bunch of libraries located in /usr/include/boost_1_40_0/stage/lib . 所以,我按照这里的入门指南中的说明进行操作,这给了我一些位于/ usr / include / boost_1_40_0 / stage / lib中的库 Among them was libboost_system.a . 其中有libboost_system.a Thus, I attempted to compile with: 因此,我尝试编译:

-I /usr/include/boost_1_40_0
-L /usr/include/boost_1_40_0/stage/lib
-l libboost_system

However, I got this: 但是,我得到了这个:

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.

I'm not sure why, but it can't seem to identify the library or any of the others that I try. 我不确定为什么,但它似乎无法识别我尝试的库或任何其他库。 What might I be doing incorrectly? 我可能做错了什么? Thanks in advance! 提前致谢!

Change -llibboost_system to -lboost_system . 更改-llibboost_system-lboost_system

In linux, the "lib" prefix in front of a library is not used when referencing said library. 在linux中,引用所述库时不使用库前面的“lib”前缀。

In this case james' answer was correct, but if anybody else happens to stumble upon this post like I did then be aware that you can get this message if you link old boost headers against newer libraries. 在这种情况下,詹姆斯的答案是正确的,但如果有其他人碰巧偶然发现这个帖子,那么请注意,如果你将旧的提升标题链接到新的库,你就可以得到这个消息。 get_system_category() specifically has been deprecated. get_system_category()具体已被弃用。 I ran into this problem while accidentally including distro-provided headers but linking against my own internal copy of boost. 我遇到了这个问题,偶然包括发行版提供的标题,但链接到我自己的内部副本的boost。

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

-lpthread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM