How to disable all optimizations in GCC

How to disable all optimizations in GCC? Using -O0 does not work, because it still optimizes statements that have no effects, or any code that after an infinite loop without any break statements.

+8
c compiler-optimization gcc
source share
2 answers

It is not possible to force gcc to ignore unreachable code and statuses that have no effect.

What you can do is make the unreachable code seem to be accessible using mutable variables.

volatile bool always_true = true; if( always_true ) { //infinite loop //return something } //Useless code 

in the above example, gcc will not fertilize useless code because it cannot know that it is useless

 int a = 5; int b = 5; volatile int c = 9; c += 37; return a + b; 

In this example, the integer c will not be optimized, because gcc cannot know that this is dead weight code.

+7
source share

You should make your code almost impossible for optimization by the compiler. For example:

  • use the volatile keyword for variables that you do not want to optimize.
  • make sure that the code has an effect, for example: not only changing the value of a variable, but also printing the value or saving it in another variable or arithmetic for the variable and saving it in another variable
  • refers to a variable in another function to ensure that the compiler cannot judge that it is not used at compile time.
+2
source share

All Articles