2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
6 <chapter id="manual.intro.using" xreflabel="Using">
7 <?dbhtml filename="using.html"?>
12 <sect1 id="manual.intro.using.flags" xreflabel="Flags">
13 <title>Command Options</title>
15 The set of features available in the GNU C++ library is shaped
17 several <ulink url="http://gcc.gnu.org/onlinedocs/gcc-4.3.2//gcc/Invoking-GCC.html">GCC
18 Command Options</ulink>. Options that impact libstdc++ are
19 enumerated and detailed in the table below.
23 By default, <command>g++</command> is equivalent to <command>g++ -std=gnu++98</command>. The standard library also defaults to this dialect.
27 <title>C++ Command Options</title>
28 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
29 <colspec colname='c1'></colspec>
30 <colspec colname='c2'></colspec>
34 <entry>Option Flags</entry>
35 <entry>Description</entry>
41 <entry><literal>-std=c++98</literal></entry>
42 <entry>Use the 1998 ISO C++ standard plus amendments.</entry>
46 <entry><literal>-std=gnu++98</literal></entry>
47 <entry>As directly above, with GNU extensions.</entry>
51 <entry><literal>-std=c++0x</literal></entry>
52 <entry>Use the working draft of the upcoming ISO C++0x standard.</entry>
56 <entry><literal>-std=gnu++0x</literal></entry>
57 <entry>As directly above, with GNU extensions.</entry>
61 <entry><literal>-fexceptions</literal></entry>
62 <entry>See <link linkend="intro.using.exception.no">exception-free dialect</link></entry>
66 <entry><literal>-frtti</literal></entry>
67 <entry>As above, but RTTI-free dialect.</entry>
71 <entry><literal>-pthread</literal> or <literal>-pthreads</literal></entry>
72 <entry>For ISO C++0x <thread>, <future>,
73 <mutex>, or <condition_variable>.</entry>
77 <entry><literal>-fopenmp</literal></entry>
78 <entry>For <link linkend="manual.ext.parallel_mode">parallel</link> mode.</entry>
87 <sect1 id="manual.intro.using.headers" xreflabel="Headers">
88 <?dbhtml filename="using_headers.html"?>
89 <title>Headers</title>
91 <sect2 id="manual.intro.using.headers.all" xreflabel="Header Files">
92 <title>Header Files</title>
95 The C++ standard specifies the entire set of header files that
96 must be available to all hosted implementations. Actually, the
97 word "files" is a misnomer, since the contents of the
98 headers don't necessarily have to be in any kind of external
99 file. The only rule is that when one <code>#include</code>'s a
100 header, the contents of that header become available, no matter
105 That said, in practice files are used.
109 There are two main types of include files: header files related
110 to a specific version of the ISO C++ standard (called Standard
111 Headers), and all others (TR1, C++ ABI, and Extensions).
115 Two dialects of standard headers are supported, corresponding to
116 the 1998 standard as updated for 2003, and the draft of the
117 upcoming 200x standard.
121 C++98/03 include files. These are available in the default compilation mode, i.e. <code>-std=c++98</code> or <code>-std=gnu++98</code>.
125 <title>C++ 1998 Library Headers</title>
126 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
127 <colspec colname='c1'></colspec>
128 <colspec colname='c2'></colspec>
129 <colspec colname='c3'></colspec>
130 <colspec colname='c4'></colspec>
133 <entry><filename class="headerfile">algorithm</filename></entry>
134 <entry><filename class="headerfile">bitset</filename></entry>
135 <entry><filename class="headerfile">complex</filename></entry>
136 <entry><filename class="headerfile">deque</filename></entry>
137 <entry><filename class="headerfile">exception</filename></entry>
140 <entry><filename class="headerfile">fstream</filename></entry>
141 <entry><filename class="headerfile">functional</filename></entry>
142 <entry><filename class="headerfile">iomanip</filename></entry>
143 <entry><filename class="headerfile">ios</filename></entry>
144 <entry><filename class="headerfile">iosfwd</filename></entry>
147 <entry><filename class="headerfile">iostream</filename></entry>
148 <entry><filename class="headerfile">istream</filename></entry>
149 <entry><filename class="headerfile">iterator</filename></entry>
150 <entry><filename class="headerfile">limits</filename></entry>
151 <entry><filename class="headerfile">list</filename></entry>
154 <entry><filename class="headerfile">locale</filename></entry>
155 <entry><filename class="headerfile">map</filename></entry>
156 <entry><filename class="headerfile">memory</filename></entry>
157 <entry><filename class="headerfile">new</filename></entry>
158 <entry><filename class="headerfile">numeric</filename></entry>
161 <entry><filename class="headerfile">ostream</filename></entry>
162 <entry><filename class="headerfile">queue</filename></entry>
163 <entry><filename class="headerfile">set</filename></entry>
164 <entry><filename class="headerfile">sstream</filename></entry>
165 <entry><filename class="headerfile">stack</filename></entry>
168 <entry><filename class="headerfile">stdexcept</filename></entry>
169 <entry><filename class="headerfile">streambuf</filename></entry>
170 <entry><filename class="headerfile">string</filename></entry>
171 <entry><filename class="headerfile">utility</filename></entry>
172 <entry><filename class="headerfile">typeinfo</filename></entry>
175 <entry><filename class="headerfile">valarray</filename></entry>
176 <entry><filename class="headerfile">vector</filename></entry>
184 <title>C++ 1998 Library Headers for C Library Facilities</title>
185 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
186 <colspec colname='c1'></colspec>
187 <colspec colname='c2'></colspec>
188 <colspec colname='c3'></colspec>
189 <colspec colname='c4'></colspec>
192 <entry><filename class="headerfile">cassert</filename></entry>
193 <entry><filename class="headerfile">cerrno</filename></entry>
194 <entry><filename class="headerfile">cctype</filename></entry>
195 <entry><filename class="headerfile">cfloat</filename></entry>
196 <entry><filename class="headerfile">ciso646</filename></entry>
199 <entry><filename class="headerfile">climits</filename></entry>
200 <entry><filename class="headerfile">clocale</filename></entry>
201 <entry><filename class="headerfile">cmath</filename></entry>
202 <entry><filename class="headerfile">csetjmp</filename></entry>
203 <entry><filename class="headerfile">csignal</filename></entry>
206 <entry><filename class="headerfile">cstdarg</filename></entry>
207 <entry><filename class="headerfile">cstddef</filename></entry>
208 <entry><filename class="headerfile">cstdio</filename></entry>
209 <entry><filename class="headerfile">cstdlib</filename></entry>
210 <entry><filename class="headerfile">cstring</filename></entry>
213 <entry><filename class="headerfile">ctime</filename></entry>
214 <entry><filename class="headerfile">cwchar</filename></entry>
215 <entry><filename class="headerfile">cwctype</filename></entry>
222 C++0x include files. These are only available in C++0x compilation
223 mode, i.e. <literal>-std=c++0x</literal> or <literal>-std=gnu++0x</literal>.
228 <title>C++ 200x Library Headers</title>
229 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
230 <colspec colname='c1'></colspec>
231 <colspec colname='c2'></colspec>
232 <colspec colname='c3'></colspec>
233 <colspec colname='c4'></colspec>
237 <entry><filename class="headerfile">algorithm</filename></entry>
238 <entry><filename class="headerfile">array</filename></entry>
239 <entry><filename class="headerfile">bitset</filename></entry>
240 <entry><filename class="headerfile">chrono</filename></entry>
241 <entry><filename class="headerfile">complex</filename></entry>
244 <entry><filename class="headerfile">condition_variable</filename></entry>
245 <entry><filename class="headerfile">deque</filename></entry>
246 <entry><filename class="headerfile">exception</filename></entry>
247 <entry><filename class="headerfile">forward_list</filename></entry>
248 <entry><filename class="headerfile">fstream</filename></entry>
251 <entry><filename class="headerfile">functional</filename></entry>
252 <entry><filename class="headerfile">future</filename></entry>
253 <entry><filename class="headerfile">initalizer_list</filename></entry>
254 <entry><filename class="headerfile">iomanip</filename></entry>
255 <entry><filename class="headerfile">ios</filename></entry>
258 <entry><filename class="headerfile">iosfwd</filename></entry>
259 <entry><filename class="headerfile">iostream</filename></entry>
260 <entry><filename class="headerfile">istream</filename></entry>
261 <entry><filename class="headerfile">iterator</filename></entry>
262 <entry><filename class="headerfile">limits</filename></entry>
265 <entry><filename class="headerfile">list</filename></entry>
266 <entry><filename class="headerfile">locale</filename></entry>
267 <entry><filename class="headerfile">map</filename></entry>
268 <entry><filename class="headerfile">memory</filename></entry>
269 <entry><filename class="headerfile">mutex</filename></entry>
272 <entry><filename class="headerfile">new</filename></entry>
273 <entry><filename class="headerfile">numeric</filename></entry>
274 <entry><filename class="headerfile">ostream</filename></entry>
275 <entry><filename class="headerfile">queue</filename></entry>
276 <entry><filename class="headerfile">random</filename></entry>
279 <entry><filename class="headerfile">ratio</filename></entry>
280 <entry><filename class="headerfile">regex</filename></entry>
281 <entry><filename class="headerfile">set</filename></entry>
282 <entry><filename class="headerfile">sstream</filename></entry>
283 <entry><filename class="headerfile">stack</filename></entry>
286 <entry><filename class="headerfile">stdexcept</filename></entry>
287 <entry><filename class="headerfile">streambuf</filename></entry>
288 <entry><filename class="headerfile">string</filename></entry>
289 <entry><filename class="headerfile">system_error</filename></entry>
290 <entry><filename class="headerfile">thread</filename></entry>
293 <entry><filename class="headerfile">tuple</filename></entry>
294 <entry><filename class="headerfile">type_traits</filename></entry>
295 <entry><filename class="headerfile">typeinfo</filename></entry>
296 <entry><filename class="headerfile">unordered_map</filename></entry>
297 <entry><filename class="headerfile">unordered_set</filename></entry>
300 <entry><filename class="headerfile">utility</filename></entry>
301 <entry><filename class="headerfile">valarray</filename></entry>
302 <entry><filename class="headerfile">vector</filename></entry>
312 <title>C++ 200x Library Headers for C Library Facilities</title>
313 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
314 <colspec colname='c1'></colspec>
315 <colspec colname='c2'></colspec>
316 <colspec colname='c3'></colspec>
317 <colspec colname='c4'></colspec>
318 <colspec colname='c5'></colspec>
321 <entry><filename class="headerfile">cassert</filename></entry>
322 <entry><filename class="headerfile">ccomplex</filename></entry>
323 <entry><filename class="headerfile">cctype</filename></entry>
324 <entry><filename class="headerfile">cerrno</filename></entry>
325 <entry><filename class="headerfile">cfenv</filename></entry>
328 <entry><filename class="headerfile">cfloat</filename></entry>
329 <entry><filename class="headerfile">cinttypes</filename></entry>
330 <entry><filename class="headerfile">ciso646</filename></entry>
331 <entry><filename class="headerfile">climits</filename></entry>
332 <entry><filename class="headerfile">clocale</filename></entry>
335 <entry><filename class="headerfile">cmath</filename></entry>
336 <entry><filename class="headerfile">csetjmp</filename></entry>
337 <entry><filename class="headerfile">csignal</filename></entry>
338 <entry><filename class="headerfile">cstdarg</filename></entry>
339 <entry><filename class="headerfile">cstdbool</filename></entry>
342 <entry><filename class="headerfile">cstddef</filename></entry>
343 <entry><filename class="headerfile">cstdint</filename></entry>
344 <entry><filename class="headerfile">cstdlib</filename></entry>
345 <entry><filename class="headerfile">cstdio</filename></entry>
346 <entry><filename class="headerfile">cstring</filename></entry>
349 <entry><filename class="headerfile">ctgmath</filename></entry>
350 <entry><filename class="headerfile">ctime</filename></entry>
351 <entry><filename class="headerfile">cuchar</filename></entry>
352 <entry><filename class="headerfile">cwchar</filename></entry>
353 <entry><filename class="headerfile">cwctype</filename></entry>
356 <entry><filename class="headerfile">stdatomic.h</filename></entry>
365 In addition, TR1 includes as:
369 <title>C++ TR 1 Library Headers</title>
370 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
371 <colspec colname='c1'></colspec>
372 <colspec colname='c2'></colspec>
373 <colspec colname='c3'></colspec>
374 <colspec colname='c4'></colspec>
375 <colspec colname='c5'></colspec>
379 <entry><filename class="headerfile">tr1/array</filename></entry>
380 <entry><filename class="headerfile">tr1/complex</filename></entry>
381 <entry><filename class="headerfile">tr1/memory</filename></entry>
382 <entry><filename class="headerfile">tr1/functional</filename></entry>
383 <entry><filename class="headerfile">tr1/random</filename></entry>
386 <entry><filename class="headerfile">tr1/regex</filename></entry>
387 <entry><filename class="headerfile">tr1/tuple</filename></entry>
388 <entry><filename class="headerfile">tr1/type_traits</filename></entry>
389 <entry><filename class="headerfile">tr1/unordered_map</filename></entry>
390 <entry><filename class="headerfile">tr1/unordered_set</filename></entry>
393 <entry><filename class="headerfile">tr1/utility</filename></entry>
404 <title>C++ TR 1 Library Headers for C Library Facilities</title>
405 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
406 <colspec colname='c1'></colspec>
407 <colspec colname='c2'></colspec>
408 <colspec colname='c3'></colspec>
409 <colspec colname='c4'></colspec>
410 <colspec colname='c5'></colspec>
414 <entry><filename class="headerfile">tr1/ccomplex</filename></entry>
415 <entry><filename class="headerfile">tr1/cfenv</filename></entry>
416 <entry><filename class="headerfile">tr1/cfloat</filename></entry>
417 <entry><filename class="headerfile">tr1/cmath</filename></entry>
418 <entry><filename class="headerfile">tr1/cinttypes</filename></entry>
421 <entry><filename class="headerfile">tr1/climits</filename></entry>
422 <entry><filename class="headerfile">tr1/cstdarg</filename></entry>
423 <entry><filename class="headerfile">tr1/cstdbool</filename></entry>
424 <entry><filename class="headerfile">tr1/cstdint</filename></entry>
425 <entry><filename class="headerfile">tr1/cstdio</filename></entry>
428 <entry><filename class="headerfile">tr1/cstdlib</filename></entry>
429 <entry><filename class="headerfile">tr1/ctgmath</filename></entry>
430 <entry><filename class="headerfile">tr1/ctime</filename></entry>
431 <entry><filename class="headerfile">tr1/cwchar</filename></entry>
432 <entry><filename class="headerfile">tr1/cwctype</filename></entry>
440 <para>Decimal floating-point arithmetic is available if the C++
441 compiler supports scalar decimal floating-point types defined via
442 <code>__attribute__((mode(SD|DD|LD)))</code>.
446 <title>C++ TR 24733 Decimal Floating-Point Header</title>
447 <tgroup cols='1' align='left' colsep='1' rowsep='1'>
448 <colspec colname='c1'></colspec>
451 <entry><filename class="headerfile">decimal/decimal</filename></entry>
458 Also included are files for the C++ ABI interface:
462 <title>C++ ABI Headers</title>
463 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
464 <colspec colname='c1'></colspec>
465 <colspec colname='c2'></colspec>
467 <row><entry><filename class="headerfile">cxxabi.h</filename></entry><entry><filename class="headerfile">cxxabi_forced.h</filename></entry></row>
473 And a large variety of extensions.
477 <title>Extension Headers</title>
478 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
479 <colspec colname='c1'></colspec>
480 <colspec colname='c2'></colspec>
481 <colspec colname='c3'></colspec>
482 <colspec colname='c4'></colspec>
483 <colspec colname='c5'></colspec>
487 <entry><filename class="headerfile">ext/algorithm</filename></entry>
488 <entry><filename class="headerfile">ext/atomicity.h</filename></entry>
489 <entry><filename class="headerfile">ext/array_allocator.h</filename></entry>
490 <entry><filename class="headerfile">ext/bitmap_allocator.h</filename></entry>
491 <entry><filename class="headerfile">ext/cast.h</filename></entry>
494 <entry><filename class="headerfile">ext/codecvt_specializations.h</filename></entry>
495 <entry><filename class="headerfile">ext/concurrence.h</filename></entry>
496 <entry><filename class="headerfile">ext/debug_allocator.h</filename></entry>
497 <entry><filename class="headerfile">ext/enc_filebuf.h</filename></entry>
498 <entry><filename class="headerfile">ext/extptr_allocator.h</filename></entry>
501 <entry><filename class="headerfile">ext/functional</filename></entry>
502 <entry><filename class="headerfile">ext/iterator</filename></entry>
503 <entry><filename class="headerfile">ext/malloc_allocator.h</filename></entry>
504 <entry><filename class="headerfile">ext/memory</filename></entry>
505 <entry><filename class="headerfile">ext/mt_allocator.h</filename></entry>
508 <entry><filename class="headerfile">ext/new_allocator.h</filename></entry>
509 <entry><filename class="headerfile">ext/numeric</filename></entry>
510 <entry><filename class="headerfile">ext/numeric_traits.h</filename></entry>
511 <entry><filename class="headerfile">ext/pb_ds/assoc_container.h</filename></entry>
512 <entry><filename class="headerfile">ext/pb_ds/priority_queue.h</filename></entry>
515 <entry><filename class="headerfile">ext/pod_char_traits.h</filename></entry>
516 <entry><filename class="headerfile">ext/pool_allocator.h</filename></entry>
517 <entry><filename class="headerfile">ext/rb_tree</filename></entry>
518 <entry><filename class="headerfile">ext/rope</filename></entry>
519 <entry><filename class="headerfile">ext/slist</filename></entry>
522 <entry><filename class="headerfile">ext/stdio_filebuf.h</filename></entry>
523 <entry><filename class="headerfile">ext/stdio_sync_filebuf.h</filename></entry>
524 <entry><filename class="headerfile">ext/throw_allocator.h</filename></entry>
525 <entry><filename class="headerfile">ext/typelist.h</filename></entry>
526 <entry><filename class="headerfile">ext/type_traits.h</filename></entry>
529 <entry><filename class="headerfile">ext/vstring.h</filename></entry>
539 <title>Extension Debug Headers</title>
540 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
541 <colspec colname='c1'></colspec>
542 <colspec colname='c2'></colspec>
543 <colspec colname='c3'></colspec>
544 <colspec colname='c4'></colspec>
545 <colspec colname='c5'></colspec>
549 <entry><filename class="headerfile">debug/bitset</filename></entry>
550 <entry><filename class="headerfile">debug/deque</filename></entry>
551 <entry><filename class="headerfile">debug/list</filename></entry>
552 <entry><filename class="headerfile">debug/map</filename></entry>
553 <entry><filename class="headerfile">debug/set</filename></entry>
557 <entry><filename class="headerfile">debug/string</filename></entry>
558 <entry><filename class="headerfile">debug/unordered_map</filename></entry>
559 <entry><filename class="headerfile">debug/unordered_set</filename></entry>
560 <entry><filename class="headerfile">debug/vector</filename></entry>
570 <title>Extension Profile Headers</title>
571 <tgroup cols='4' align='left' colsep='1' rowsep='1'>
572 <colspec colname='c1'></colspec>
573 <colspec colname='c2'></colspec>
574 <colspec colname='c3'></colspec>
575 <colspec colname='c4'></colspec>
579 <entry><filename class="headerfile">profile/bitset</filename></entry>
580 <entry><filename class="headerfile">profile/deque</filename></entry>
581 <entry><filename class="headerfile">profile/list</filename></entry>
582 <entry><filename class="headerfile">profile/map</filename></entry>
586 <entry><filename class="headerfile">profile/set</filename></entry>
587 <entry><filename class="headerfile">profile/unordered_map</filename></entry>
588 <entry><filename class="headerfile">profile/unordered_set</filename></entry>
589 <entry><filename class="headerfile">profile/vector</filename></entry>
599 <title>Extension Parallel Headers</title>
600 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
601 <colspec colname='c1'></colspec>
602 <colspec colname='c2'></colspec>
605 <entry><filename class="headerfile">parallel/algorithm</filename></entry>
606 <entry><filename class="headerfile">parallel/numeric</filename></entry>
614 <sect2 id="manual.intro.using.headers.mixing" xreflabel="Mixing Headers">
615 <title>Mixing Headers</title>
617 <para> A few simple rules.
620 <para>First, mixing different dialects of the standard headers is not
621 possible. It's an all-or-nothing affair. Thus, code like
625 #include <array>
626 #include <functional>
629 <para>Implies C++0x mode. To use the entities in <array>, the C++0x
630 compilation mode must be used, which implies the C++0x functionality
631 (and deprecations) in <functional> will be present.
634 <para>Second, the other headers can be included with either dialect of
635 the standard headers, although features and types specific to C++0x
636 are still only enabled when in C++0x compilation mode. So, to use
637 rvalue references with <code>__gnu_cxx::vstring</code>, or to use the
638 debug-mode versions of <code>std::unordered_map</code>, one must use
639 the <code>std=gnu++0x</code> compiler flag. (Or <code>std=c++0x</code>, of course.)
642 <para>A special case of the second rule is the mixing of TR1 and C++0x
643 facilities. It is possible (although not especially prudent) to
644 include both the TR1 version and the C++0x version of header in the
645 same translation unit:
649 #include <tr1/type_traits>
650 #include <type_traits>
653 <para> Several parts of C++0x diverge quite substantially from TR1 predecessors.
657 <sect2 id="manual.intro.using.headers.cheaders" xreflabel="C Headers and">
658 <title>The C Headers and <code>namespace std</code></title>
661 The standard specifies that if one includes the C-style header
662 (<math.h> in this case), the symbols will be available
663 in the global namespace and perhaps in
664 namespace <code>std::</code> (but this is no longer a firm
665 requirement.) On the other hand, including the C++-style
666 header (<cmath>) guarantees that the entities will be
667 found in namespace std and perhaps in the global namespace.
671 Usage of C++-style headers is recommended, as then
672 C-linkage names can be disambiguated by explicit qualification, such
673 as by <code>std::abort</code>. In addition, the C++-style headers can
674 use function overloading to provide a simpler interface to certain
675 families of C-functions. For instance in <cmath>, the
676 function <code>std::sin</code> has overloads for all the builtin
677 floating-point types. This means that <code>std::sin</code> can be
678 used uniformly, instead of a combination
679 of <code>std::sinf</code>, <code>std::sin</code>,
680 and <code>std::sinl</code>.
684 <sect2 id="manual.intro.using.headers.pre" xreflabel="Precompiled Headers">
685 <title>Precompiled Headers</title>
688 <para>There are three base header files that are provided. They can be
689 used to precompile the standard headers and extensions into binary
690 files that may the be used to speed compiles that use these headers.
696 <para>stdc++.h</para>
697 <para>Includes all standard headers. Actual content varies depending on
703 <para>stdtr1c++.h</para>
704 <para>Includes all of <stdc++.h>, and adds all the TR1 headers.
708 <listitem><para>extc++.h</para>
709 <para>Includes all of <stdtr1c++.h>, and adds all the Extension headers.
713 <para>How to construct a .gch file from one of these base header files.</para>
715 <para>First, find the include directory for the compiler. One way to do
721 #include <...> search starts here:
722 /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
728 <para>Then, create a precompiled header file with the same flags that
729 will be used to compile other projects.</para>
732 g++ -Winvalid-pch -x c++-header -g -O2 -o ./stdc++.h.gch /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h
735 <para>The resulting file will be quite large: the current size is around
736 thirty megabytes. </para>
738 <para>How to use the resulting file.</para>
741 g++ -I. -include stdc++.h -H -g -O2 hello.cc
744 <para>Verification that the PCH file is being used is easy:</para>
747 g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
749 . /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
750 . /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string
753 <para>The exclamation point to the left of the <code>stdc++.h.gch</code> listing means that the generated PCH file was used, and thus the </para>
756 <para> Detailed information about creating precompiled header files can be found in the GCC <ulink url="http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html">documentation</ulink>.
763 <sect1 id="manual.intro.using.macros" xreflabel="Macros">
764 <?dbhtml filename="using_macros.html"?>
765 <title>Macros</title>
768 All library macros begin with <code>_GLIBCXX_</code>.
772 Furthermore, all pre-processor macros, switches, and
773 configuration options are gathered in the
774 file <filename class="headerfile">c++config.h</filename>, which
775 is generated during the libstdc++ configuration and build
776 process. This file is then included when needed by files part of
777 the public libstdc++ API, like <ios>. Most of these macros
778 should not be used by consumers of libstdc++, and are reserved
779 for internal implementation use. <emphasis>These macros cannot
780 be redefined</emphasis>.
784 A select handful of macros control libstdc++ extensions and extra
785 features, or provide versioning information for the API. Only
786 those macros listed below are offered for consideration by the
790 <para>Below is the macro which users may check for library version
795 <term><code>__GLIBCXX__</code></term>
797 <para>The current version of
798 libstdc++ in compressed ISO date format, form of an unsigned
799 long. For details on the value of this particular macro for a
800 particular release, please consult this <link linkend="appendix.porting.abi">
807 <para>Below are the macros which users may change with #define/#undef or
808 with -D/-U compiler flags. The default state of the symbol is
811 <para><quote>Configurable</quote> (or <quote>Not configurable</quote>) means
812 that the symbol is initially chosen (or not) based on
813 --enable/--disable options at library build and configure time
814 (documented <link linkend="manual.intro.setup.configure">here</link>), with the
815 various --enable/--disable choices being translated to
819 <para> <acronym>ABI</acronym> means that changing from the default value may
820 mean changing the <acronym>ABI</acronym> of compiled code. In other words, these
821 choices control code which has already been compiled (i.e., in a
822 binary such as libstdc++.a/.so). If you explicitly #define or
823 #undef these macros, the <emphasis>headers</emphasis> may see different code
824 paths, but the <emphasis>libraries</emphasis> which you link against will not.
825 Experimenting with different values with the expectation of
826 consistent linkage requires changing the config headers before
827 building/installing the library.
831 <varlistentry><term><code>_GLIBCXX_DEPRECATED</code></term>
834 Defined by default. Not configurable. ABI-changing. Turning this off
835 removes older ARM-style iostreams code, and other anachronisms
836 from the API. This macro is dependent on the version of the
837 standard being tracked, and as a result may give different results for
838 <code>-std=c++98</code> and <code>-std=c++0x</code>. This may
839 be useful in updating old C++ code which no longer meet the
840 requirements of the language, or for checking current code
841 against new language standards.
843 </listitem></varlistentry>
845 <varlistentry><term><code>_GLIBCXX_FORCE_NEW</code></term>
848 Undefined by default. When defined, memory allocation and
849 allocators controlled by libstdc++ call operator new/delete
850 without caching and pooling. Configurable via
851 <code>--enable-libstdcxx-allocator</code>. ABI-changing.
853 </listitem></varlistentry>
856 <varlistentry><term><code>_GLIBCXX_CONCEPT_CHECKS</code></term>
859 Undefined by default. Configurable via
860 <code>--enable-concept-checks</code>. When defined, performs
861 compile-time checking on certain template instantiations to
862 detect violations of the requirements of the standard. This
863 is described in more detail <link
864 linkend="manual.ext.compile_checks">here</link>.
866 </listitem></varlistentry>
868 <varlistentry><term><code>_GLIBCXX_DEBUG</code></term>
871 Undefined by default. When defined, compiles user code using
872 the <link linkend="manual.ext.debug_mode">debug mode</link>.
874 </listitem></varlistentry>
875 <varlistentry><term><code>_GLIBCXX_DEBUG_PEDANTIC</code></term>
878 Undefined by default. When defined while compiling with
879 the <link linkend="manual.ext.debug_mode">debug mode</link>, makes
880 the debug mode extremely picky by making the use of libstdc++
881 extensions and libstdc++-specific behavior into errors.
883 </listitem></varlistentry>
884 <varlistentry><term><code>_GLIBCXX_PARALLEL</code></term>
886 <para>Undefined by default. When defined, compiles user code
887 using the <link linkend="manual.ext.parallel_mode">parallel
890 </listitem></varlistentry>
892 <varlistentry><term><code>_GLIBCXX_PROFILE</code></term>
894 <para>Undefined by default. When defined, compiles user code
895 using the <link linkend="manual.ext.profile_mode">profile
898 </listitem></varlistentry>
903 <sect1 id="manual.intro.using.namespaces" xreflabel="Namespaces">
904 <?dbhtml filename="using_namespaces.html"?>
905 <title>Namespaces</title>
907 <sect2 id="manual.intro.using.namespaces.all" xreflabel="Available Namespaces">
908 <title>Available Namespaces</title>
912 <para> There are three main namespaces.
916 <listitem><para>std</para>
917 <para>The ISO C++ standards specify that "all library entities are defined
918 within namespace std." This includes namespaces nested
919 within <code>namespace std</code>, such as <code>namespace
923 <listitem><para>abi</para>
924 <para>Specified by the C++ ABI. This ABI specifies a number of type and
925 function APIs supplemental to those required by the ISO C++ Standard,
926 but necessary for interoperability.
930 <listitem><para>__gnu_</para>
931 <para>Indicating one of several GNU extensions. Choices
932 include <code>__gnu_cxx</code>, <code>__gnu_debug</code>, <code>__gnu_parallel</code>,
933 and <code>__gnu_pbds</code>.
937 <para> A complete list of implementation namespaces (including namespace contents) is available in the generated source <ulink url="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaces.html">documentation</ulink>.
943 <sect2 id="manual.intro.using.namespaces.std" xreflabel="namespace std">
944 <title>namespace std</title>
948 One standard requirement is that the library components are defined
949 in <code>namespace std::</code>. Thus, in order to use these types or
950 functions, one must do one of two things:
954 <listitem><para>put a kind of <emphasis>using-declaration</emphasis> in your source
955 (either <code>using namespace std;</code> or i.e. <code>using
956 std::string;</code>) This approach works well for individual source files, but
957 should not be used in a global context, like header files.
958 </para></listitem> <listitem><para>use a <emphasis>fully
959 qualified name</emphasis> for each library symbol
960 (i.e. <code>std::string</code>, <code>std::cout</code>) Always can be
961 used, and usually enhanced, by strategic use of typedefs. (In the
962 cases where the qualified verbiage becomes unwieldy.)
969 <sect2 id="manual.intro.using.namespaces.comp" xreflabel="Using Namespace Composition">
970 <title>Using Namespace Composition</title>
973 Best practice in programming suggests sequestering new data or
974 functionality in a sanely-named, unique namespace whenever
975 possible. This is considered an advantage over dumping everything in
976 the global namespace, as then name look-up can be explicitly enabled or
977 disabled as above, symbols are consistently mangled without repetitive
978 naming prefixes or macros, etc.
981 <para>For instance, consider a project that defines most of its classes in <code>namespace gtk</code>. It is possible to
982 adapt <code>namespace gtk</code> to <code>namespace std</code> by using a C++-feature called
983 <emphasis>namespace composition</emphasis>. This is what happens if
984 a <emphasis>using</emphasis>-declaration is put into a
985 namespace-definition: the imported symbol(s) gets imported into the
986 currently active namespace(s). For example:
992 using std::tr1::array;
994 class Window { ... };
998 In this example, <code>std::string</code> gets imported into
999 <code>namespace gtk</code>. The result is that use of
1000 <code>std::string</code> inside namespace gtk can just use <code>string</code>, without the explicit qualification.
1002 <code>std::string</code> does not get imported into
1003 the global namespace. Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
1004 <code>using</code>-declarations can wrapped in macros that
1005 are set based on autoconf-tests to either "" or i.e. <code>using
1006 std::string;</code> (depending on whether the system has
1007 libstdc++ in <code>std::</code> or not). (ideas from
1008 Llewelly and Karl Nelson)
1015 <sect1 id="manual.intro.using.linkage" xreflabel="Linkage">
1016 <?dbhtml filename="using_dynamic_or_shared.html"?>
1017 <title>Linking</title>
1019 <sect2 id="manual.intro.using.linkage.freestanding" xreflabel="Freestanding">
1020 <title>Almost Nothing</title>
1022 Or as close as it gets: freestanding. This is a minimal
1023 configuration, with only partial support for the standard
1024 library. Assume only the following header files can be used:
1030 <filename class="headerfile">cstdarg</filename>
1036 <filename class="headerfile">cstddef</filename>
1042 <filename class="headerfile">cstdlib</filename>
1048 <filename class="headerfile">exception</filename>
1054 <filename class="headerfile">limits</filename>
1060 <filename class="headerfile">new</filename>
1066 <filename class="headerfile">exception</filename>
1072 <filename class="headerfile">typeinfo</filename>
1078 In addition, throw in
1084 <filename class="headerfile">cxxabi.h</filename>.
1091 C++0x <link linkend="manual.intro.using.flags">dialect</link> add
1097 <filename class="headerfile">initializer_list</filename>
1102 <filename class="headerfile">type_traits</filename>
1107 <para> There exists a library that offers runtime support for
1108 just these headers, and it is called
1109 <filename class="libraryfile">libsupc++.a</filename>. To use it, compile with <command>gcc</command> instead of <command>g++</command>, like so:
1113 <command>gcc foo.cc -lsupc++</command>
1117 No attempt is made to verify that only the minimal subset
1118 identified above is actually used at compile time. Violations
1119 are diagnosed as undefined symbols at link time.
1123 <sect2 id="manual.intro.using.linkage.dynamic" xreflabel="Dynamic and Shared">
1124 <title>Finding Dynamic or Shared Libraries</title>
1127 If the only library built is the static library
1128 (<filename class="libraryfile">libstdc++.a</filename>), or if
1129 specifying static linking, this section is can be skipped. But
1130 if building or using a shared library
1131 (<filename class="libraryfile">libstdc++.so</filename>), then
1132 additional location information will need to be provided.
1138 A quick read of the relevant part of the GCC
1139 manual, <ulink url="http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b">Compiling
1140 C++ Programs</ulink>, specifies linking against a C++
1141 library. More details from the
1142 GCC <ulink url="http://gcc.gnu.org/faq.html#rpath">FAQ</ulink>,
1143 which states <emphasis>GCC does not, by default, specify a
1144 location so that the dynamic linker can find dynamic libraries at
1148 Users will have to provide this information.
1151 Methods vary for different platforms and different styles, and
1152 are printed to the screen during installation. To summarize:
1157 At runtime set <literal>LD_LIBRARY_PATH</literal> in your
1158 environment correctly, so that the shared library for
1159 libstdc++ can be found and loaded. Be certain that you
1160 understand all of the other implications and behavior
1161 of <literal>LD_LIBRARY_PATH</literal> first.
1167 Compile the path to find the library at runtime into the
1168 program. This can be done by passing certain options to
1169 <command>g++</command>, which will in turn pass them on to
1170 the linker. The exact format of the options is dependent on
1171 which linker you use:
1176 GNU ld (default on Linux):<literal>-Wl,--rpath,<filename class="directory">destdir</filename>/lib</literal>
1182 -Wl,-rpath,<filename class="directory">destdir</filename>/lib</literal>
1187 Solaris ld:<literal>-Wl,-R<filename class="directory">destdir</filename>/lib</literal>
1194 Use the <command>ldd</command> utility on the linked executable
1196 which <filename class="libraryfile">libstdc++.so</filename>
1197 library the system will get at runtime.
1200 A <filename class="libraryfile">libstdc++.la</filename> file is
1201 also installed, for use with Libtool. If you use Libtool to
1202 create your executables, these details are taken care of for
1209 <sect1 id="manual.intro.using.concurrency" xreflabel="Concurrency">
1210 <?dbhtml filename="using_concurrency.html"?>
1211 <title>Concurrency</title>
1213 <para>This section discusses issues surrounding the proper compilation
1214 of multithreaded applications which use the Standard C++
1215 library. This information is GCC-specific since the C++
1216 standard does not address matters of multithreaded applications.
1219 <sect2 id="manual.intro.using.concurrency.prereq" xreflabel="Thread Prereq">
1220 <title>Prerequisites</title>
1222 <para>All normal disclaimers aside, multithreaded C++ application are
1223 only supported when libstdc++ and all user code was built with
1224 compilers which report (via <code> gcc/g++ -v </code>) the same thread
1225 model and that model is not <emphasis>single</emphasis>. As long as your
1226 final application is actually single-threaded, then it should be
1227 safe to mix user code built with a thread model of
1228 <emphasis>single</emphasis> with a libstdc++ and other C++ libraries built
1229 with another thread model useful on the platform. Other mixes
1230 may or may not work but are not considered supported. (Thus, if
1231 you distribute a shared C++ library in binary form only, it may
1232 be best to compile it with a GCC configured with
1233 --enable-threads for maximal interchangeability and usefulness
1234 with a user population that may have built GCC with either
1235 --enable-threads or --disable-threads.)
1237 <para>When you link a multithreaded application, you will probably
1238 need to add a library or flag to g++. This is a very
1239 non-standardized area of GCC across ports. Some ports support a
1240 special flag (the spelling isn't even standardized yet) to add
1241 all required macros to a compilation (if any such flags are
1242 required then you must provide the flag for all compilations not
1243 just linking) and link-library additions and/or replacements at
1244 link time. The documentation is weak. Here is a quick summary
1245 to display how ad hoc this is: On Solaris, both -pthreads and
1246 -threads (with subtly different meanings) are honored. On OSF,
1247 -pthread and -threads (with subtly different meanings) are
1248 honored. On Linux/i386, -pthread is honored. On FreeBSD,
1249 -pthread is honored. Some other ports use other switches.
1250 AFAIK, none of this is properly documented anywhere other than
1251 in ``gcc -dumpspecs'' (look at lib and cpp entries).
1256 <sect2 id="manual.intro.using.concurrency.thread_safety" xreflabel="Thread Safety">
1257 <title>Thread Safety</title>
1261 We currently use the <ulink url="http://www.sgi.com/tech/stl/thread_safety.html">SGI STL</ulink> definition of thread safety.
1265 <para>The library strives to be thread-safe when all of the following
1270 <para>The system's libc is itself thread-safe,
1275 The compiler in use reports a thread model other than
1276 'single'. This can be tested via output from <code>gcc
1277 -v</code>. Multi-thread capable versions of gcc output
1278 something like this:
1282 Using built-in specs.
1285 gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
1288 <para>Look for "Thread model" lines that aren't equal to "single."</para>
1292 Requisite command-line flags are used for atomic operations
1293 and threading. Examples of this include <code>-pthread</code>
1294 and <code>-march=native</code>, although specifics vary
1295 depending on the host environment. See <ulink
1296 url="http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html">Machine
1297 Dependent Options</ulink>.
1302 An implementation of atomicity.h functions
1303 exists for the architecture in question. See the internals documentation for more <link linkend="internals.thread_safety">details</link>.
1308 <para>The user-code must guard against concurrent method calls which may
1309 access any particular library object's state. Typically, the
1310 application programmer may infer what object locks must be held
1311 based on the objects referenced in a method call. Without getting
1312 into great detail, here is an example which requires user-level
1316 library_class_a shared_object_a;
1319 library_class_b *object_b = new library_class_b;
1320 shared_object_a.add_b (object_b); // must hold lock for shared_object_a
1321 shared_object_a.mutate (); // must hold lock for shared_object_a
1324 // Multiple copies of thread_main() are started in independent threads.</programlisting>
1325 <para>Under the assumption that object_a and object_b are never exposed to
1326 another thread, here is an example that should not require any
1331 library_class_a object_a;
1332 library_class_b *object_b = new library_class_b;
1333 object_a.add_b (object_b);
1336 <para>All library objects are safe to use in a multithreaded program as
1337 long as each thread carefully locks out access by any other
1338 thread while it uses any object visible to another thread, i.e.,
1339 treat library objects like any other shared resource. In general,
1340 this requirement includes both read and write access to objects;
1341 unless otherwise documented as safe, do not assume that two threads
1342 may access a shared standard library object at the same time.
1347 <sect2 id="manual.intro.using.concurrency.atomics" xreflabel="Atomics">
1348 <title>Atomics</title>
1353 <sect2 id="manual.intro.using.concurrency.io" xreflabel="IO">
1355 <para>This gets a bit tricky. Please read carefully, and bear with me.
1358 <sect3 id="concurrency.io.structure" xreflabel="Structure">
1359 <title>Structure</title>
1361 type called <code>__basic_file</code> provides our abstraction layer
1362 for the <code>std::filebuf</code> classes. Nearly all decisions dealing
1363 with actual input and output must be made in <code>__basic_file</code>.
1365 <para>A generic locking mechanism is somewhat in place at the filebuf layer,
1366 but is not used in the current code. Providing locking at any higher
1367 level is akin to providing locking within containers, and is not done
1368 for the same reasons (see the links above).
1372 <sect3 id="concurrency.io.defaults" xreflabel="Defaults">
1373 <title>Defaults</title>
1374 <para>The __basic_file type is simply a collection of small wrappers around
1375 the C stdio layer (again, see the link under Structure). We do no
1376 locking ourselves, but simply pass through to calls to <code>fopen</code>,
1377 <code>fwrite</code>, and so forth.
1379 <para>So, for 3.0, the question of "is multithreading safe for I/O"
1380 must be answered with, "is your platform's C library threadsafe
1381 for I/O?" Some are by default, some are not; many offer multiple
1382 implementations of the C library with varying tradeoffs of threadsafety
1383 and efficiency. You, the programmer, are always required to take care
1384 with multiple threads.
1386 <para>(As an example, the POSIX standard requires that C stdio FILE*
1387 operations are atomic. POSIX-conforming C libraries (e.g, on Solaris
1388 and GNU/Linux) have an internal mutex to serialize operations on
1389 FILE*s. However, you still need to not do stupid things like calling
1390 <code>fclose(fs)</code> in one thread followed by an access of
1391 <code>fs</code> in another.)
1393 <para>So, if your platform's C library is threadsafe, then your
1394 <code>fstream</code> I/O operations will be threadsafe at the lowest
1395 level. For higher-level operations, such as manipulating the data
1396 contained in the stream formatting classes (e.g., setting up callbacks
1397 inside an <code>std::ofstream</code>), you need to guard such accesses
1398 like any other critical shared resource.
1402 <sect3 id="concurrency.io.future" xreflabel="Future">
1403 <title>Future</title>
1405 second choice may be available for I/O implementations: libio. This is
1406 disabled by default, and in fact will not currently work due to other
1407 issues. It will be revisited, however.
1409 <para>The libio code is a subset of the guts of the GNU libc (glibc) I/O
1410 implementation. When libio is in use, the <code>__basic_file</code>
1411 type is basically derived from FILE. (The real situation is more
1412 complex than that... it's derived from an internal type used to
1413 implement FILE. See libio/libioP.h to see scary things done with
1414 vtbls.) The result is that there is no "layer" of C stdio
1415 to go through; the filebuf makes calls directly into the same
1416 functions used to implement <code>fread</code>, <code>fwrite</code>,
1417 and so forth, using internal data structures. (And when I say
1418 "makes calls directly," I mean the function is literally
1419 replaced by a jump into an internal function. Fast but frightening.
1422 <para>Also, the libio internal locks are used. This requires pulling in
1423 large chunks of glibc, such as a pthreads implementation, and is one
1424 of the issues preventing widespread use of libio as the libstdc++
1425 cstdio implementation.
1427 <para>But we plan to make this work, at least as an option if not a future
1428 default. Platforms running a copy of glibc with a recent-enough
1429 version will see calls from libstdc++ directly into the glibc already
1430 installed. For other platforms, a copy of the libio subsection will
1431 be built and included in libstdc++.
1435 <sect3 id="concurrency.io.alt" xreflabel="Alt">
1436 <title>Alternatives</title>
1437 <para>Don't forget that other cstdio implementations are possible. You could
1438 easily write one to perform your own forms of locking, to solve your
1439 "interesting" problems.
1445 <sect2 id="manual.intro.using.concurrency.containers" xreflabel="Containers">
1446 <title>Containers</title>
1448 <para>This section discusses issues surrounding the design of
1449 multithreaded applications which use Standard C++ containers.
1450 All information in this section is current as of the gcc 3.0
1451 release and all later point releases. Although earlier gcc
1452 releases had a different approach to threading configuration and
1453 proper compilation, the basic code design rules presented here
1454 were similar. For information on all other aspects of
1455 multithreading as it relates to libstdc++, including details on
1456 the proper compilation of threaded code (and compatibility between
1457 threaded and non-threaded code), see Chapter 17.
1459 <para>Two excellent pages to read when working with the Standard C++
1460 containers and threads are
1461 <ulink url="http://www.sgi.com/tech/stl/thread_safety.html">SGI's
1462 http://www.sgi.com/tech/stl/thread_safety.html</ulink> and
1463 <ulink url="http://www.sgi.com/tech/stl/Allocators.html">SGI's
1464 http://www.sgi.com/tech/stl/Allocators.html</ulink>.
1466 <para><emphasis>However, please ignore all discussions about the user-level
1467 configuration of the lock implementation inside the STL
1468 container-memory allocator on those pages. For the sake of this
1469 discussion, libstdc++ configures the SGI STL implementation,
1470 not you. This is quite different from how gcc pre-3.0 worked.
1471 In particular, past advice was for people using g++ to
1472 explicitly define _PTHREADS or other macros or port-specific
1473 compilation options on the command line to get a thread-safe
1474 STL. This is no longer required for any port and should no
1475 longer be done unless you really know what you are doing and
1476 assume all responsibility.</emphasis>
1478 <para>Since the container implementation of libstdc++ uses the SGI
1479 code, we use the same definition of thread safety as SGI when
1480 discussing design. A key point that beginners may miss is the
1481 fourth major paragraph of the first page mentioned above
1482 (<emphasis>For most clients...</emphasis>), which points out that
1483 locking must nearly always be done outside the container, by
1484 client code (that'd be you, not us). There is a notable
1485 exceptions to this rule. Allocators called while a container or
1486 element is constructed uses an internal lock obtained and
1487 released solely within libstdc++ code (in fact, this is the
1488 reason STL requires any knowledge of the thread configuration).
1490 <para>For implementing a container which does its own locking, it is
1491 trivial to provide a wrapper class which obtains the lock (as
1492 SGI suggests), performs the container operation, and then
1493 releases the lock. This could be templatized <emphasis>to a certain
1494 extent</emphasis>, on the underlying container and/or a locking
1495 mechanism. Trying to provide a catch-all general template
1496 solution would probably be more trouble than it's worth.
1498 <para>The library implementation may be configured to use the
1499 high-speed caching memory allocator, which complicates thread
1500 safety issues. For all details about how to globally override
1501 this at application run-time
1502 see <link linkend="manual.intro.using.macros">here</link>. Also
1504 on <link linkend="std.util.memory.allocator">allocator</link>
1505 options and capabilities.
1511 <!-- Section 0x : Exception policies, expectations, topics -->
1512 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
1513 parse="xml" href="using_exceptions.xml">
1516 <!-- Section 0x : Debug -->
1517 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
1518 parse="xml" href="debug.xml">