20

I would understand this error message if I had not put the -lboost_system flag, but it is really here:

g++ -o build/myproject build/main/main.o -L/usr/local/boost/boost_1_52_0/boost/libs -L/usr/lib -Lbuild -L. -lboost_system -lboost_thread -lpthread -lboost_regex -lpq -lmylibrary
build/libmylibrary.a(library.o): In function `__static_initialization_and_destruction_0(int, int)':
library.cpp:(.text+0x25f): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x269): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x273): undefined reference to `boost::system::system_category()'

Do you have any idea what should I investigate to solve the problem ? (I use gcc 4.6.3)

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • 9
    try putting it at the end. –  Mar 07 '13 at 20:17
  • 1
    nooo !? **that works ! ! !** do you have any idea why ? – Stephane Rolland Mar 07 '13 at 20:25
  • 3
    It's explained in the second part of [this answer](http://stackoverflow.com/a/492498/1252091). –  Mar 07 '13 at 20:26
  • 2
    Even easier, `-DBOOST_SYSTEM_NO_DEPRECATED` likely makes it unnecessary to link with boost_system at all (http://stackoverflow.com/a/30877725/1918193). – Marc Glisse Jun 16 '15 at 20:41
  • @MarcGlisse Still complains:/home/meir/boost_1_59_0/boost/thread/pthread/thread_data.hpp:278: undefined reference to `boost::this_thread::hiden::sleep_until(timespec const&)' – AlwaysLearning Dec 04 '15 at 07:06
  • @AlwaysLearning That's different, I was only talking about the symbols generic_category and system_category. You on the other hand need `-lboost_thread`. – Marc Glisse Dec 04 '15 at 08:09

1 Answers1

31

The order at which you link your libraries matters, in your case you have library.cpp that apparently uses the boost_system library

library.cpp:(.text+0x25f): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x269): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x273): undefined reference to `boost::system::system_category()'

To solve this you should move the boost_system library to the end of your link line

g++ -o build/myproject build/main/main.o -L/usr/local/boost/boost_1_52_0/boost/libs -L/usr/lib -Lbuild -L. -lboost_thread -lpthread -lboost_regex -lpq -lmylibrary **-lboost_system** 

Alternatively, build libmylibrary.so as a shared library and link to the boost_system library directly.

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
  • 3
    Saved two lives! (at least) – Ami Tavory Oct 03 '16 at 19:44
  • 2
    With boost 1.65.0, I was linking `system` before `filesystem`, and it worked well. With boost 1.68.0 I had to reverse the order. Note that the two lins were not compiled with the exact same script/jam files. Apparently, they can change the fact that `system` should be linked last or first... – jpo38 Jan 23 '19 at 13:07