How to disable compiler optimizations in gcc?
I am trying to learn assembly language. I have searched and found how to disassemble a .c
file but I think it produces some optimized version of the program. Is there any way so that I can see the exact assembly code which corresponds to my C file.
The gcc option -O
enables different levels of optimization. Use -O0
to disable them and use -S
to output assembly. -O3
is the highest level of optimization.
Starting with gcc 4.8 the optimization level -Og
is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.
To change the dialect of the assembly to either intel or att use -masm=intel
or -masm=att
.
You can also enable certain optimizations manually with -fname
.
Have a look at the gcc manual for much more.
For gcc you want to omit any -O1 -O2 or -O3 options passed to the compiler or if you already have them you can append the -O0 option to turn it off again. It might also help you to add -g for debug so that you can see the c source and disassembled machine code in your debugger.
See also: http://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html