掌握CMake命令行高效技巧

CMake命令行工具概述

CMake是一个跨平台的构建系统生成工具,通过编写CMakeLists.txt文件来定义项目的构建过程。CMake命令行工具(cmake)用于配置、生成和管理项目构建系统,支持多种生成器(如Makefile、Ninja、Visual Studio等)。

核心命令行参数

-B <build-path>
指定构建目录路径,避免手动创建目录。例如:
cmake -B build 会在当前目录下创建build文件夹并初始化构建系统。

-S <source-path>
指定CMakeLists.txt所在的源码目录。与-B配合使用可显式分离源码和构建目录:
cmake -S . -B build

-G <generator-name>
指定生成器类型。常见选项包括:

  • Unix Makefiles(默认Linux)
  • Ninja(高性能构建工具)
  • Visual Studio 17 2022(Windows IDE项目)
    示例:
    cmake -G Ninja -B build

配置变量传递

-D <var>:<type>=<value>
定义或覆盖CMake缓存变量。例如:
cmake -DCMAKE_BUILD_TYPE=Debug -B build
cmake -DBUILD_SHARED_LIBS=ON -B build

-U <globbing_expr>
从缓存中删除匹配的变量。支持通配符:
cmake -U "*-TEST" -B build

构建与安装控制

--build <dir>
执行实际构建操作,等价于调用底层构建工具(如make/ninja)。常用附加参数:

  • --target <tgt> 指定构建目标
  • --parallel [<jobs>] 并行构建(如--parallel 8
    示例:
    cmake --build build --target install --parallel 4

--install <dir>
执行安装阶段(CMake 3.15+)。可附加参数:

  • --prefix <path> 指定安装路径
  • --config <type> 指定构建配置(如Debug/Release)
    示例:
    cmake --install build --prefix ~/software

高级调试工具

--graphviz=<file>
生成依赖关系图(需Graphviz支持):
cmake --graphviz=deps.dot -B build

--debug-trycompile
显示try_compile()测试的详细日志,用于诊断配置错误。

--trace-source=<file>
跟踪特定CMake文件的执行过程(CMake 3.7+):
cmake --trace-source=CMakeLists.txt -B build

实用技巧

跨平台编译配置示例:

# Windows生成VS项目
cmake -G "Visual Studio 17 2022" -A x64 -B build

# Linux生成Release版本
cmake -DCMAKE_BUILD_TYPE=Release -B build

# macOS生成Xcode项目
cmake -G Xcode -B build

预设文件使用(CMake 3.19+):

cmake --preset=ninja-release -B build

清除构建缓存:

rm -rf build
# 或Windows
rd /s /q build

版本与帮助信息

查看版本:
cmake --version

获取完整帮助:
cmake --help

查看特定生成器帮助:
cmake -G "Ninja" --help

5G.okacbd071.asia/PoSt/1123_184968.HtM
5G.okacbd072.asia/PoSt/1123_697759.HtM
5G.okacbd073.asia/PoSt/1123_218653.HtM
5G.okacbd074.asia/PoSt/1123_292728.HtM
5G.okacbd075.asia/PoSt/1123_881075.HtM
5G.okacbd076.asia/PoSt/1123_554352.HtM
5G.okacbd077.asia/PoSt/1123_103932.HtM
5G.okacbd078.asia/PoSt/1123_639719.HtM
5G.okacbd079.asia/PoSt/1123_003343.HtM
5G.okacbd080.asia/PoSt/1123_908423.HtM
5G.okacbd071.asia/PoSt/1123_869453.HtM
5G.okacbd072.asia/PoSt/1123_932744.HtM
5G.okacbd073.asia/PoSt/1123_906509.HtM
5G.okacbd074.asia/PoSt/1123_282557.HtM
5G.okacbd075.asia/PoSt/1123_608838.HtM
5G.okacbd076.asia/PoSt/1123_159990.HtM
5G.okacbd077.asia/PoSt/1123_347700.HtM
5G.okacbd078.asia/PoSt/1123_367336.HtM
5G.okacbd079.asia/PoSt/1123_761710.HtM
5G.okacbd080.asia/PoSt/1123_752632.HtM
5G.okacbd071.asia/PoSt/1123_833881.HtM
5G.okacbd072.asia/PoSt/1123_274806.HtM
5G.okacbd073.asia/PoSt/1123_335503.HtM
5G.okacbd074.asia/PoSt/1123_014762.HtM
5G.okacbd075.asia/PoSt/1123_375874.HtM
5G.okacbd076.asia/PoSt/1123_393863.HtM
5G.okacbd077.asia/PoSt/1123_124146.HtM
5G.okacbd078.asia/PoSt/1123_051839.HtM
5G.okacbd079.asia/PoSt/1123_682322.HtM
5G.okacbd080.asia/PoSt/1123_479390.HtM
5G.okacbd071.asia/PoSt/1123_618227.HtM
5G.okacbd072.asia/PoSt/1123_319938.HtM
5G.okacbd073.asia/PoSt/1123_376332.HtM
5G.okacbd074.asia/PoSt/1123_477214.HtM
5G.okacbd075.asia/PoSt/1123_559508.HtM
5G.okacbd076.asia/PoSt/1123_721623.HtM
5G.okacbd077.asia/PoSt/1123_677299.HtM
5G.okacbd078.asia/PoSt/1123_737276.HtM
5G.okacbd079.asia/PoSt/1123_156186.HtM
5G.okacbd080.asia/PoSt/1123_099590.HtM
5G.okacbd071.asia/PoSt/1123_635724.HtM
5G.okacbd072.asia/PoSt/1123_157741.HtM
5G.okacbd073.asia/PoSt/1123_177830.HtM
5G.okacbd074.asia/PoSt/1123_543056.HtM
5G.okacbd075.asia/PoSt/1123_914054.HtM
5G.okacbd076.asia/PoSt/1123_009978.HtM
5G.okacbd077.asia/PoSt/1123_974434.HtM
5G.okacbd078.asia/PoSt/1123_463832.HtM
5G.okacbd079.asia/PoSt/1123_457918.HtM
5G.okacbd080.asia/PoSt/1123_512738.HtM
5G.okacbd071.asia/PoSt/1123_093165.HtM
5G.okacbd072.asia/PoSt/1123_704226.HtM
5G.okacbd073.asia/PoSt/1123_452975.HtM
5G.okacbd074.asia/PoSt/1123_542505.HtM
5G.okacbd075.asia/PoSt/1123_482841.HtM
5G.okacbd076.asia/PoSt/1123_578073.HtM
5G.okacbd077.asia/PoSt/1123_309769.HtM
5G.okacbd078.asia/PoSt/1123_033890.HtM
5G.okacbd079.asia/PoSt/1123_110417.HtM
5G.okacbd080.asia/PoSt/1123_473905.HtM
5G.okacbd071.asia/PoSt/1123_217045.HtM
5G.okacbd072.asia/PoSt/1123_408370.HtM
5G.okacbd073.asia/PoSt/1123_896376.HtM
5G.okacbd074.asia/PoSt/1123_834006.HtM
5G.okacbd075.asia/PoSt/1123_954748.HtM
5G.okacbd076.asia/PoSt/1123_344969.HtM
5G.okacbd077.asia/PoSt/1123_375087.HtM
5G.okacbd078.asia/PoSt/1123_070806.HtM
5G.okacbd079.asia/PoSt/1123_896479.HtM
5G.okacbd080.asia/PoSt/1123_015249.HtM
5G.okacbd071.asia/PoSt/1123_521743.HtM
5G.okacbd072.asia/PoSt/1123_150791.HtM
5G.okacbd073.asia/PoSt/1123_917861.HtM
5G.okacbd074.asia/PoSt/1123_504895.HtM
5G.okacbd075.asia/PoSt/1123_429281.HtM
5G.okacbd076.asia/PoSt/1123_178149.HtM
5G.okacbd077.asia/PoSt/1123_345435.HtM
5G.okacbd078.asia/PoSt/1123_086957.HtM
5G.okacbd079.asia/PoSt/1123_756669.HtM
5G.okacbd080.asia/PoSt/1123_103751.HtM

#牛客AI配图神器#

全部评论

相关推荐

11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
10-10 00:14
门头沟学院 Java
程序员小白条:20年架构师,无工资
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务