Using in a CMake Project
How do I use this library in a CMake project?
One way of doing it (or at least, how I'm doing in my project) is this
# === boost ===
find_package(Boost REQUIRED COMPONENTS filesystem system thread regex)
# === websocket++ ===
FetchContent_Declare(websocketpp
GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git
GIT_TAG 0.8.2)
FetchContent_GetProperties(websocketpp)
if(NOT websocketpp_POPULATED)
FetchContent_Populate(websocketpp)
add_subdirectory(${websocketpp_SOURCE_DIR} ${websocketpp_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
# add interface library with all websocketpp dependencies
add_library(Websockets INTERFACE)
target_include_directories(Websockets INTERFACE ${websocketpp_SOURCE_DIR})
target_link_libraries(Websockets INTERFACE Boost::system Boost::thread Boost::regex)
you get the Websockets target, which sohld play nice with the modern CMake style of target dependency handling
@valerioformato thank you for this example code! I wonder if a PR can be raised to add this to the README since this is likely a common question.
There is already an issue open about conforming to the "Modern CMake" style, but it's not been addressed yet, AFAIK.
I tried this approach but I keep getting an error:
CMake Error at websocketpp/websocketpp/CMakeLists.txt:1 (init_target):
Unknown CMake command "init_target".
-- Configuring incomplete, errors occurred!
These are the lines that I include on CMakeLists.txt
add_subdirectory(${CMAKE_SOURCE_DIR}/websocketpp) // I've also try the inner folder${CMAKE_SOURCE_DIR}/websocketpp/websocketpp
add_library(Websockets INTERFACE)
target_include_directories(Websockets INTERFACE ${CMAKE_SOURCE_DIR}/websocketpp/websocketpp)
target_link_libraries(Websockets INTERFACE Boost::system Boost::thread Boost::regex)
@Rumpelstinsk Don't use ${CMAKE_SOURCE_DIR}/websocketpp but ${websocketpp_SOURCE_DIR} . You don't really know where websocketpp sources live after being fetched.