当前位置: 首页 > 工具软件 > cmake-ide > 使用案例 >

Cmake

陈瀚
2023-12-01
1.构建系统介绍

构建系统描述如何使用构建工具从其源代码构建项目的可执行文件和库,以自动化该过程。例如,一个构建系统可能是与命令行生成工具一起使用的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时,不需要特定的环境。

2.生成一个项目构件系统

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
3.Options - 构建选项
  • -S path-to-source>
    Path to root directory of the CMake project to build.
  • -B <path-to-build>
    Path to directory which CMake will use as the root of build directory.
    If the directory doesn’t already exist CMake will make it.
  • -G <generator-name>
    Specify a build system generator.
    If not specified, CMake checks the CMAKE_GENERATOR environment variable and otherwise falls back to a builtin default selection.
  • -T <toolset-spec>
    Toolset specification for the generator, if supported.
    Some CMake generators support a toolset specification to tell the native build system how to choose a compiler. See the CMAKE_GENERATOR_TOOLSET variable for details.
    -T host=x86 or x64
  • -A <platform-name>
    Specify platform name if supported by generator.
    Some CMake generators support a platform name to be given to the native build system to choose a compiler or SDK. See the CMAKE_GENERATOR_PLATFORM variable for details.
    -A Win32 or x64
4.Build a Project - 编译项目

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>
    Project binary directory to be built. This is required (unless a preset is specified) and must be first.
  • --preset <preset>, --preset=<preset>
    Use a build preset to specify build options. The project binary directory is inferred from the configurePreset key. The current working directory must contain CMake preset files. See preset for more details.
  • --list-presets
    Lists the available build presets. The current working directory must contain CMake preset files.
  • --parallel [<jobs>], -j [<jobs>]
    The maximum number of concurrent processes to use when building. If is omitted the native build tool’s default number is used.
    The CMAKE_BUILD_PARALLEL_LEVEL environment variable, if set, specifies a default parallel level when this option is not given.
    Some native build tools always build in parallel. The use of value of 1 can be used to limit to a single job.
  • --target <tgt>..., -t <tgt>...
    Build instead of the default target. Multiple targets may be given, separated by spaces.
  • --config <cfg>
    For multi-configuration tools, choose configuration .
    –clean-first
    Build target clean first, then build. (To clean only, use --target clean.)
  • --use-stderr
    Ignored. Behavior is default in CMake >= 3.0.
  • --verbose, -v
    Enable verbose output - if supported - including the build commands to be executed.
    This option can be omitted if VERBOSE environment variable or CMAKE_VERBOSE_MAKEFILE cached variable is set.
    --
    Pass remaining options to the native tool.
    Run cmake --build with no options for quick help.
 类似资料: