简体   繁体   English

boost.asio链接和库

[英]boost.asio linking and libraries

I'm new to boost.asio programming and i have difficulties linking boost libraries. 我是boost.asio编程的新手,我很难连接boost库。

My question is that how to find out which libraries I should link to my project when I include asio headers. 我的问题是,当包含asio标头时,如何找出应该链接到我的项目的库。

For example I used #include <boost/date_time/posix_time/posix_time.hpp> and #include <boost/asio.hpp directives. 例如,我使用了#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/asio.hpp指令。

so this is my command to compile it: 所以这是我的命令来编译它:

g++ -I /usr/local/boost_1_55_0 ASIO.cpp -o HELLO -L /usr/local/lib/ -l boost_system

consider my boost library is installed on /usr/local/boost_1_55_0 and binaries are on /usr/local/lib. 考虑我的boost库安装在/ usr / local / boost_1_55_0上,二进制文件在/ usr / local / lib上。

my problem is actually what I need to write after -l 我的问题实际上是我在-l之后需要写的东西

Thanks for your answers. 谢谢你的回答。

The libraries needing to be linked against will be determined based on the Boost.Asio features used and other Boost header files. 需要链接的库将根据所使用的Boost.Asio功能和其他Boost头文件来确定。 In general, one can determine which library to link against with some guess work on the undefined reference's namespaces, and inspecting the symbols in the Boost libraries if that doesn't work. 通常,可以使用未定义引用的名称空间上的一些猜测来确定要链接到哪个库,并检查Boost库中的符号是否无效。


In all of the examples below, I am using gcc 4.8.1. 在下面的所有示例中,我都使用gcc 4.8.1。 The exact error messages are not important, as the content in them should be fairly similar between compilers. 确切的错误消息并不重要,因为它们的内容在编译器之间应该非常相似。

Take a basic program that include Boost.Asio: 采取一个包含Boost.Asio的基本程序:

#include <boost/asio.hpp>

int main() {}

When compiling this: 编译时:

$ g++ example.cpp -isystem /usr/local/boost_1_55_0
...
example.cpp:(.text+0x31): undefined reference to
    `boost::system::generic_category()'
...
/tmp/cc9ow7fd.o: In function `boost::asio::error::get_system_category()':
    example.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[
    _ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference
     to `boost::system::system_category()'

Note the undefined references are symbols contained within the boost::system namespace. 注意,未定义的引用是boost::system命名空间中包含的符号。 If one was to look at the list of installed Boost libraries, libboost_system.so may come to mind as a good candidate for linking. 如果要查看已安装的Boost库的列表,可能会想到libboost_system.so是链接的理想选择。 If there is not an obvious candidate, then one can use tools like nm or readelf to examine candidate libraries and look for a symbol: 如果没有明显的候选对象,则可以使用nmreadelf之类的工具来检查候选库并查找符号:

$ nm --defined-only --print-file-name --demangle \
> /usr/local/lib/libboost_*.so | grep boost::system::generic_category
...
/usr/local/lib/libboost_system.so:
    00000000000015c0 T boost::system::generic_category()
...

In this case, the boost::system::generic_category() symbol is defined in the text section of libboost_system.so . 在这种情况下, boost::system::generic_category()符号在libboost_system.so的文本部分中libboost_system.so Thus, the basic program needs to link against boost_system : 因此,基本程序需要链接到boost_system

g++ example.cpp -isystem /usr/local/boost_1_55_0 -L/usr/local/lib/ -lboost_system

Here is an example that is a bit more complex. 这是一个更复杂的示例。 The program asynchronously waits on a monotonic timer in a coroutine that is executed by an additional thread. 程序异步等待协程中的单调定时器,该协程由另一个线程执行。

#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/thread/thread.hpp>

boost::asio::io_service io_service;

void handler(boost::asio::yield_context yield)
{
  boost::asio::steady_timer timer(io_service);
  timer.expires_from_now(boost::chrono::seconds(3));
  timer.async_wait(yield);
}

int main()
{
  boost::asio::spawn(io_service, &handler);
  boost::thread thread(boost::bind(
      &boost::asio::io_service::run, &io_service));
  thread.join();
}

While the same approach as before can be used, the linker error results in 40+ lines of mangled template symbols, which can be quite daunting. 尽管可以使用与以前相同的方法,但链接器错误会导致40多行错误的模板符号,这可能会让人望而生畏。 As an alternative, one can compile the example, but not link it by using the -c option. 另一种方法是,可以编译该示例,但不能使用-c选项将其链接。 Then, the resulting object file can be examined for undefined symbols: 然后,可以检查生成的目标文件中是否存在未定义的符号:

$ g++ example.cpp -isystem /usr/local/boost_1_55_0 -c
$ nm --demangle --undefined example.o | grep boost
    U boost::coroutines::stack_traits::default_size()
    U boost::coroutines::stack_traits::is_unbounded()
    U boost::coroutines::stack_traits::maximum_size()
    U boost::coroutines::stack_traits::minimum_size()
    U boost::coroutines::detail::coroutine_context::jump(
        boost::coroutines::detail::coroutine_context&, long, bool)
    U boost::coroutines::detail::coroutine_context::coroutine_context(
        void (*)(long), boost::coroutines::stack_context const&)
    U boost::coroutines::detail::coroutine_context::coroutine_context()
    U boost::chrono::steady_clock::now()
    U boost::detail::thread_data_base::~thread_data_base()
    U boost::system::system_category()
    U boost::system::generic_category()
    U boost::thread::join_noexcept()
    U boost::thread::native_handle()
    U boost::thread::start_thread_noexcept()
    U boost::thread::detach()
    U typeinfo for boost::detail::thread_data_base
    U vtable for boost::detail::thread_data_base

This output is much more manageable than the linker error. 该输出比链接器错误更易于管理。 Once again, the symbol namespaces ( boost::coroutine , boost::chrono , boost::thread , and boost::system ) provide a good hint as to what library may define the symbol. 再次,符号名称空间( boost::coroutineboost::chronoboost::threadboost::system )提供了一个很好的提示,说明哪些库可以定义该符号。 The above program can be compiled and linked with: 上述程序可以编译和链接:

g++ example.cpp -isystem /usr/local/boost_1_55_0 -L/usr/local/lib/ -lboost_coroutine -lboost_chrono -lboost_thread -lboost_system

Also, be aware that the linking order matters . 另外,请注意链接顺序很重要

As you told in the comments that you are on Ubuntu, I'll answer how to do it on an Ubuntu system. 正如你在评论中所说,你在Ubuntu上,我会回答如何在Ubuntu系统上做到这一点。

First, make sure you have the library installed. 首先,请确保您已安装该库。 It will be most convenient to use apt, since it saves you the effort of having to manually putting the library in /usr/local/lib . 使用apt将是最方便的,因为它省去了您手动将库放在/usr/local/lib

sudo apt-get install libboost-all-dev

Then you can compile your file using: 然后,您可以使用以下命令编译文件:

g++ my_file.cpp -lboost_system

EDIT: 编辑:

The only apparent mistake I can see in your command is that you use spaces after the flag indicators. 我可以在你的命令中看到的唯一明显错误是你在标志指示符之后使用空格。 Try this: 尝试这个:

g++ -I/usr/local/boost_1_55_0 ASIO.cpp -o HELLO -L/usr/local/lib/ -lboost_system

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

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