How do I view the value of an lt;optimized outgt; variable in C++?(如何查看 lt;optimized outgt; 的值?C++中的变量?)
问题描述
我正在使用 gdb 调试 C++ 程序.
I am using gdb to debug a C++ program.
我有这个代码:
int x = floor(sqrt(3));
我想查看 x 的值.但是,gdb 声称 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也可以正常工作. - 您可以添加一些显式打印语句来记录输出值.
- 您通常也可以强制编译器通过将其设置为 volatile 来保留此特定值(但请记住在完成后将其取消设置为 volatile!).但是请注意,由于控制流在优化代码中也会发生变化,即使您可以看到变量的值,当您查看有问题的变量.
- You can reduce the optimization level to make it easier for the debugger to keep track of things.
-O0is certain to work (but will be quite a lot slower),-O1might 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.
这篇关于如何查看 <optimized out> 的值?C++中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何查看 <optimized out> 的值?C++中的变量
基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
