Search code examples
c++linuxboostcmake

undefined reference to `boost::system::generic_category()


I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error: /usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:

cmake_minimum_required(VERSION 3.7)
project(pki_cpp)

set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})

include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)

find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
    message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)


# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library

#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp

add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads  boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)

Any help is appreciated!


Solution

  • That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System

    I'd go with

    FIND_PACKAGE(Boost 1.65.0
         COMPONENTS system program_options REQUIRED)
    
    LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
    INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
    LINK_LIBRARIES(${Boost_LIBRARIES})
    

    Alternatively you can be specific and link specific libs to specific targets

    TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
    TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})