5.7.6.5 G: Produce Debugging Information Option

The -g option instructs the compiler to produce additional information, which can be used by hardware tools to debug your program.

The option can be used with -O, making it possible to debug optimized code. The shortcuts taken by optimized code may occasionally produce surprising results:

  • Some declared variables may not exist at all
  • Flow of control may briefly move unexpectedly
  • Some statements may not be executed because they compute constant results or their values were already at hand
  • Some statements may execute in different places because they were moved out of loops

Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.