当前位置: 首页 > 工具软件 > 0release > 使用案例 >

gcc中的debug版本和release版本

缪升
2023-12-01

结论:Gcc中并没有Release和debug版本之分,只有编译选项的组合

大学时候使用VS开发,VS编译的程序是可以选debug或者release的,搞得我一直误以为使用gcc编译,也有这种区分。


详细介绍

1 优化参数 -Os的使用

Note that gcc doesn’t have a “release mode” and a “debug mode” like MSVC does. All code is just code. The presence of the various optimization options (-O2 and -Os are the only ones you generally need to care about unless you’re doing very fine tuning) modifies the generated code, but not in a way to prevent interoperability with other ABI-compliant code. Generally you want optimization on stuff you want to release.

没有特别需求的情况下,gcc编译选项你只需要关注-Os这个选项,这个选项主要是对代码做优化的。

  1. -O0,-O1: the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.
  2. -O2:Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code.
  3. -O3:Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the extended optimization flags.

根据我已知的资料,O3的优化在某些时候可能会有问题,gcc对O3的实现估计有问题,所以最好只用到O2,O3不要用

2 调试信息的产生:-g

The presence of the “-g” option will cause extended symbol and source code information to be placed in the generated files, which is useful for debugging but increases the size of the file (and reveals your source code), which is something you often don’t want in “released” binaries.

But they’re not exclusive. You can have a binary compiled with optimization and debug info, or one with neither.

-g只是说要不要导出符号表到程序里面,它可以跟 -0s组合使用,比如:-O2 -g,即优化代码又导出符号表。

有时候我们使用了-g,使用gdb调试的时候,会出现“No symbol xxx in current context”
这样的问题。
这是因为比较新的gcc版本导出的符号格式默认是DWARF4,比较老的gdb版本无法识别这种格式。
这种问题可以通过加 -gdwarf-2或者-gdwarf-3告诉gcc,产生老版本的符号信息,这样老版本的gdb就
能识别

reference

[1] https://stackoverflow.com/questions/1534912/how-to-build-in-release-mode-with-optimizations-in-gcc

[2] http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

 类似资料: