如何查看< optimized out>的值变量在C ++? [英] How do I view the value of an <optimized out> variable in C++?

查看:2526
本文介绍了如何查看< optimized out>的值变量在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gdb来调试C ++程序。

I am using gdb to debug a C++ program.

我有这个代码:

int x = floor(sqrt(3));

,我想查看x的值。然而,gdb声称x是< optimized_out>。如何查看x的值?我应该改变我的编译器标志吗?

and I want to view the value of x. However, gdb claims that x is "< optimized_out >". How do I view the value of x? Should I change my compiler flags?

推荐答案

在高优化级别,编译器可以消除中间值,有很多选项:

On high optimization levels, the compiler can eliminate intermediate values, as you have seen here. There are a number of options:


  • 您可以降低优化级别,使调试器更容易跟踪事情。 -O0 一定会工作(但会慢很多), -O1 也可以正常工作。

  • 您可以添加一些显式打印语句以记录输出值。

  • 您通常也可以强制编译器保留此特定值(但是要记住,当你完成后,不要让它变得不稳定)。但请注意,由于控制流程也会在优化代码中发生变化,即使您可以看到变量的值,也可能不清楚您在查看代码时的代码中的哪一点

  • You can reduce the optimization level to make it easier for the debugger to keep track of things. -O0 is certain to work (but will be quite a lot slower), -O1 might work okay as well.
  • You can add some explicit print statements to log the output value.
  • You can also usually force the compiler to retain this specific value by making it volatile (but remember to un-make it volatile when you're done!). Note, however, that since control flow is also subject to alteration in optimized code, even if you can see the value of the variable, it may not be entirely clear what point in the code you're at when you're looking at the variable in question.

这篇关于如何查看&lt; optimized out&gt;的值变量在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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