Windows下CMake无法找到Boost .lib文件及CONFIG模式配置问题
1. 链接器错误
使用非仅头文件的Boost包时出现链接失败:
error: LNK1104: cannot open file 'libboost_wave-vc143-mt-gd-x64-1_87.lib'
2. CMake CONFIG模式查找Boost失败
使用CMake CONFIG模式查找Boost时,报错无法定位配置文件:
By not providing "FindBoost.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Boost", but CMake did not find one. CMake Error at cmake/Modules/UHDBoost.cmake:190 (find_package): By not providing "FindBoost.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Boost", but CMake did not find one. Could not find a package configuration file provided by "Boost" (requested version 1.66) with any of the following names: BoostConfig.cmake boost-config.cmake Add the installation prefix of "Boost" to CMAKE_PREFIX_PATH or set "Boost_DIR" to a directory containing one of the above files. If "Boost" provides a separate development package or SDK, be sure it has been installed. Call Stack (most recent call first): CMakeLists.txt:309 (include)
已配置环境变量
BOOST_ROOT C:\DevTools\boost_1_87_0 BOOST_LIBRARYDIR C:\DevTools\boost_1_87_0\lib64-msvc-14.3 BOOST_INCLUDEDIR C:\DevTools\boost_1_87_0
环境信息
- Boost版本:1.87.0(Visual Studio 2022/14.3对应预编译二进制包)
- CMake版本:3.3(Qt内置)、4.0(系统安装)
- 开发环境:Qt + CMake + Visual Studio 2022
一、解决链接器找不到库文件问题
确认目标库文件存在
检查C:\DevTools\boost_1_87_0\lib64-msvc-14.3目录下是否存在libboost_wave-vc143-mt-gd-x64-1_87.lib。若不存在,说明预编译包未包含wave模块,需重新下载完整的对应版本预编译包,或手动编译Boost的wave模块。让CMake正确识别Boost路径
在CMakeLists.txt中显式配置Boost路径,优先使用CMake导出的目标链接:# 用MODULE模式查找Boost(适配预编译包) find_package(Boost 1.87 REQUIRED COMPONENTS wave) # 若环境变量未被读取,手动指定路径 if(NOT Boost_FOUND) set(BOOST_ROOT "C:/DevTools/boost_1_87_0") set(BOOST_LIBRARYDIR "C:/DevTools/boost_1_87_0/lib64-msvc-14.3") find_package(Boost 1.87 REQUIRED COMPONENTS wave) endif() # 链接Boost库到目标项目 target_link_libraries(你的项目名称 PRIVATE Boost::wave)使用
Boost::wave目标可自动处理依赖和路径,避免手动指定库文件名。匹配编译配置
确保项目编译配置(Debug/Release、x86/x64)与Boost预编译库一致:- 错误中的
-gd对应Debug模式,当前项目需为Debug+x64配置 - 切换到Release模式时,需使用不带
-gd的Boost库(如libboost_wave-vc143-mt-x64-1_87.lib)
- 错误中的
二、解决CMake CONFIG模式查找失败问题
Boost预编译二进制包默认不提供BoostConfig.cmake,CONFIG模式需要Boost安装时生成的配置文件,预编译包通常仅包含头文件和库文件,无CMake配置文件。解决方式:
切换到MODULE模式查找
修改find_package命令为MODULE模式(CMake默认自带FindBoost.cmake模块):# MODULE模式(可省略MODULE关键字) find_package(Boost 1.66 REQUIRED COMPONENTS wave)注意:报错要求版本为1.66,但你安装的是1.87,需确认项目是否真的需要1.66,或修改项目中的Boost版本要求为1.87。
手动生成Boost CMake配置文件
若必须使用CONFIG模式,可通过Boost的b2工具生成配置文件:- 打开Visual Studio 2022开发者命令提示符
- 进入Boost根目录
C:\DevTools\boost_1_87_0 - 执行命令:
bootstrap.bat b2 install --prefix=C:\DevTools\boost_installed toolset=msvc-14.3 address-model=64 - 安装完成后,
C:\DevTools\boost_installed\lib\cmake\Boost-1.87.0目录下会生成BoostConfig.cmake - 在CMake中指定路径:
set(Boost_DIR "C:/DevTools/boost_installed/lib/cmake/Boost-1.87.0") find_package(Boost 1.87 REQUIRED CONFIG COMPONENTS wave)
修改项目中的
UHDBoost.cmake
报错来自cmake/Modules/UHDBoost.cmake第190行,该文件可能强制使用CONFIG模式。可将其中的find_package(Boost ... CONFIG)改为find_package(Boost ... MODULE),或添加对Boost环境变量的支持。
额外检查项
- 重启Qt Creator和命令提示符,确保环境变量生效
- 确认Visual Studio 2022工具集版本为14.3(与Boost预编译包的
vc143匹配) - 若使用Qt Creator,在项目CMake配置中手动添加
BOOST_ROOT、BOOST_LIBRARYDIR作为CMake变量,避免环境变量未被读取
内容的提问来源于stack exchange,提问作者mehdihfzp

