|Using the GNU Compiler Collection (GCC)|
On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but will probably make other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).
GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.
Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.
The following options are useful when GCC is generated with the
capability for more than one debugging format.
Note that with DWARF version 2 some ports require, and will always use, some non-conflicting DWARF 3 extensions in the unwind tables.
Version 4 may require GDB 7.0 and -fvar-tracking-assignments
for maximum benefit.
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, but no information about local variables and no line numbers.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
-gdwarf-2 does not accept a concatenated debug level, because
GCC used to support an option -gdwarf that meant to generate
debug information in version 1 of the DWARF format (which is very
different from version 2), and it would have been too confusing. That
debug format is long obsolete, but the option cannot be changed now.
Instead use an additional -glevel option to change the
debug level for DWARF.
.), the name of the dump file will be determined by appending
.gkdto the compilation output file name.
If the equal sign is omitted, the default -gtoggle is used.
The environment variable GCC_COMPARE_DEBUG, if defined, non-empty and nonzero, implicitly enables -fcompare-debug. If GCC_COMPARE_DEBUG is defined to a string starting with a dash, then it is used for opts, otherwise the default -gtoggle is used.
-fcompare-debug=, with the equal sign but without opts, is equivalent to -fno-compare-debug, which disables the dumping of the final representation and the second compilation, preventing even GCC_COMPARE_DEBUG from taking effect.
To verify full coverage during -fcompare-debug testing, set
GCC_COMPARE_DEBUG to say ‘-fcompare-debug-not-overridden’,
which GCC will reject as an invalid option in any actual compilation
(rather than preprocessing, assembly or linking). To get just a
warning, setting GCC_COMPARE_DEBUG to ‘-w%n-fcompare-debug
not overridden’ will do.
.gkadditional extension during the second compilation, to avoid overwriting those generated by the first.
When this option is passed to the compiler driver, it causes the
first compilation to be skipped, which makes it useful for little
other than debugging the compiler proper.
This option substantially reduces the size of debugging information, but at significant potential loss in type information to the debugger. See -femit-struct-debug-reduced for a less aggressive option. See -femit-struct-debug-detailed for more detailed control.
This option works only with DWARF 2.
This option significantly reduces the size of debugging information, with some potential loss in type information to the debugger. See -femit-struct-debug-baseonly for a more aggressive option. See -femit-struct-debug-detailed for more detailed control.
This option works only with DWARF 2.
This option is a detailed version of -femit-struct-debug-reduced and -femit-struct-debug-baseonly, which will serve for most needs.
A specification has the syntax
The optional first word limits the specification to structs that are used directly (‘dir:’) or used indirectly (‘ind:’). A struct type is used directly when it is the type of a variable, member. Indirect uses arise through pointers to structs. That is, when use of an incomplete struct would be legal, the use is indirect. An example is ‘struct one direct; struct two * indirect;’.
The optional second word limits the specification to ordinary structs (‘ord:’) or generic structs (‘gen:’). Generic structs are a bit complicated to explain. For C++, these are non-explicit specializations of template classes, or non-template classes within the above. Other programming languages have generics, but ‘-femit-struct-debug-detailed’ does not yet implement them.
The third word specifies the source files for those structs for which the compiler will emit debug information. The values ‘none’ and ‘any’ have the normal meaning. The value ‘base’ means that the base of name of the file in which the type declaration appears must match the base of the name of the main compilation file. In practice, this means that types declared in foo.c and foo.h will have debug information, but types declared in other header will not. The value ‘sys’ means those types satisfying ‘base’ or declared in system or compiler headers.
You may need to experiment to determine the best settings for your application.
The default is ‘-femit-struct-debug-detailed=all’.
This option works only with DWARF 2.
.eh_framesection instead of using GAS
static means that the function manipulates the stack
statically: a fixed number of bytes are allocated for the frame on function
entry and released on function exit; no stack adjustments are otherwise made
in the function. The second field is this fixed number of bytes.
dynamic means that the function manipulates the stack
dynamically: in addition to the static allocation described above, stack
adjustments are made in the body of the function, for example to push/pop
arguments around function calls. If the qualifier
bounded is also
present, the amount of these adjustments is bounded at compile time and
the second field is an upper bound of the total amount of stack used by
the function. If it is not present, the amount of these adjustments is
not bounded at compile time and the second field only represents the
forkcalls are detected and correctly handled (double counting will not happen).
With -fprofile-arcs, for each function of your program GCC
creates a program flow graph, then finds a spanning tree for the graph.
Only arcs that are not on the spanning tree have to be instrumented: the
compiler adds code to count the number of times that these arcs are
executed. When an arc is the only exit or only entrance to a block, the
instrumentation code can be added to the block; otherwise, a new basic
block must be created to hold the instrumentation code.
# disable ccp1 for all functions -fdisable-tree-ccp1 # disable complete unroll for function whose cgraph node uid is 1 -fenable-tree-cunroll=1 # disable gcse2 for functions at the following ranges [1,1], # [300,400], and [400,1000] # disable gcse2 for functions foo and foo2 -fdisable-rtl-gcse2=foo,foo2 # disable early inlining -fdisable-tree-einline # disable ipa inlining -fdisable-ipa-inline # enable tree full unroll -fenable-tree-unroll
Debug dumps can be enabled with a -fdump-rtl switch or some -d option letters. Here are the possible letters for use in pass and letters, and their meanings:
-fdump-translation-unit-options (C++ only)
-fdump-class-hierarchy-options (C++ only)
DECL_ASSEMBLER_NAMEhas been set for a given decl, use that in the dump instead of
DECL_NAME. Its primary use is ease of use working backward from mangled names in the assembly file.
DECL_UID) for each variable.
The following tree dumps are possible:
The string should be different for every file you compile.
For n greater than zero, -fsched-verbose outputs the
same information as -fdump-rtl-sched1 and -fdump-rtl-sched2.
For n greater than one, it also output basic block probabilities,
detailed ready list information and unit/insn info. For n greater
than two, it includes RTL at abort point, control-flow and regions info.
And for n over four, -fsched-verbose also includes
When used in combination with the -x command-line option, -save-temps is sensible enough to avoid over writing an input source file with the same extension as an intermediate file. The corresponding intermediate file may be obtained by renaming the source file before using -save-temps.
If you invoke GCC in parallel, compiling several different source files that share a common base name in different subdirectories or the same source file compiled for multiple output destinations, it is likely that the different parallel compilers will interfere with each other, and overwrite the temporary files. For instance:
gcc -save-temps -o outdir1/foo.o indir1/foo.c& gcc -save-temps -o outdir2/foo.o indir2/foo.c&
may result in foo.i and foo.o being written to
simultaneously by both compilers.
gcc -save-temps=obj -c foo.c gcc -save-temps=obj -c bar.c -o dir/xbar.o gcc -save-temps=obj foobar.c -o dir2/yfoobar
would create foo.i, foo.s, dir/xbar.i,
dir/xbar.s, dir2/yfoobar.i, dir2/yfoobar.s, and
Without the specification of an output file, the output looks like this:
# cc1 0.12 0.01 # as 0.00 0.01
The first number on each line is the “user time”, that is time spent executing the program itself. The second number is “system time”, time spent executing operating system routines on behalf of the program. Both numbers are in seconds.
With the specification of an output file, the output is appended to the named file, and it looks like this:
0.12 0.01 cc1 options 0.00 0.01 as options
The “user time” and the “system time” are moved before the program
name, and the options passed to the program are displayed, so that one
can later tell what file was being compiled, and with which options.
It is enabled by default when compiling with optimization (-Os,
-O, -O2, ...), debugging information (-g) and
the debug info format supports it.
It can be enabled even if var-tracking is disabled, in which case
annotations will be created and maintained, but discarded at the end.
This is useful when you use -nostdlib or -nodefaultlibs but you do want to link with libgcc.a. You can do
gcc -nostdlib files... `gcc -print-libgcc-file-name`
This is useful when gcc prints the error message
‘installation problem, cannot exec cpp0: No such file or directory’.
To resolve this you either need to put cpp0 and the other compiler
components where gcc expects to find them, or you can set the environment
variable GCC_EXEC_PREFIX to the directory where you installed them.
Don't forget the trailing ‘/’.
See Environment Variables.