8

I have been running through a few tutorials of boost and the libraries it has.

I ran through the basic tutorial for boost:

http://www.boost.org/doc/libs/1_52_0/more/getting_started/unix-variants.html

and it worked fine. I am having issues with the asio tutorial:

http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/tutorial/tutdaytime1/src.html

It looks like linking errors, but I am including the same path as before:

g++ -I /usr/local/boost_1_52_0 test.cpp -o example

Error:

/tmp/cce4EZME.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x57b): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x587): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x593): undefined reference to `boost::system::system_category()'
/tmp/cce4EZME.o: In function `boost::system::error_code::error_code()':
test.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/cce4EZME.o: In function `boost::asio::error::get_system_category()':
test.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
/tmp/cce4EZME.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
test.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/cce4EZME.o: In function `boost::asio::detail::posix_thread::join()':
test.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference to `pthread_join'
/tmp/cce4EZME.o: In function `boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)':
test.cpp:(.text._ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE[_ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE]+0x29): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
Jeff
  • 4,285
  • 15
  • 63
  • 115
  • 7
    you need -lpthread and -lboost_system-(something system specific) and maybe -lboost_signals-(something system specifc) – frankc Dec 13 '12 at 15:56
  • When you say system specific, if I were to compile it on windows, I'd have to change find the windows equivilant? – Jeff Dec 13 '12 at 17:39
  • I mean like the system library will be named something like libboost_system-gcc41-mt.so. I don't even know what it looks like on windows – frankc Dec 13 '12 at 20:23
  • On Windows boost uses auto-linking so you don't usually have to worry about that – Ralf Dec 13 '12 at 22:19
  • possible duplicate of [Problem Linking Boost Library in Linux](http://stackoverflow.com/questions/1408619/problem-linking-boost-library-in-linux) – Fraser Dec 14 '12 at 02:42
  • @frankc, thanks! I'll try to figure out what it is called. Cross-platform coding seems to be more difficult than I anticipated. – Jeff Dec 17 '12 at 14:55

1 Answers1

21

assuming you have installed your boost libraries in your /usr/lib directory and boost header files in /usr/include/boost and you have saved your code in a file named daytime1.cpp, you have to use the following command to compile the source code you have linked to:

$ g++ daytime1.cpp -o daytime -L /usr/lib/ -lboost_system -lboost_thread -lpthread
max
  • 2,627
  • 1
  • 24
  • 44
  • One important thing to note is that the linker flags must come after the filenames – Tom Jan 14 '21 at 02:21