Syntax_Error
Syntax_Error

Reputation: 6210

Understanding g++ compilation flags

I want to know what each flag does to the code to understand its overhead. I checked

man g++ for the '-g' flag

and found only "Generate extra code to write profile...."

It doesn't specify where?

Are their any detailed manuals to help me understand what -g/-p do?

Upvotes: 4

Views: 17012

Answers (3)

Ahmed Masud
Ahmed Masud

Reputation: 22374

Interesting question, and it's a bit hard to find information if your starting point is the gcc man page :-) so here goes.

Basically the -g flag writes extra "debugging" information right into the generated object files (.o) and executable file. This extra information can then be used by a debugger (say gdb) to help make sense of what's going on for the person doing the debugging.

So for example if you have a variable name that will be kept around as extra information so that when you use a debugger you can refer to the variable that you used in your source code rather than some random memory address because symbol debug information wasn't there.

The debugging options are somewhat explained in the gcc manual here

However I think you need a debugging intro. So have a look at the GDB introduction by UWA to get a better understanding of what's going on.

The same goes for profiling data. -p adds extra information in the executable so that a profiler like prof can trace the software as its running and tell you where it spends most of its time (what loops/functions etc) and how (in)efficient a program is. :-)

related things to read up on

There is a difference between symbol information in a file and debugging information. Once you start looking at linking you'll run into symbol resolution.

Upvotes: 5

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

The thing is they provide this statement: "Generate extra code to write profile information suitable for the analysis program prof" Non are providing information about "what type of information is suitable for analysis.

It's abstracted away from you, because you do not need to know.

If you deem otherwise, examine the compiler's source code.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129314

"Generate extra code to write profile" is the -p option, and that will add extra code. The extent of that extra code is at least one register load and one function call at start and end of functions, but it does depend on the architecture. This may in turn affect other optimisations during compilation, such as availability of registers, inlining and when actually running the code it may affect cache-hit rates/miss-rates, and aside from the direct effect of executing those extra instructions.

The -g option does not in itself add extra code, it just produces debug symbols that become part of the executable file so that a debugger can figure out where the functions, variables, etc are when you use the debugger.

Upvotes: 8

Related Questions