I'm only using my program for myself. Should I just always compile it with -g3 (Debug max) because sometimes I need to debug it?
If with -g0 (Debug none) my program executes faster comparing to -g3 (Debug max)?
About the -gLEVEL
(from gcc manual):
Request debugging information and also use level to specify how much information. The default level is 2.
Level 0 produces no debug information at all. Thus, -g0 negates -g.
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, and line number tables, but no information about local variables.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
So the difference between -g0
and -g3
is that with level 0 you don't get debugging symbols, with level 3 you get a lot of symbols.
Anyway the debug symbols are located in totally different sections from the code/data sections. You can check with objdump
(and you can read How do debug symbols affect performance of a Linux executable compiled by GCC?).
Strictly speaking with -g0
you shouldn't get a faster program BUT with many debug symbols program load time can be longer.
It's interesting to note that for GCC the presence of debug symbols (-g
) and the optimization level (e.g. -O2
) are orthogonal, you can use -g -O2
without losing compiler optimization (you just get less useful debug info because optimized code less closely resembles the original source code).
I'd also consider the -Og
optimization level (introduced with GCC 4.8):
A new general optimization level,
-Og
, has been introduced. It addresses the need for fast compilation and a superior debugging experience while providing a reasonable level of runtime performance. Overall experience for development should be better than the default optimization level-O0
.