Files
build_infra/0_cmake_library/doc.md
T
2026-07-22 10:58:59 +08:00

2.8 KiB

CMake 调试 / try_compile / 查找库 相关选项整理

1️⃣ try_compile / 编译测试相关

选项 / 变量 类型 示例 作用
--debug-trycompile 命令行 cmake --debug-trycompile -S . -B build 不删除 try_compile() / try_run() 生成的测试工程
CMAKE_TRY_COMPILE_CONFIGURATION 变量 -DCMAKE_TRY_COMPILE_CONFIGURATION=Debug 指定 try_compile 使用的构建类型
CMAKE_TRY_COMPILE_TARGET_TYPE 变量 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY 指定 try_compile 生成的目标类型
CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES 变量 -DCMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES=ON try_compile 不继承平台变量

2️⃣ CMake 脚本执行追踪

选项 示例 作用
--trace cmake --trace 打印每个执行的 CMake 指令
--trace-expand cmake --trace-expand trace 并展开变量
--trace-source=<file> cmake --trace-source=CMakeLists.txt 只 trace 指定文件
--trace-redirect=<file> cmake --trace-redirect=trace.log 把 trace 输出写入文件

3️⃣ find_package / 查找库调试

选项 / 变量 示例 作用
--debug-find cmake --debug-find 打印 find_package 搜索路径
CMAKE_FIND_DEBUG_MODE -DCMAKE_FIND_DEBUG_MODE=ON 调试 find 系列命令

4️⃣ 日志级别控制

选项 示例 作用
--log-level=<level> cmake --log-level=DEBUG 控制日志输出级别
--log-context cmake --log-context 显示日志来源
--log-level=TRACE cmake --log-level=TRACE 最详细日志

可用日志级别:

级别
ERROR
WARNING
NOTICE
STATUS
VERBOSE
DEBUG
TRACE

5️⃣ 生成系统调试

选项 示例 作用
--graphviz=<file> cmake --graphviz=deps.dot 生成依赖图
--profiling-output=<file> cmake --profiling-output=profile.json 输出配置性能分析

6️⃣ 常用调试组合

调试 try_compile

cmake --debug-trycompile -S . -B build

调试 find_package

cmake --debug-find

查看所有脚本执行

cmake --trace-expand

超级调试模式

cmake \
  --debug-trycompile \
  --trace-expand \
  --debug-find \
  --log-level=DEBUG

7️⃣ 交叉编译最常用三项

选项 作用
--debug-trycompile 查看 CMake 编译器测试
--debug-find 查看库搜索路径
--trace-expand 查看 CMake 变量和执行流程

8️⃣ try_compile 临时目录

CMake 的编译测试通常生成在:

build/CMakeFiles/CMakeTmp

开启 --debug-trycompile 后该目录不会被删除,方便调试编译问题。