Undefined reference to 'boost::system::generic_category()'?

18,212

Solution 1

You must explicitly add the system library for it to be linked into your program

find_package(Boost REQUIRED COMPONENTS system)
#                                      ^^^^^^ this :)

This must also be done for other Boost libraries that are built separately (regex, thread, etc.) (see here).

Solution 2

The "system" lib is missing in the linking stage. My config is as follows and it fixes the error:

find_package(Boost 1.55.0 REQUIRED COMPONENTS system filesystem)

include_directories(... ${Boost_INCLUDE_DIRS})

link_directories(... ${Boost_LIBRARY_DIRS})

target_link_libraries(... ${Boost_LIBRARIES})

Note that using only

find_package(Boost 1.55.0 REQUIRED)

does not work as ${Boost_LIBRARIES} won't be available then.

Solution 3

Ok, for those interested, the answer of Mark Garcia was a good call but what's more important here is that you need to explicitly link the libs you want from boost like

TARGET_LINK_LIBRARIES(ATFOR ${OpenCV_LIBS} curl ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY})

All other options i tried and read about didn't work me, don't know why but i hope this helps someone.

Solution 4

Check if you have the correct build (gcc, msvc...) of your boost libraries. I had this error when I tried linking a project using mingw with libraries built for msvc. If it is different, try building it for the platform you are using in your project.

The first time I used Boost I built it with default parameters resulting in libraries built for msvc, even though I was using mingw for my project. However CMake did find headers and libraries and everything seemed ok until I started using library features and compiling, that resulted in error mentioned by OP.

Rebuilding boost to toolset=gcc solved it (I was using Windows, CMake + MinGW).

Share:
18,212
Auberotte
Author by

Auberotte

Updated on June 17, 2022

Comments

  • Auberotte
    Auberotte almost 2 years

    it seems that i am unable to see the obvious. I wanted to use some Boost library features for my project and know i am getting these nice errors all of a sudden:

    Linking CXX executable ATFOR CMakeFiles/ATFOR.dir/stdafx.cc.o: In function __static_initialization_and_destruction_0(int, int)': stdafx.cc:(.text+0x3c): undefined reference toboost::system::generic_category()' stdafx.cc:(.text+0x48): undefined reference to boost::system::generic_category()' stdafx.cc:(.text+0x54): undefined reference toboost::system::system_category()' CMakeFiles/ATFOR.dir/Main.cc.o: In function __static_initialization_and_destruction_0(int, int)': Main.cc:(.text+0x29d): undefined reference toboost::system::generic_category()' Main.cc:(.text+0x2a9): undefined reference to boost::system::generic_category()' Main.cc:(.text+0x2b5): undefined reference toboost::system::system_category()' collect2: error: ld returned 1 exit status

    Here you find my CMakeLists.txt, headers, and main: http://pastie.org/8231509
    As you can see, i tried a lot playing around with the CMakeLists and i am pretty sure that i have all headers my project requires. Anyways, i've never had such errors before and i really appreciate any suggestions/solutions about what's wrong here since i don't have any ideas left for now. Thx in advance.

  • Auberotte
    Auberotte over 10 years
    Cmake says that it found the libs for filesystem, system, ... but when i 'make' there is still is a linking error, i installed libboost-1.53.0-all-dev, so i should have all i need. Is there a header for boost::system that i need to include? Didn't found one
  • Randall Cook
    Randall Cook about 9 years
    Super helpful. Thanks. +1. In my case, all I had to write was find_package(Boost REQUIRED system). I do not (yet) need filesystem.
  • rayryeng
    rayryeng about 7 years
    Though this question was unrelated to my problem, I decided to peruse through the answers here and your answer made me realise that I needed filesystem which finally resolved my problem. Thank you. +1.