33

I am trying to build a project using Boost's Asio and I am having some trouble. 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. Among them was 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!

jacknad
  • 12,453
  • 38
  • 115
  • 190

3 Answers3

35

Change -llibboost_system to -lboost_system.

In linux, the "lib" prefix in front of a library is not used when referencing said library.

Patrizio Bertoni
  • 2,172
  • 23
  • 38
jameszhao00
  • 6,903
  • 14
  • 54
  • 109
  • Wow, that was subtle! I missed that in the guide. –  Sep 11 '09 at 02:05
  • By the way, make sure you change the tag from eclipse to linux. To be honest, this has nothing to do with eclipse :) – jameszhao00 Sep 11 '09 at 02:10
  • 7
    In general, with Linux, the libs in /usr/lib/ look like libname.so, but when you are linking them, you just strip off the 'lib' and '.so' part so it looks like -lname. – initzero Oct 06 '11 at 16:20
25

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. I ran into this problem while accidentally including distro-provided headers but linking against my own internal copy of boost.

Sam Miller
  • 22,843
  • 3
  • 61
  • 85
tgoodhart
  • 2,889
  • 21
  • 35
23

If you still get problems you might want to include posix-threads by adding to the linker flags:

-lpthread
Jeff
  • 1,230
  • 1
  • 11
  • 21
Gerhard Stein
  • 281
  • 3
  • 2
  • 1
    correct way to build multithreaded code is not by linking to libptread, but by adding -pthread switch during compile and link gcc invocations `-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.` – pal Feb 13 '17 at 17:55