我正在尝试在我的 CMake 项目中使用 Boost::Beast for WebSocket。但我在使用 CMake 查找 Boost 包时遇到了麻烦。我尝试了
Boost_USE_STATIC_LIBS=OFF
和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)
输出:
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
环境:
cmake version 3.31.1
macOS Sequoia 15.0
project(
必须几乎位于 cmake 文件的顶部,唯一可以位于它之前的是绝对必须位于它之前的几个命令,例如 cmake_minimum_required
https://cmake.org/cmake/help /latest/command/project.html
find_package
绝对不能在project
之前,您的特定错误似乎是由未启用C++引起,这是因为它只能通过调用project
来启用。
您应该使用:
cmake_minimum_required (VERSION 3.20)
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 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)
message(STATUS "Boost system found: ${Boost_SYSTEM_FOUND}")
add_subdirectory(ThirdParty)
add_subdirectory(src)
add_subdirectory(main)