Why “undefined reference to `boost::system::generic_category” even if I do link against boost_system

boostgcclinker

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)

Best Answer

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.

Related Topic