如何在cmake find_package中正确包含boost模块?

mnemlml8  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(309)

要查找常规的boost模块,cmake代码相当简单

cmake_minimum_required(VERSION 3.25)
project(win32)

set(CMAKE_CXX_STANDARD 17)

find_package(fmt CONFIG REQUIRED)
find_package(Boost REQUIRED COMPONENTS program_options)

add_executable(win32  main.cpp)

target_link_libraries(win32 PRIVATE fmt::fmt Boost::program_options)

但是,如果我打算查找像boost::lexical_cast这样的仅头模块,find_package会失败,并显示错误消息:

[0/1] Re-running CMake...
CMake Warning at C:/Users/isudfv/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/231.8109.80/bin/cmake/win/x64/share/cmake-3.25/Modules/FindBoost.cmake:2218 (message):
  No header defined for lexical_cast; skipping header check (note:
  header-only libraries have no designated component)
Call Stack (most recent call first):
  C:/Program Files/Vcpkg/installed/x64-windows-static/share/boost/vcpkg-cmake-wrapper.cmake:11 (_find_package)
  C:/Program Files/Vcpkg/scripts/buildsystems/vcpkg.cmake:806 (include)
  CMakeLists.txt:8 (find_package)

CMake Error at C:/Users/isudfv/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/231.8109.80/bin/cmake/win/x64/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost (missing: lexical_cast) (found version "1.81.0")
Call Stack (most recent call first):
  C:/Users/isudfv/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/231.8109.80/bin/cmake/win/x64/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  C:/Users/isudfv/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/231.8109.80/bin/cmake/win/x64/share/cmake-3.25/Modules/FindBoost.cmake:2377 (find_package_handle_standard_args)
  C:/Program Files/Vcpkg/installed/x64-windows-static/share/boost/vcpkg-cmake-wrapper.cmake:11 (_find_package)
  C:/Program Files/Vcpkg/scripts/buildsystems/vcpkg.cmake:806 (include)
  CMakeLists.txt:8 (find_package)

ninja: error: rebuilding 'build.ninja': subcommand failed
-- Configuring incomplete, errors occurred!
See also "C:/Users/isudfv/Desktop/tmp/win32/cmake-build-release/CMakeFiles/CMakeOutput.log".
FAILED: build.ninja

有一个解决方案说我应该使用target_include_directories,但它使cmake代码不那么统一。
我错过什么了吗?

lyfkaqu1

lyfkaqu11#

如警告消息中所述,header-onlyBoost库不应在COMPONENTS中为find_package(Boost)指定。如果没有COMPONENTS,

find_package(Boost)

将自动搜索所有只包含头的库。要获取该库的包含目录,请使用Boost::boost target链接:

target_link_libraries(win32 PRIVATE Boost::boost)

还有Boost::headers目标,这意味着实际上与Boost::boost目标相同。
更多信息可以在documentation中找到。

相关问题