How to turn off all optimizations in GCC

c, compiler-optimization, gcc

Solution

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.

Problem

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.

Original source

Related problems