使用 g++ 和 -Og 选项优化的变量

2022-01-23 00:00:00 g++ c++ compiler-flags

当我使用 -Og 选项使用 g++ 编译我的 C++ 程序时,我看到了 <optimized out> 的变量,而且当前行有时会跳过.这种优化级别的行为是预期的,还是我有一些问题?gcc 的手册页说:

When I compile my C++ program with g++ using the -Og option I see variables that are <optimized out>, and also the current line sometimes skips around. Is this behaviour expected at this optimization level, or do I have some problem? The man page of gcc says:

-Og

优化调试体验.-Og 启用不干扰调试的优化.它应该是标准编辑-编译-调试周期的首选优化级别,提供合理的优化级别,同时保持快速编译和良好的调试体验.

-Og

Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.

因此我没想到会出现这种行为.在我的系统上,我有 g++ 4.9.2 版和 gdb 7.7.1 版.

hence I did not expect this behaviour. On my system I have g++ version 4.9.2 and gdb version 7.7.1.

推荐答案

这是使用 -Og 选项编译时的正常行为.在此优化级别,只要编译器遵循 as-if 规则.这可能包括删除变量(或转换为常量),以及删除未使用的函数.

This is normal behaviour when compiling with the -Og option. At this optimisation level the compiler is allowed to optimize as long as it adheres to the as-if rule. This could include the removal of variables (or conversion to constants), as well as the dropping of unused functions.

建议要么习惯跳过,要么使用-O0选项进行编译.

The recommendation is either to get used to the skipping or to compile with the -O0option.

相关文章