构建系统描述如何使用构建工具从其源代码构建项目的可执行文件和库,以自动化该过程。例如,一个构建系统可能是与命令行生成工具一起使用的Makefile,或者是集成开发环境(IDE)的项目文件。为了避免维护多个这样的构建系统,一个项目可以使用用CMake语言编写的文件抽象地指定它的构建系统。
源代码树 —— 源代码顶级目录
包含项目提供的源文件的顶级目录。该项目使用cmake-language(7)手册中描述的文件指定它的构建系统,从一个名为CMakeLists.txt的顶级文件开始。这些文件指定了构建目标和它们的依赖关系,如cmake-buildsystem(7)手册中所描述的。
构建树 —— 生成工程和可执行文件或库的顶级目录
用于存储构建系统文件和构建输出工件(例如可执行文件和库)的顶级目录。CMake会写一个CMakeCache.txt文件来将目录标识为构建树,并存储诸如构建系统配置选项之类的持久信息。 要维护原始的源树,请使用单独的专用构建树执行源外构建。也支持将构建树放置在与源树相同的目录中的源代码内构建,但不建议这样做。
生成器
这将选择生成的构建系统类型。所有生成器的文档请参见cmake-generators(7)手册。运行cmake --help查看本地可用的生成器列表。可以选择使用下面的-G选项来指定一个生成器,或者简单地接受当前平台的默认CMake选择。 当使用命令行构建工具生成器时,CMake期望编译器工具链所需的环境已经在shell中配置好。当使用IDE Build Tool generator时,不需要特定的环境。
cmake [<options>] <path-to-source>
Uses the current working directory as the build tree, and as the source tree. The source tree must contain a CMakeLists.txt file and must not contain a CMakeCache.txt file because the latter identifies an existing build tree. For example:
$ mkdir build ; cd build
$ cmake ../src
cmake [<options>] <path-to-existing-build>
Uses as the build tree, and loads the path to the source tree from its CMakeCache.txt file, which must have already been generated by a previous run of CMake. The specified path may be absolute or relative to the current working directory. For example:
$ cd build
$ cmake .
cmake [<options>] -S <path-to-source> -B <path-to-build>
Uses as the build tree and as the source tree. The source tree must contain a CMakeLists.txt file. The build tree will be created automatically if it does not already exist. For example:
$ cmake -S src -B build
-S path-to-source>
-B <path-to-build>
-G <generator-name>
-T <toolset-spec>
-T host=x86 or x64
-A <platform-name>
-A Win32 or x64
CMake provides a command-line signature to build an already-generated project binary tree:
cmake --build <dir> [<options>] [-- <build-tool-options>]
cmake --build --preset <preset> [<options>] [-- <build-tool-options>]
This abstracts a native build tool’s command-line interface with the following options:
--build <dir>
--preset <preset>, --preset=<preset>
--list-presets
--parallel [<jobs>], -j [<jobs>]
--target <tgt>..., -t <tgt>...
--config <cfg>
--use-stderr
--verbose, -v
--