How to turn off all optimizations in GCC? Using -O0 does not work since it still optimizes out the statements that have no effects, or any code that is after an infinite loop without any break statements.
The gcc option -O enables different levels of optimization. Use -O0 to disable them and use -S to output assembly.
GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).
The compiler optimizes to reduce the size of the binary instead of execution speed. If you do not specify an optimization option, gcc attempts to reduce the compilation time and to make debugging always yield the result expected from reading the source code.
GCC has since added -Og to bring the total to 8. From the man page: -O0 (do no optimization, the default if no optimization level is specified) -O1 (optimize minimally)
There is no way to make gcc not ignore unreachable code and statments that have no effect.
What you can do is make code that is unreachable appear to be reachable by using volatile variables.
volatile bool always_true = true;
if( always_true )
{
//infinite loop
//return something
}
//Useless code
in the above example, gcc won't optomize out useless code because it cannot know it is infact useless
int a = 5;
int b = 5;
volatile int c = 9;
c += 37;
return a + b;
In this example, integer c won't be optimized out because gcc does can't know it is dead weight code.
You have to make your code nearly impossible to be optimized by the compiler. For example:
volatile
keyword on variables that you wish not to be optimizedIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With