websocketpp icon indicating copy to clipboard operation
websocketpp copied to clipboard

Using in a CMake Project

Open yash101 opened this issue 4 years ago • 5 comments

How do I use this library in a CMake project?

yash101 avatar May 17 '21 00:05 yash101

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 avatar May 17 '21 09:05 valerioformato

@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.

yash101 avatar May 17 '21 18:05 yash101

There is already an issue open about conforming to the "Modern CMake" style, but it's not been addressed yet, AFAIK.

valerioformato avatar May 18 '21 08:05 valerioformato

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 avatar Apr 12 '22 13:04 Rumpelstinsk

@Rumpelstinsk Don't use ${CMAKE_SOURCE_DIR}/websocketpp but ${websocketpp_SOURCE_DIR} . You don't really know where websocketpp sources live after being fetched.

MartyMcFlyInTheSky avatar Nov 15 '22 08:11 MartyMcFlyInTheSky