21

Just to give some context, I'm talking about compiling C++ code with g++ here.

I can see how including the -g flag for production builds would be convenient for maintenance: the program will be much easier to debug if it crashes unexpectedly.

My question here is, does including the -g flag affect the output executable in any other way than increasing its size? Can it somehow make the code slower (e.g. by turning off certain optimizations)?

From what I understand, it shouldn't (the documentation only mentions the inclusion of debug symbols), but I'm not sure.

2 Answers2

19

The -g flag does not affect code generation, only the symbol table and debug metadata are changed. Those do not live in the executable code section, so they won't even affect performance when the code is run outside of hte debugger.

Andy Ross
  • 11,699
  • 1
  • 34
  • 31
  • how about code security? Wouldn't leaving the debugger info make the binary more susceptible to leak information? For example, in case of a buffer overflow, the system may send data on its binary and if this data is debug info, it could be like sending the source code, which could enable further investigative and exploitable attacks. – b.g. May 01 '20 at 16:25
1

My question here is, does including the -g flag affect the output executable in any other way than increasing its size?

No, it is perfectly possible to produce optimized binaries with debugging info, which doesn't affect the normal code in any way (although that info might be less useful, since variables need not exist at times, inlined functions are harder to debug etc.)

The Debian distribution builds packages with debugging info, which is stripped later on (sometimes split into a "debugging package").

Note however that the size increase might be quite big.

jpalecek
  • 47,058
  • 7
  • 102
  • 144