# CMake

**CMake** 是一个跨平台的开源构建系统生成器：项目用 CMake 语言文件描述其构建过程，CMake 则为原生构建系统（如 Makefile、Ninja 文件，或 [Visual Studio](https://www.edgepedia.org/zh/visual-studio) 与 Xcode 工程）生成输入文件。它于 2000 年在 Kitware 为美国国家医学图书馆的 Insight Toolkit（ITK）项目而创建，如今是 C 和 C++ 代码最广泛使用的构建工具。<sup>[1](https://cmake.org/history/)</sup><sup> • </sup><sup>[2](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)</sup><sup> • </sup><sup>[3](https://lp.jetbrains.com/the-state-of-cpp-2025/)</sup>

| 关键事实 | 详情 |
|---|---|
| 创建 | 2000 年，为美国国家医学图书馆的 Insight Toolkit 项目而创建；原始作者兼首席架构师为 Kitware 的 Bill Hoffman<sup>[1](https://cmake.org/history/)</sup><sup> • </sup><sup>[4](https://www.kitware.com/celebrating-20-years-of-cmake/)</sup> |
| 类型 | “元”构建系统：它为被称为生成器的后端（Unix Makefiles、Ninja、Visual Studio、Xcode 等）生成构建系统<sup>[2](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)</sup><sup> • </sup><sup>[5](https://cmake.org/cmake/help/latest/guide/tutorial/Before%20You%20Begin.html)</sup> |
| 项目描述 | 每个源码树有一个顶层 `CMakeLists.txt` 文件，用 CMake 语言编写；目标通过 `add_library()`、`add_executable()` 等命令声明<sup>[2](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)</sup><sup> • </sup><sup>[6](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html)</sup> |
| 采用率 | 在 JetBrains 2025 年的调查中，59% 的 C++ 开发者和 56% 的 C 开发者报告经常使用；ISO C++ 2024 开发者调查中为 83.24%<sup>[3](https://lp.jetbrains.com/the-state-of-cpp-2025/)</sup><sup> • </sup><sup>[7](https://lp.jetbrains.com/the-state-of-c-2025/)</sup><sup> • </sup><sup>[8](https://isocpp.org/files/papers/CppDevSurvey-2024-summary.pdf)</sup> |
| 配套工具 | 用于运行测试的 CTest、用于跨平台安装程序的 CPack，以及用于依赖管理的 FetchContent 和 ExternalProject 模块<sup>[9](https://cmake.org/features/)</sup> |
| 当前发布系列 | CMake 4.x；4.0 版移除了对早于 3.5 的策略版本的兼容性<sup>[10](https://cmake.org/cmake/help/latest/release/4.0.html)</sup> |

## 工作原理

CMake 是一个配置程序，而非编译代码的程序。项目用 CMake 语言编写的文件来指定其构建，从顶层 `CMakeLists.txt` 开始；这些文件声明构建目标（如可执行文件和库）及其依赖关系。CMake 运行时，先执行配置步骤，检测编译器和环境，然后执行生成步骤，为选定的原生构建系统（称为生成器）写出输入文件。真正运行编译和链接命令的是生成的构建系统，而非 CMake 本身。<sup>[2](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)</sup><sup> • </sup><sup>[5](https://cmake.org/cmake/help/latest/guide/tutorial/Before%20You%20Begin.html)</sup>

**源码树与构建树。** CMake 区分源码树（存放项目文件）与构建树（接收生成的构建系统文件和构建产物）。CMake 会在构建树中写入一个 `CMakeCache.txt` 文件，用以标识该构建树并存储持久的配置信息。推荐做法是源外构建，即使用与源码分离的专用构建树；源内构建虽受支持但不被鼓励。<sup>[2](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)</sup>

**生成器。** `-G` 选项用于选择生成器，否则 CMake 接受平台默认值。命令行构建工具生成器（如 `Unix Makefiles` 和 `Ninja`）需要相应的构建工具，并要求在 shell 中配置好编译器环境；IDE 生成器（如 Visual Studio 和 Xcode）则自行配置环境。一个构建目录不能换用不同的生成器重复使用。<sup>[5](https://cmake.org/cmake/help/latest/guide/tutorial/Before%20You%20Begin.html)</sup><sup> • </sup><sup>[2](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)</sup>

**构建配置。** 单配置生成器（如 Makefiles 和 Ninja）在配置时通过 `CMAKE_BUILD_TYPE` 变量固定构建类型，例如 `Debug` 或 `Release`。多配置生成器（如 Visual Studio、Xcode 和 Ninja Multi-Config）则在构建时用 `cmake --build --config` 选择配置，并忽略 `CMAKE_BUILD_TYPE`。标准配置有 `Debug`、`Release`、`RelWithDebInfo` 和 `MinSizeRel`。<sup>[6](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html)</sup><sup> • </sup><sup>[5](https://cmake.org/cmake/help/latest/guide/tutorial/Before%20You%20Begin.html)</sup>

## 特性

CMake 的既定目标是让开发者在从 Android、iOS 到高性能计算系统的各类平台上，用同一套输入文件构建简单和复杂的软件系统。其特性包括：<sup>[9](https://cmake.org/features/)</sup>

- 源外构建，每个源码树可对应多个构建树。
- 以目标为中心的模型：每个目标声明其所依赖的内容，CMake 据此安排构建顺序并传播传递性链接需求。
- 系统自省：每个构建树有一份缓存，记录探测到的设置、已安装软件的路径和构建标志，可通过 cmake-gui、ccmake 等图形界面编辑。
- 跨平台安装，以及用于在 Linux、Windows 和 Mac 上生成安装程序的 CPack 打包系统。
- CTest 测试系统：测试在项目的 CMake 文件中声明，结果可上报至 CDash。
- 自定义构建时命令、配置生成的头文件，以及可供其他项目复用的目标导出/导入。
- 通过工具链文件支持交叉编译，以及基于 `try_compile()` 的特性检查。<sup>[11](https://cmake.org/cmake/help/v3.31/manual/cmake-toolchains.7.html)</sup>

依赖可以用 `ExternalProject` 模块处理（在构建时下载），或用 `FetchContent` 模块处理（在配置时使下载内容可用，从而能通过 `add_subdirectory()` 直接加入构建）。<sup>[12](https://cmake.org/cmake/help/v3.20/module/FetchContent.html)</sup>

## 历史

2000 年，美国国家医学图书馆将 Kitware 纳入一个由三家商业公司和三所大学组成的团队，为医学数据的分割与配准构建一个开源 C++ 工具包，即 Insight Toolkit，CMake 由此诞生。合同只要求可移植到 Unix、Windows 和 Mac，这为新的构建工具留出了空间；Bill Hoffman 在 GE Research 用了十年 autotools 和 gmake 之后，为该项目编写了 CMake。Kitware 早先的可视化工具包（VTK）的成功为其赢得了这份合同。<sup>[1](https://cmake.org/history/)</sup><sup> • </sup><sup>[4](https://www.kitware.com/celebrating-20-years-of-cmake/)</sup>

采用率通过其他开源项目增长。2006 年，KDE 项目在 KDE 开发者 Alexander Neundorf 的推动下改用 CMake；Kitware 在几周内做出了原型构建系统，CMake 比当时使用的 SCons 系统更快地构建了更多 KDE 内容，KDE 借此迅速将应用移植到 Windows 和 Mac。在此过程中，CMake 获得了共享库版本管理和无需重新链接的安装 rpath 重写功能，各大 Linux 发行版也开始分发 CMake 以支持 KDE。<sup>[1](https://cmake.org/history/)</sup>

项目历史记录的后来的里程碑包括：2008 年 LLVM 的第一个 `CMakeLists.txt`；2009 年由 ARL 资助的 ExternalProject 模块；2011 年的 Ninja 生成器；2012 年 Stephen Kelly 的“现代 CMake”使用需求工作；2016 年 LLVM 从 autoconf 全面切换；2017 年 Microsoft Visual Studio 集成 CMake 项目支持；2018 年 Craig Scott 的《Professional CMake》一书；2019 年 [Qt Group](https://www.edgepedia.org/zh/qt-group) 的切换；2020 年 Visual Studio 中的 CMake Presets 和 Ninja Multi-Config 生成器；以及从 2020 年起与 Bloomberg 合作支持 C++20 模块。<sup>[1](https://cmake.org/history/)</sup>

2023 年，CMake 推动了 p1689 标准格式，编译器借此描述 C++20 模块依赖信息；该格式已由 Visual Studio、Clang 和 GCC 实现，CMake 用它来支持构建 C++20 模块。<sup>[1](https://cmake.org/history/)</sup>

## 版本与兼容性

CMake 通过其策略机制保持向后兼容。每项行为变更都赋予一个形如 `CMP<NNNN>` 的标识符，记录旧行为和新行为，项目用 `cmake_minimum_required(VERSION <min>...<max>)` 声明其支持的版本范围，该命令请求采用截至指定版本所推荐的行为，同时让更新的 CMake 版本对新策略发出警告。<sup>[13](https://cmake.org/cmake/help/v4.0/command/cmake_policy.html)</sup>

4.0 版划出了明确界线：移除了对早于 3.5 的策略版本的兼容性，调用 `cmake_minimum_required()` 或 `cmake_policy()` 设置更旧版本现在会产生错误，不过 `<min>...<max>` 语法仍允许项目只要求一个旧的最低版本，同时在更新的 CMake 运行时采用新行为。新增了 `CMAKE_POLICY_VERSION_MINIMUM` 变量，以帮助打包者配置尚未更新的项目。<sup>[10](https://cmake.org/cmake/help/latest/release/4.0.html)</sup> 截至 2026 年底，当前发布系列为 4.4，4.4.4 于 2026 年 10 月发布。<sup>[14](https://discourse.cmake.org/t/cmake-4-4-4-available-for-download/15862)</sup>

## 采用情况与替代方案比较

调查显示，CMake 在 C 系代码构建工具中遥遥领先。在 [JetBrains](https://www.edgepedia.org/zh/jetbrains) 2025 年对 1,800 名 C++ 开发者的调查中，CMake 的使用率为 59%，高于 Visual Studio 工程的 34% 和 Makefile 的 27%；在近 900 名 C 开发者中，CMake 以 56% 领先，Makefile 为 37%。<sup>[3](https://lp.jetbrains.com/the-state-of-cpp-2025/)</sup><sup> • </sup><sup>[7](https://lp.jetbrains.com/the-state-of-c-2025/)</sup> ISO C++ 年度开发者调查报告 2024 年使用率为 83.24%，高于 2023 年的 79.88%。<sup>[8](https://isocpp.org/files/papers/CppDevSurvey-2024-summary.pdf)</sup><sup> • </sup><sup>[15](https://isocpp.org/files/papers/CppDevSurvey-2023-summary.pdf)</sup> 桑迪亚国家实验室的一份报告称 CMake 在 C++ 代码构建市场占 55%。<sup>[16](https://www.osti.gov/servlets/purl/2003320)</sup>

Meson 项目自己的比较称赞 CMake 对多种后端（包括 Visual Studio 和 Xcode）的良好支持，同时批评其脚本语言笨重，一些简单的事情被弄得比必要的更复杂。<sup>[17](https://github.com/mesonbuild/meson/blob/master/docs/markdown/Comparisons.md)</sup> 在 Meson 对 1,000 个生成的 C 文件的基准测试中，两个基于 Ninja 的构建系统都优于非 Ninja 的构建系统，而将 CMake 的后端从 Make 换成 Ninja，在项目 CMake 文件毫无改动的情况下，总构建时间减少了 3.5 秒，超过 20%。<sup>[18](https://mesonbuild.com/Simple-comparison.html)</sup>

## References

1. [History of CMake, Kitware/cmake.org](https://cmake.org/history/)
2. [cmake(1) manual, CMake 4.4 Documentation](https://cmake.org/cmake/help/latest/manual/cmake.1.html?highlight=install)
3. [The State of C++ 2025, JetBrains](https://lp.jetbrains.com/the-state-of-cpp-2025/)
4. [Celebrating 20 Years of CMake!, Kitware](https://www.kitware.com/celebrating-20-years-of-cmake/)
5. [Step 0: Before You Begin, CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/Before%20You%20Begin.html)
6. [cmake-buildsystem(7), CMake Documentation](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html)
7. [The State of C 2025, JetBrains](https://lp.jetbrains.com/the-state-of-c-2025/)
8. [2024 Annual C++ Developer Survey "Lite", ISO C++](https://isocpp.org/files/papers/CppDevSurvey-2024-summary.pdf)
9. [CMake: The Standard Build System, cmake.org](https://cmake.org/features/)
10. [CMake 4.0 Release Notes](https://cmake.org/cmake/help/latest/release/4.0.html)
11. [cmake-toolchains(7), CMake 3.31 Documentation](https://cmake.org/cmake/help/v3.31/manual/cmake-toolchains.7.html)
12. [FetchContent module, CMake 3.20 Documentation](https://cmake.org/cmake/help/v3.20/module/FetchContent.html)
13. [cmake_policy command, CMake 4.0 Documentation](https://cmake.org/cmake/help/v4.0/command/cmake_policy.html)
14. [CMake 4.4.4 available for download, CMake Discourse](https://discourse.cmake.org/t/cmake-4-4-4-available-for-download/15862)
15. [2023 Annual C++ Developer Survey "Lite", ISO C++](https://isocpp.org/files/papers/CppDevSurvey-2023-summary.pdf)
16. [SAND2022-7018C build-systems report, OSTI](https://www.osti.gov/servlets/purl/2003320)
17. [Comparing Meson with other build systems, Meson docs](https://github.com/mesonbuild/meson/blob/master/docs/markdown/Comparisons.md)
18. [A simple comparison, mesonbuild.com](https://mesonbuild.com/Simple-comparison.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/cmake

English original: https://www.edgepedia.org/cmake
