Step over occationally steps to previous line using gdb with both -g and -O2 flags. Remove the -O2 flags and it works as expected. Can anyone explain why that happens ?
I'm new to gdb in e开发者_如何学Goclipse CDT and use it on C++ code with "Standard create process launcher".
Is this expected behaviour or is there a solution leaving optimization in ?
It's expected. The compiler will feel free to re-order code on higher optimization levels. The man page even says that:
"Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results."
That goal has to fall by the wayside at higher levels.
Optimization may re-arrange your code in sometimes unexpected ways. The debug information in the optimized code will have followed those re-arrangements around.
There are no requirements that the code is executed exactly in the order you have written it, just that the result, "the observable behavior", is the same as-if it did.
§1.9:
The least requirements on a conforming implementation are:
— Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
— At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.
— The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.These collectively are referred to as the observable behavior of the program.
精彩评论