Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables optimized out with g++ and the -Og option

Tags:

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

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.

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

like image 826
Svaberg Avatar asked Jul 15 '15 16:07

Svaberg


People also ask

What does optimized out mean?

(transitive, programming) To omit (some portion of program logic) through optimization, when it is found to be unused or unnecessary.

How do I avoid optimized in GDB?

You need to turn off the compiler optimisation. If you are interested in a particular variable in gdb, you can delare the variable as "volatile" and recompile the code. This will make the compiler turn off compiler optimization for that variable.

What does optimize code do?

Code optimization is any method of code modification to improve code quality and efficiency. A program may be optimized so that it becomes a smaller size, consumes less memory, executes more rapidly, or performs fewer input/output operations.

How do I know if GCC is not optimized?

Compiler specific pragma gcc provides pragma GCC as a way to control temporarily the compiler behavior. By using pragma GCC optimize("O0") , the optimization level can be set to zero, which means absolutely no optimize for gcc.


1 Answers

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.

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

like image 129
Svaberg Avatar answered Sep 19 '22 20:09

Svaberg