To use the libstdc++ debug mode, compile your application with the
compiler flag -D_GLIBCXX_DEBUG
. Note that this flag
changes the sizes and behavior of standard class templates such
as std::vector
, and therefore you can only link code
compiled with debug mode and code compiled without debug mode if no
instantiation of a container is passed between the two translation
units.
By default, error messages are formatted to fit on lines of about
78 characters. The environment variable
GLIBCXX_DEBUG_MESSAGE_LENGTH
can be used to request a
different length.
Note that libstdc++ is able to produce backtraces on error.
To enable these, compile with -D_GLIBCXX_DEBUG_BACKTRACE
and then link with -lstdc++exp
.
These backtraces are not supported on all platforms.
When it is not feasible to recompile your entire application, or only specific containers need checking, debugging containers are available as GNU extensions. These debugging containers are functionally equivalent to the standard drop-in containers used in debug mode, but they are available in a separate namespace as GNU extensions and may be used in programs compiled with either release mode or with debug mode. The following table provides the names and headers of the debugging containers:
Table 17.1. Debugging Containers
Container | Header | Debug container | Debug header |
---|---|---|---|
std::bitset | bitset | __gnu_debug::bitset | <debug/bitset> |
std::deque | deque | __gnu_debug::deque | <debug/deque> |
std::list | list | __gnu_debug::list | <debug/list> |
std::map | map | __gnu_debug::map | <debug/map> |
std::multimap | map | __gnu_debug::multimap | <debug/map> |
std::multiset | set | __gnu_debug::multiset | <debug/set> |
std::set | set | __gnu_debug::set | <debug/set> |
std::string | string | __gnu_debug::string | <debug/string> |
std::wstring | string | __gnu_debug::wstring | <debug/string> |
std::basic_string | string | __gnu_debug::basic_string | <debug/string> |
std::vector | vector | __gnu_debug::vector | <debug/vector> |
When compiling in C++11 mode (or newer), these containers have additional debug capability.
Table 17.2. Debugging Containers C++11
Container | Header | Debug container | Debug header |
---|---|---|---|
std::forward_list | forward_list | __gnu_debug::forward_list | <debug/forward_list> |
std::unordered_map | unordered_map | __gnu_debug::unordered_map | <debug/unordered_map> |
std::unordered_multimap | unordered_map | __gnu_debug::unordered_multimap | <debug/unordered_map> |
std::unordered_set | unordered_set | __gnu_debug::unordered_set | <debug/unordered_set> |
std::unordered_multiset | unordered_set | __gnu_debug::unordered_multiset | <debug/unordered_set> |
Prior to GCC 11 a debug version of std::array
was available as __gnu_debug::array
in the
header <debug/array>
.
Because array::iterator
is just a pointer,
the debug array
can't check iterator operations,
it can only check direct accesses to the container.
Starting with GCC 11 all the debug capabilities are available in
std::array
, without needing a separate type,
so __gnu_debug::array
is just an alias for
std::array
.
That alias is deprecated and may be removed in a future release.