16

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.

3 Answers3

15

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,255
  • 1
  • 31
  • 30
  • 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
7

I remember reading that certain optimizations are turned off with debug symbols: How Does The Debugging Option -g Change the Binary Executable?

Googling also shows more posts related to this topic.

I don't think it will really affect you unless your code is very performance-sensitive, though; and, other than that, I don't know of any downsides off the top of my head (other than larger binaries).

Community
  • 1
  • 1
user541686
  • 197,378
  • 118
  • 507
  • 856
  • 2
    As I read these documents, only compilers other than gcc have optimizations affected by -g. My experience is that for gcc it goes the other direction: what you can see and do in the debugger is affected by optimizations - sometimes severely. Of course this is what you'd expect from aggressive optimization: you can't debug what isn't there. – Gene Jun 11 '12 at 22:49
  • @Gene: Oh I see... interesting, thanks for pointing it out. So seems like I was the one who was wrong about GCC then! – user541686 Jun 12 '12 at 00:00
0

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
  • 45,889
  • 7
  • 97
  • 139