开始学习
< 返回

CMake 设置 Debug 和 Release 编译模式

对 CMake 工程进行编译时,可以通过 CMAKE_BUILD_TYPE 宏决定项目编译成 Debug 版本还是 Release 版本,默认为 Release 版本。

Debug 模式,可以进行 gdb 调试

mkdir debug
cd debug 
cmake -DCMAKE_BUILD_TYPE=Debug .. 
make

Release 模式

mkdir release
cd release 
cmake -DCMAKE_BUILD_TYPE=Release .. 
make

当然,你也可以在 CMakeLists.txt 文件中指定编译模式,如下。

SET(CMAKE_BUILD_TYPE "Debug")
# 或者
SET(CMAKE_BUILD_TYPE "Release")

编译完成后,我们可以使用 readelf 命令来查看该二进制可执行文件是否包含 debugging symbols 信息。

例如:查看 debug/bin/Template 的 Section 头部信息,可以看到 debug 相关的符号。

$ readelf -S debug/bin/Template | grep debug
  [28] .debug_aranges    PROGBITS         0000000000000000  0000609b
  [29] .debug_info       PROGBITS         0000000000000000  0000615b
  [30] .debug_abbrev     PROGBITS         0000000000000000  00007c79
  [31] .debug_line       PROGBITS         0000000000000000  0000823b
  [32] .debug_str        PROGBITS         0000000000000000  00008cdc

而查看 release/bin/Template 的 Section 头部信息,则没有打印任何内容。

$ readelf -S release/bin/Template | grep debug
Was this article helpful?
0 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
Please Share Your Feedback
How Can We Improve This Article?
文章目录