我最近检查了一些由一些在该领域(编程等)非常熟练的作者制作的Makefile。这些 makefile 适用于在 Linux 系统上使用 gcc 编译器编译的程序。
在其中一些文件中,我在 CCFLAGS 和 LDFLAGS 选项中发现了“-g”标志,即使在非调试目标中也是如此。我想知道它的作用,发现它向代码添加了一些基本的调试信息。
我想知道对性能有什么负面影响?我是否应该更好地将它们从我自己的非调试目标中删除,或者当实际没有使用调试器或分析器时负面影响可以忽略不计?
最佳答案
-g
参数本身不应对程序的性能产生负面影响:
GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.
(参见 https://gcc.gnu.org/onlinedocs/gcc-4.4.6/gcc/Debugging-Options.html#Debugging-Options )
因此,您可以将 -g
和 -O
结合起来,尽管您应该被警告,上面提到的奇怪的事情可能会发生。因此,总的来说,将发布代码与调试符号结合起来是没有意义的,除非您尝试在客户站点上调试代码。但当您这样做时,您可能不会获得最佳的调试体验。
但请记住,调试会大大增加程序的文件大小。
如果有任何预期的副作用,您只能通过您的实现进行基准测试,您无法将其他人获得的知识转移到您的代码/算法中。
关于c - 海湾合作委员会 : Compiling with basic debbuging information added (flag -g) what are the effects on the performance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34244292/