文章目录
cmake_minimum_required
简介
- 功能:为项目设置cmake的最低要求版本
- 常用程度:⭐⭐⭐⭐⭐
- 命令格式
bash
cmake_minimum_required(VERSION <min>[...<policy_max>] [FATAL_ERROR])
- 命令详解
<min>
和<policy_max>
的格式:major.minor[.patch[.tweak]]
- major:主版本号
- minor:次版本号
- patch:补丁版本号
- tweak:微调版本号
- 如果执行构建的cmake版本低于
<min>
,项目构建会停止并且会报告错误 <policy_max>
如果指定,必须至少为<min>
,影响策略设置。
使用案例
- Windows终端查看cmake版本
bash
cmake --version
- 输出当前版本号为
3.28.3
:
bash
D:\Test\projectX\build>cmake --version
cmake version 3.28.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
普通设置
- CMakeLists.txt中的内容
bash
cmake_minimum_required(VERSION 3.27)
- 【✅】执行cmake构建命令后的输出
bash
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: D:/Test/projectX/build
执行构建的cmake版本低于<min>
-
CMakeLists.txt中的内容
bashcmake_minimum_required(VERSION 3.30)
-
【❌】执行cmake构建命令后的输出:
bash-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045. CMake Error at CMakeLists.txt:1 (cmake_minimum_required): CMake 3.30 or higher is required. You are running version 3.28.3 -- Configuring incomplete, errors occurred!
project
简介
- 功能:设置项目信息
- 常用程度:⭐⭐⭐⭐⭐
- 命令格式
bash
project(<PROJECT-NAME> [<language-name>...])
project(<PROJECT-NAME>
[VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
[DESCRIPTION <project-description-string>]
[HOMEPAGE_URL <url-string>]
[LANGUAGES <language-name>...])
- 命令详解
使用案例
普通设置
- CMakeLists.txt中的内容
bash
cmake_minimum_required(VERSION 3.20)
project(projectX)
- 【✅】执行cmake构建命令后的输出
bash
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: D:/Test/projectX/build
set
简介
- 功能:设置变量值
- 常用程度:⭐⭐⭐⭐⭐
- 命令格式
bash
set(<variable> <value>... [PARENT_SCOPE])
- 命令详解
使用案例
普通设置
- CMakeLists.txt中的内容
bash
cmake_minimum_required(VERSION 3.20)
project(projectX)
set(CMAKE_CXX_STANDARD 11)
- 【✅】执行cmake构建命令后的输出
bash
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: D:/Test/projectX/build