CMake找不到Boost?

背景说明

版本:cmake 3.24.2/boost 1.80.0

环境:win11

问题描述

在 CMakeList.txt 中设置

SET(BOOST_ROOT ${CMAKE_SOURCE_DIR}/ext/boost})
find_package(Boost REQUIRED COMPONENTS thread)

后报错

Could NOT find Boost (missing: Boost_INCLUDE_DIR thread)

解决方法

发现有3种方法可解:

1. CMakeList.txt中直接使用常量路径

SET(BOOST_ROOT "C:/boost_1_80_0")
注:路径中不能使用变量

2. 在CMakeList.txt中设置变量的值一定要用双引号包裹

SET(BOOST_ROOT "${CMAKE_SOURCE_DIR}/ext/boost}")

3. 设置环境变量

setx -m BOOST_ROOT "C:/boost_1_80_0"
注:以管理员身份执行

注意事项

其中的路径(C:/boost_1_80_0)即为boost源码下载后,执行完

.\bootstrap.bat
.\b2.exe

之后提示的这部分路径

The following directory should be added to linker library paths:
    C:\boost_1_80_0\stage\lib

你可能感兴趣的:(c++,开发语言,1024程序员节)