在CMake项目中使用Boost::Beast实现WebSocket的Boost依赖查找问题
CMake查找Boost包报错问题解决
问题详情
在CMake项目中使用Boost::Beast开发WebSocket功能时,CMake查找Boost包出现错误。尝试设置Boost_USE_STATIC_LIBS=OFF和ON两种配置均无法解决。
CMake配置代码
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.1macOS Sequoia 15.0
解决办法
核心问题
错误根源是**find_package(Boost)调用时机过早**,CMake在执行project命令前未完成编译器的识别和初始化,导致Boost的工具集检测脚本BoostDetectToolset-1.86.0.cmake无法获取有效编译器信息,触发正则匹配参数错误。
修改步骤
将project命令移至find_package(Boost)之前,确保CMake先识别编译器:
cmake_minimum_required (VERSION 3.20) # 先声明项目,让CMake初始化编译器信息 project (myProject 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)
额外优化
- 编译器识别正常后,Boost能正确检测到AppleClang工具集,动态链接库的警告会自动消失。
- 若仍有链接问题,可显式设置
Boost_USE_MULTITHREADED=ON(默认已开启),确保链接多线程版本的Boost库。
内容的提问来源于stack exchange,提问作者cinargursoy
相关产品推荐
相关产品推荐

