Contents|Index|Previous|Next
C and C++

The following documentation discusses using C and C++ languages with GDB. Since C and C++ are so closely related, many features of GDB apply to both languages. Whenever this is the case, we discuss those languages together.

The C++ debugging facilities are jointly implemented by the GNU C++ compiler and GDB. Therefore, to debug your C++ code effectively, you must compile your C++ programs with the GNU C++ compiler, g++.

For best results when debugging C++ programs, use the stabs debugging format. You can select that format explicitly with the g++  ‘-gstabs’ or ‘-gstabs+’ command-line options. See Options for debugging your program or GNU CC in Using GNU CC in GNUPro Compiler Tools for more information.

C and C++ operators

Operators must be defined on values of specific types. For instance, + is defined on numbers and not on structures. Operators are often defined on groups of types. For the purposes of C and C++, the following definitions hold.

The following operators are supported. They are listed in order of increasing precedence:
  C and C++ constants

GDB allows you to express the constants of C and C++ in the following ways:

C++ expressions

GDB expression handling can interpret most C++ expressions.

C and C++ defaults

If you allow GDB to set type and range checking automatically, they both default to off whenever the working language changes to C or C++.

This happens regardless of whether you or GDB selects the working language.

If you allow GDB to set the language automatically, it recognizes source files whose names end with ‘.c’, ‘.C’, or ‘.cc’, and when GDB enters code compiled from one of these files, it sets the working language to C or C++. See Having GDB infer the source language for further details.

C and C++ type and range checks

By default, when GDB parses C or C++ expressions, type checking is not used. However, if you turn type checking on, GDB considers two variables type equivalent if:

Range checking, if turned on, is done on mathematical operations. Array indices are not checked, since they are often used to index a pointer that is not itself an array.

GDB and C

The set print union and show print union commands apply to the union type. When set to ‘on’, any union that is inside a struct or class is also printed. Otherwise, it appears as ‘{...}’.

The @ operator aids in the debugging of dynamic arrays, formed with pointers and a memory allocation function. See Expressions.

GDB features for C++

Some GDB commands are particularly useful with C++, and some are designed specifically for use with C++. The following is a summary:
 


Top|Contents|Index|Previous|Next