Search code examples
gccgdbmappingsymbol-table

Why gcc -Og doesn't generate source code-line mapping?


I've got a simple .c file:

$ cat a.c
int i=0;
int j;
static int l=4;
void k(){}
void main(){
  k();
}

I tried to compile it with "-Og" to enable debug information. GCC man says

-Og Optimize debugging experience.
-Og enables optimizations that do not interfere with debugging.

But when I use "gdb a.out" and try "list", it says:

(gdb) l
No symbol table read. Please use "file" command.

Well this is so weird as out of my expectation, If I use gcc a.c -g, "list" is fine in gdb. Why is that?


Solution

  • -Og is optimization switch. It enables any optimizations that don't interfere with debugging, but says nothing about debug information.

    -g enables debug information (which is usually a separate non-code section).

    So, use -Og -g or similar, to enable debug information and debug optimizations.

    Your build with -g only enabled debug information, but left optimization at default level (which is completely disabled).

    More information about debug options in GCC manual, including this: "If you are not using some other optimization option, consider using -Og (see Optimize Options) with -g. With no -O option at all, some compiler passes that collect information useful for debugging do not run at all, so that -Og may result in a better debugging experience."