Search code examples
c++cmakerestbed

Link restbed with Cmake


In trying to link restbed with CMake I get the usual undefined function error. However, trying the exact same code linked with g++ test.cpp -o test -lrestbed works fine.

Furthermore, when I first implemented the CMakeLists.txt it also worked fine and as I added to the project it started facing issues. Now even a single restbed function is not defined.

My restbed includes are located at /usr/local/include and the shared objects to link at /usr/local/lib. Pretty standard locations.

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(vcar-server)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -lrestbed")

file(GLOB CXX_EXEC "src/*.cpp")

add_subdirectory(vcar-embedded)

add_executable(vcar-server ${CXX_EXEC})
target_link_libraries(vcar-server vcar)

Solution

  • Do not use CMAKE_CXX_FLAGS, or rather use it as a "last resort" when configuring the build system as a user. Prefer to use object model in place of global variables. The modern cmake way would most probably be something along:

    cmake_minimum_required(VERSION 3.11)
    project(vcar-server)
    add_subdirectory(vcar-embedded)
    find_package(Threads REQUIRED)
    add_executable(vcar-server ${cxx_exec})
    target_link_libraries(vcar-server PUBLIC vcar restbed Threads::Threads)
    set_target_properties(vcar-server PUBLIC CXX_STANDARD 20)