6

The example I'm trying to compile is from: http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html (the chat example)

Here is what I'm using to make it:

>>>    g++ chat_client.cpp chat_message.hpp
>>>    g++ chat_server.cpp chat_message.hpp

This is what the terminal outputs:

% g++ chat_client.cpp chat_message.hpp
In file included from chat_client.cpp:17:
chat_message.hpp: In member function ‘void chat_message::encode_header()’:
chat_message.hpp:84: warning: format ‘%4d’ expects type ‘int’, but argument 3 has type ‘size_t’
ld: warning: in chat_message.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols:
  "boost::system::generic_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in cctJA2c1.o
      __static_initialization_and_destruction_0(int, int)in cctJA2c1.o
  "boost::thread::start_thread()", referenced from:
      boost::thread::thread<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >(boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >, boost::disable_if<boost::is_convertible<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >&, boost::detail::thread_move_t<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > > >, boost::thread::dummy*>::type)in cctJA2c1.o
  "boost::thread::join()", referenced from:
      _main in cctJA2c1.o
  "typeinfo for boost::detail::thread_data_base", referenced from:
      typeinfo for boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >in cctJA2c1.o
  "vtable for boost::detail::thread_data_base", referenced from:
      boost::detail::thread_data_base::thread_data_base()in cctJA2c1.o
  "boost::thread::~thread()", referenced from:
      _main in cctJA2c1.o
      _main in cctJA2c1.o
  "boost::detail::thread_data_base::~thread_data_base()", referenced from:
      boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data()in cctJA2c1.o
      boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data()in cctJA2c1.o
  "boost::system::system_category()", referenced from:
      boost::system::get_system_category()     in cctJA2c1.o
      boost::system::error_code::error_code()in cctJA2c1.o
      __static_initialization_and_destruction_0(int, int)in cctJA2c1.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
  • Boost is installed correctly. I have another app that uses a couple of boosts bit features.

UPDATE: this is the current command I'm using:

% g++ chat_client.cpp chat_message.hpp -lboost_asio -lboost_thread -o client -L/opt/local/lib/

says it can't find -lboost_asio

I looked in opt/local/lib (where boost is installed) and I don't see any libboost_asio.so or anything like that =\

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352

1 Answers1

14

You need to provide g++ boost libraries to link with usins -l option. I have a quick look on this examples and guess that you definitly need thread library. You can link with it using

g++ source.cpp -o executable -lboost_thread 

Also you may need with boost_system library using

g++ source.cpp -o executable -lboost_thread -lboost_system
beduin
  • 7,943
  • 3
  • 27
  • 24
  • adding -lboost doesn't work... says library not found. I installed boost using my terminal's package manager... so... I know I have it. + my other app works with it... though we don't use -lboost with that either... – NullVoxPopuli Apr 28 '11 at 14:41
  • In the -l option you should specify not just boost, but the name of boost library used. For example, when your application uses boost-thread library (one with soname `libboost_thread.so`) you should specify `-lboost_thread`. – beduin Apr 28 '11 at 14:50
  • added update. I'm really not sure what libs I need. I wish this were documented somewhere – NullVoxPopuli Apr 28 '11 at 15:03
  • 2
    found the answer, I needed -mt at the end of boost_thread. Thanks for helping! http://stackoverflow.com/questions/2568243/linker-error-when-compiling-boost-asio-example – NullVoxPopuli Apr 28 '11 at 15:06
  • I corrected my answer several minutes ago. Boost asio is header-only library, it doesn't have corresponding shared library to link with. Sorry, I overlooked this at the beginning. Just remove -lboost_asio from command line. – beduin Apr 28 '11 at 15:06
  • What worked for me: `g++ source.cpp -o executable -lboost_system-mt` (note the "-mt" at the end). And of course, you have to have Boost **binaries** installed on the system, just headers are not enough to use boost::system. The easiest way for me to install was `brew install boost`. – altumano Oct 16 '13 at 09:01