要查找常规的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代码不那么统一。
我错过什么了吗?
1条答案
按热度按时间lyfkaqu11#
如警告消息中所述,header-onlyBoost库不应在COMPONENTS中为
find_package(Boost)
指定。如果没有COMPONENTS,将自动搜索所有只包含头的库。要获取该库的包含目录,请使用
Boost::boost
target链接:还有
Boost::headers
目标,这意味着实际上与Boost::boost
目标相同。更多信息可以在documentation中找到。