I'm new to boost.asio programming and i have difficulties linking boost libraries.
My question is that how to find out which libraries I should link to my project when I include asio headers.
For example I used #include <boost/date_time/posix_time/posix_time.hpp>
and #include <boost/asio.hpp
directives.
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.
my problem is actually what I need to write after -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. 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.
In all of the examples below, I am using 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:
When compiling this:
Note the undefined references are symbols contained within the
boost::system
namespace. 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. 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:In this case, the
boost::system::generic_category()
symbol is defined in the text section oflibboost_system.so
. Thus, the basic program needs to link againstboost_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.
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. As an alternative, one can compile the example, but not link it by using the
-c
option. Then, the resulting object file can be examined for undefined symbols:This output is much more manageable than the linker error. Once again, the symbol namespaces (
boost::coroutine
,boost::chrono
,boost::thread
, andboost::system
) provide a good hint as to what library may define the symbol. The above program can be compiled and linked with: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.
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
.Then you can compile your file using:
EDIT:
The only apparent mistake I can see in your command is that you use spaces after the flag indicators. Try this: