c++macoscmakeboost

Using Boost::Beast with CMake for WebSocket


I'm trying to use Boost::Beast for WebSocket in my CMake prject. But I'm having trouble with CMake to find Boost package. I tried both Boost_USE_STATIC_LIBS=OFF and ON.

cmake_minimum_required (VERSION 3.20)

# set the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif(NOT CMAKE_BUILD_TYPE)

if(CMAKE_BUILD_TYPE MATCHES Debug)
  message("Debug build.")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
  message("Release build.")
endif()

set(application_name "myProject")
set(CMAKE_CXX_STANDARD 23)
set(application_version "0.0.1")
set(PROJECT_VER ${application_version})

cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0167 NEW)
# cmake_policy(SET CMP0175 NEW)

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_DEBUG ON)

find_package(Boost 1.75.0 REQUIRED COMPONENTS system)
# find_package(OpenSSL REQUIRED)

project (${application_name} LANGUAGES C CXX)

message(STATUS "Boost system found: ${Boost_SYSTEM_FOUND}")

add_subdirectory(ThirdParty)
add_subdirectory(src)
add_subdirectory(main)

Output:

Release build.
-- Found Boost 1.86.0 at /opt/homebrew/lib/cmake/Boost-1.86.0
--   Requested configuration: REQUIRED COMPONENTS system
-- BoostConfig: find_package(boost_headers 1.86.0 EXACT CONFIG REQUIRED  HINTS /opt/homebrew/lib/cmake)
-- Found boost_headers 1.86.0 at /opt/homebrew/lib/cmake/boost_headers-1.86.0
-- BoostConfig: find_package(boost_system 1.86.0 EXACT CONFIG REQUIRED  HINTS /opt/homebrew/lib/cmake)
-- Found boost_system 1.86.0 at /opt/homebrew/lib/cmake/boost_system-1.86.0
CMake Error at /opt/homebrew/lib/cmake/BoostDetectToolset-1.86.0.cmake:5 (string):
  string sub-command REGEX, mode MATCHALL needs at least 5 arguments total to
  command.
Call Stack (most recent call first):
  /opt/homebrew/lib/cmake/boost_system-1.86.0/boost_system-config.cmake:29 (include)
  /opt/homebrew/lib/cmake/Boost-1.86.0/BoostConfig.cmake:141 (find_package)
  /opt/homebrew/lib/cmake/Boost-1.86.0/BoostConfig.cmake:262 (boost_find_component)
  CMakeLists.txt:26 (find_package)


-- Boost toolset is unknown (compiler  )
-- Scanning /opt/homebrew/lib/cmake/boost_system-1.86.0/libboost_system-variant*.cmake
--   Including /opt/homebrew/lib/cmake/boost_system-1.86.0/libboost_system-variant-mt-shared.cmake
--   [x] libboost_system-mt.dylib
CMake Warning (dev) at /opt/homebrew/lib/cmake/boost_system-1.86.0/libboost_system-variant-mt-shared.cmake:63 (add_library):
  ADD_LIBRARY called with SHARED option but the target platform does not
  support dynamic linking.  Building a STATIC library instead.  This may lead
  to problems.
Call Stack (most recent call first):
  /opt/homebrew/lib/cmake/boost_system-1.86.0/boost_system-config.cmake:53 (include)
  /opt/homebrew/lib/cmake/Boost-1.86.0/BoostConfig.cmake:141 (find_package)
  /opt/homebrew/lib/cmake/Boost-1.86.0/BoostConfig.cmake:262 (boost_find_component)
  CMakeLists.txt:26 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

--   Including /opt/homebrew/lib/cmake/boost_system-1.86.0/libboost_system-variant-mt-static.cmake
--   [ ] libboost_system-mt.a
--   Including /opt/homebrew/lib/cmake/boost_system-1.86.0/libboost_system-variant-shared.cmake
--   [ ] libboost_system.dylib
--   Including /opt/homebrew/lib/cmake/boost_system-1.86.0/libboost_system-variant-static.cmake
--   [ ] libboost_system.a
-- Adding boost_system dependencies: headers
-- The C compiler identification is AppleClang 15.0.0.15000309
-- The CXX compiler identification is AppleClang 15.0.0.15000309
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost system found: 1

Environment:


Solution

  • project( must be nearly at the top of your cmake file, the only things that can be before it are the few commands that absolutely have to be before it like cmake_minimum_required https://cmake.org/cmake/help/latest/command/project.html

    find_package definitely can't be before project, your particular error seems to be caused by C++ not being enabled, this is because it's only enabled by calling project.

    You should be using:

    cmake_minimum_required (VERSION 3.20)
    cmake_policy(SET CMP0048 NEW)
    project (${application_name} LANGUAGES C CXX)
    
    # set the build type
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Release)
    endif(NOT CMAKE_BUILD_TYPE)
    
    if(CMAKE_BUILD_TYPE MATCHES Debug)
      message("Debug build.")
    elseif(CMAKE_BUILD_TYPE MATCHES Release)
      message("Release build.")
    endif()
    
    set(application_name "myProject")
    set(CMAKE_CXX_STANDARD 23)
    set(application_version "0.0.1")
    set(PROJECT_VER ${application_version})
    
    cmake_policy(SET CMP0167 NEW)
    # cmake_policy(SET CMP0175 NEW)
    
    set(Boost_USE_STATIC_LIBS OFF)
    set(Boost_DEBUG ON)
    
    find_package(Boost 1.75.0 REQUIRED COMPONENTS system)
    # find_package(OpenSSL REQUIRED)
    
    message(STATUS "Boost system found: ${Boost_SYSTEM_FOUND}")
    
    add_subdirectory(ThirdParty)
    add_subdirectory(src)
    add_subdirectory(main)