构建调试目标时,最佳的g ++优化级别是什么? [英] What's the best g++ optimization level when building a debug target?

查看:92
本文介绍了构建调试目标时,最佳的g ++优化级别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你想要构建可调试的东西时(特别是g ++,但可能与gcc共享一个答案),最好的-O级别是什么?换句话说,构建调试目标而不是发布目标。

在比较-O0和-O1时,gcc联机文档有点粗略此处)。我的解释是,-O1只启用一个甚至可能影响可调试性的优化,即-fomit-frame-pointer。但是引用文档,它只在-O1中启用,这样做不会影响调试。我是否正确解释?

本网站的另一篇文章( here )专门讨论-O2,答案基本上是它可以工作,但是你的乱序执行。其中,国际海事组织(IMO)的范围可以从烦人到破坏性,取决于事情的发展情况。

解决方案

那么......适合调试版本的标志是什么?


无论你喜欢调试什么。



使用 -g -O0 构建时,调试是最简单的,但代码运行速度非常缓慢。



使用 -g -O1 构建时,您将开始观察优化有时



使用 -g -O2 ,你会注意到很多优化 。打印变量时,你会得到优化,你会在代码中得到意想不到的跳跃等。



使用 -g -O3 您会看到相同的症状,但频率更高。



GCC实际上并没有超过 -O3 的水平,所以这是行的末尾。



理解转换的人使用 -O3 执行的GCC在调试代码时几乎没有任何问题(您可以随时查看程序集,找出所需变量的实际位置居住,并从那里去)。但对于凡人来说,通常很难调试 -O2 代码。



[1]目前的工作在GDB和GCC中减少优化出的实例的数量,但尚未完成。


When you want to build something that's debuggable (g++ specifically, but perhaps shares an answer with gcc), what's the best -O level? In other words, when building a "debug" target rather than a "release" target.

The gcc online docs are a little sketchy when comparing -O0 and -O1 (here). My interpretation is that -O1 only enables one optimization that even may affect debugability, which is -fomit-frame-pointer. But to quote the doc, it's only enabled in -O1 "where doing so does not interfere with debugging." Am I interpreting that correctly?

Another post on this site (here) talks about -O2 specifically, and the answer is basically "it works but you get out-of-order execution". Which, IMO, can range from annoying to devastating depending on how badly things jump around.

解决方案

So... what flags are appropriate for the debug build?

Whatever you are comfortable debugging.

When you build with -g -O0, debugging is easiest, but the code runs very slowly.

When you build with -g -O1, you will start observing optimization sometimes. You'll try to step into a function, and discover that it got inlined, etc.

With -g -O2, you'll notice optimization a lot. You'll get optimized out when printing variables [1], you'll get unexpected jumping around in the code, etc.

With -g -O3 you'll see the same symptoms, but more frequently.

GCC doesn't actually have levels beyond -O3, so that's the end of the line.

People who understand transformations that GCC performed with -O3 will have little trouble debugging that code (you can always peek at the assembly, figure out where the variable you want actually resides, and go from there). But for mere mortals it is usually quite hard to debug -O2 code.

[1] There is current work in GDB and GCC to reduce the number of optimized out instances, but it's not finished yet.

这篇关于构建调试目标时,最佳的g ++优化级别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆