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"?>
11 <sect1 id="manual.intro.using.lib" xreflabel="Lib">
12 <title>Linking Library Binary Files</title>
15 If you only built a static library (libstdc++.a), or if you
16 specified static linking, you don't have to worry about this.
17 But if you built a shared library (libstdc++.so) and linked
18 against it, then you will need to find that library when you run
22 Methods vary for different platforms and different styles, but
23 the usual ones are printed to the screen during installation.
29 At runtime set LD_LIBRARY_PATH in your environment
30 correctly, so that the shared library for libstdc++ can be
31 found and loaded. Be certain that you understand all of the
32 other implications and behavior of LD_LIBRARY_PATH first
33 (few people do, and they get into trouble).
38 Compile the path to find the library at runtime into the
39 program. This can be done by passing certain options to
40 g++, which will in turn pass them on to the linker. The
41 exact format of the options is dependent on which linker you
47 GNU ld (default on Linux):<literal>-Wl,--rpath,<filename class="directory">destdir</filename>/lib</literal>
53 -Wl,-rpath,<filename class="directory">destdir</filename>/lib</literal>
58 Solaris ld:<literal>-Wl,-R<filename class="directory">destdir</filename>/lib</literal>
70 Use the <command>ldd</command> utility to show which library the
71 system thinks it will get at runtime.
74 A libstdc++.la file is also installed, for use with Libtool. If
75 you use Libtool to create your executables, these details are
76 taken care of for you.
80 <sect1 id="manual.intro.using.headers" xreflabel="Headers">
81 <?dbhtml filename="using_headers.html"?>
82 <title>Headers</title>
84 <sect2 id="manual.intro.using.headers.all" xreflabel="Header Files">
85 <title>Header Files</title>
88 The C++ standard specifies the entire set of header files that
89 must be available to all hosted implementations. Actually, the
90 word "files" is a misnomer, since the contents of the
91 headers don't necessarily have to be in any kind of external
92 file. The only rule is that when one <code>#include</code>'s a
93 header, the contents of that header become available, no matter
98 That said, in practice files are used.
102 There are two main types of include files: header files related
103 to a specific version of the ISO C++ standard (called Standard
104 Headers), and all others (TR1, C++ ABI, and Extensions).
108 Two dialects of standard headers are supported, corresponding to
109 the 1998 standard as updated for 2003, and the draft of the
110 upcoming 200x standard.
114 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>.
118 <title>C++ 1998 Library Headers</title>
119 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
120 <colspec colname='c1'></colspec>
121 <colspec colname='c2'></colspec>
122 <colspec colname='c3'></colspec>
123 <colspec colname='c4'></colspec>
126 <entry><filename class="headerfile">algorithm</filename></entry>
127 <entry><filename class="headerfile">bitset</filename></entry>
128 <entry><filename class="headerfile">complex</filename></entry>
129 <entry><filename class="headerfile">deque</filename></entry>
130 <entry><filename class="headerfile">exception</filename></entry>
133 <entry><filename class="headerfile">fstream</filename></entry>
134 <entry><filename class="headerfile">functional</filename></entry>
135 <entry><filename class="headerfile">iomanip</filename></entry>
136 <entry><filename class="headerfile">ios</filename></entry>
137 <entry><filename class="headerfile">iosfwd</filename></entry>
140 <entry><filename class="headerfile">iostream</filename></entry>
141 <entry><filename class="headerfile">istream</filename></entry>
142 <entry><filename class="headerfile">iterator</filename></entry>
143 <entry><filename class="headerfile">limits</filename></entry>
144 <entry><filename class="headerfile">list</filename></entry>
147 <entry><filename class="headerfile">locale</filename></entry>
148 <entry><filename class="headerfile">map</filename></entry>
149 <entry><filename class="headerfile">memory</filename></entry>
150 <entry><filename class="headerfile">new</filename></entry>
151 <entry><filename class="headerfile">numeric</filename></entry>
154 <entry><filename class="headerfile">ostream</filename></entry>
155 <entry><filename class="headerfile">queue</filename></entry>
156 <entry><filename class="headerfile">set</filename></entry>
157 <entry><filename class="headerfile">sstream</filename></entry>
158 <entry><filename class="headerfile">stack</filename></entry>
161 <entry><filename class="headerfile">stdexcept</filename></entry>
162 <entry><filename class="headerfile">streambuf</filename></entry>
163 <entry><filename class="headerfile">string</filename></entry>
164 <entry><filename class="headerfile">utility</filename></entry>
165 <entry><filename class="headerfile">typeinfo</filename></entry>
168 <entry><filename class="headerfile">valarray</filename></entry>
169 <entry><filename class="headerfile">vector</filename></entry>
177 <title>C++ 1998 Library Headers for C Library Facilities</title>
178 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
179 <colspec colname='c1'></colspec>
180 <colspec colname='c2'></colspec>
181 <colspec colname='c3'></colspec>
182 <colspec colname='c4'></colspec>
185 <entry><filename class="headerfile">cassert</filename></entry>
186 <entry><filename class="headerfile">cerrno</filename></entry>
187 <entry><filename class="headerfile">cctype</filename></entry>
188 <entry><filename class="headerfile">cfloat</filename></entry>
189 <entry><filename class="headerfile">ciso646</filename></entry>
192 <entry><filename class="headerfile">climits</filename></entry>
193 <entry><filename class="headerfile">clocale</filename></entry>
194 <entry><filename class="headerfile">cmath</filename></entry>
195 <entry><filename class="headerfile">csetjmp</filename></entry>
196 <entry><filename class="headerfile">csignal</filename></entry>
199 <entry><filename class="headerfile">cstdarg</filename></entry>
200 <entry><filename class="headerfile">cstddef</filename></entry>
201 <entry><filename class="headerfile">cstdio</filename></entry>
202 <entry><filename class="headerfile">cstdlib</filename></entry>
203 <entry><filename class="headerfile">cstring</filename></entry>
206 <entry><filename class="headerfile">ctime</filename></entry>
207 <entry><filename class="headerfile">cwchar</filename></entry>
208 <entry><filename class="headerfile">cwctype</filename></entry>
215 C++0x include files. These are only available in C++0x compilation
216 mode, i.e. <literal>-std=c++0x</literal> or <literal>-std=gnu++0x</literal>.
221 <title>C++ 200x Library Headers</title>
222 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
223 <colspec colname='c1'></colspec>
224 <colspec colname='c2'></colspec>
225 <colspec colname='c3'></colspec>
226 <colspec colname='c4'></colspec>
230 <entry><filename class="headerfile">algorithm</filename></entry>
231 <entry><filename class="headerfile">array</filename></entry>
232 <entry><filename class="headerfile">bitset</filename></entry>
233 <entry><filename class="headerfile">chrono</filename></entry>
234 <entry><filename class="headerfile">complex</filename></entry>
237 <entry><filename class="headerfile">condition_variable</filename></entry>
238 <entry><filename class="headerfile">deque</filename></entry>
239 <entry><filename class="headerfile">exception</filename></entry>
240 <entry><filename class="headerfile">forward_list</filename></entry>
241 <entry><filename class="headerfile">fstream</filename></entry>
244 <entry><filename class="headerfile">functional</filename></entry>
245 <entry><filename class="headerfile">initalizer_list</filename></entry>
246 <entry><filename class="headerfile">iomanip</filename></entry>
247 <entry><filename class="headerfile">ios</filename></entry>
248 <entry><filename class="headerfile">iosfwd</filename></entry>
251 <entry><filename class="headerfile">iostream</filename></entry>
252 <entry><filename class="headerfile">istream</filename></entry>
253 <entry><filename class="headerfile">iterator</filename></entry>
254 <entry><filename class="headerfile">limits</filename></entry>
255 <entry><filename class="headerfile">list</filename></entry>
258 <entry><filename class="headerfile">locale</filename></entry>
259 <entry><filename class="headerfile">map</filename></entry>
260 <entry><filename class="headerfile">memory</filename></entry>
261 <entry><filename class="headerfile">mutex</filename></entry>
262 <entry><filename class="headerfile">new</filename></entry>
265 <entry><filename class="headerfile">numeric</filename></entry>
266 <entry><filename class="headerfile">ostream</filename></entry>
267 <entry><filename class="headerfile">queue</filename></entry>
268 <entry><filename class="headerfile">random</filename></entry>
269 <entry><filename class="headerfile">ratio</filename></entry>
272 <entry><filename class="headerfile">regex</filename></entry>
273 <entry><filename class="headerfile">set</filename></entry>
274 <entry><filename class="headerfile">sstream</filename></entry>
275 <entry><filename class="headerfile">stack</filename></entry>
276 <entry><filename class="headerfile">stdexcept</filename></entry>
279 <entry><filename class="headerfile">streambuf</filename></entry>
280 <entry><filename class="headerfile">string</filename></entry>
281 <entry><filename class="headerfile">system_error</filename></entry>
282 <entry><filename class="headerfile">thread</filename></entry>
283 <entry><filename class="headerfile">tuple</filename></entry>
286 <entry><filename class="headerfile">type_traits</filename></entry>
287 <entry><filename class="headerfile">typeinfo</filename></entry>
288 <entry><filename class="headerfile">unordered_map</filename></entry>
289 <entry><filename class="headerfile">unordered_set</filename></entry>
290 <entry><filename class="headerfile">utility</filename></entry>
293 <entry><filename class="headerfile">valarray</filename></entry>
294 <entry><filename class="headerfile">vector</filename></entry>
304 <title>C++ 200x Library Headers for C Library Facilities</title>
305 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
306 <colspec colname='c1'></colspec>
307 <colspec colname='c2'></colspec>
308 <colspec colname='c3'></colspec>
309 <colspec colname='c4'></colspec>
310 <colspec colname='c5'></colspec>
313 <entry><filename class="headerfile">cassert</filename></entry>
314 <entry><filename class="headerfile">ccomplex</filename></entry>
315 <entry><filename class="headerfile">cctype</filename></entry>
316 <entry><filename class="headerfile">cerrno</filename></entry>
317 <entry><filename class="headerfile">cfenv</filename></entry>
320 <entry><filename class="headerfile">cfloat</filename></entry>
321 <entry><filename class="headerfile">cinttypes</filename></entry>
322 <entry><filename class="headerfile">ciso646</filename></entry>
323 <entry><filename class="headerfile">climits</filename></entry>
324 <entry><filename class="headerfile">clocale</filename></entry>
327 <entry><filename class="headerfile">cmath</filename></entry>
328 <entry><filename class="headerfile">csetjmp</filename></entry>
329 <entry><filename class="headerfile">csignal</filename></entry>
330 <entry><filename class="headerfile">cstdarg</filename></entry>
331 <entry><filename class="headerfile">cstdatomic</filename></entry>
334 <entry><filename class="headerfile">cstdbool</filename></entry>
335 <entry><filename class="headerfile">cstddef</filename></entry>
336 <entry><filename class="headerfile">cstdint</filename></entry>
337 <entry><filename class="headerfile">cstdlib</filename></entry>
338 <entry><filename class="headerfile">cstdio</filename></entry>
341 <entry><filename class="headerfile">cstring</filename></entry>
342 <entry><filename class="headerfile">ctgmath</filename></entry>
343 <entry><filename class="headerfile">ctime</filename></entry>
344 <entry><filename class="headerfile">cuchar</filename></entry>
345 <entry><filename class="headerfile">cwchar</filename></entry>
348 <entry><filename class="headerfile">cwctype</filename></entry>
349 <entry><filename class="headerfile">stdatomic.h</filename></entry>
358 In addition, TR1 includes as:
362 <title>C++ TR1 Library Headers</title>
363 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
364 <colspec colname='c1'></colspec>
365 <colspec colname='c2'></colspec>
366 <colspec colname='c3'></colspec>
367 <colspec colname='c4'></colspec>
368 <colspec colname='c5'></colspec>
372 <entry><filename class="headerfile">tr1/array</filename></entry>
373 <entry><filename class="headerfile">tr1/complex</filename></entry>
374 <entry><filename class="headerfile">tr1/memory</filename></entry>
375 <entry><filename class="headerfile">tr1/functional</filename></entry>
376 <entry><filename class="headerfile">tr1/random</filename></entry>
379 <entry><filename class="headerfile">tr1/regex</filename></entry>
380 <entry><filename class="headerfile">tr1/tuple</filename></entry>
381 <entry><filename class="headerfile">tr1/type_traits</filename></entry>
382 <entry><filename class="headerfile">tr1/unordered_map</filename></entry>
383 <entry><filename class="headerfile">tr1/unordered_set</filename></entry>
386 <entry><filename class="headerfile">tr1/utility</filename></entry>
397 <title>C++ TR1 Library Headers for C Library Facilities</title>
398 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
399 <colspec colname='c1'></colspec>
400 <colspec colname='c2'></colspec>
401 <colspec colname='c3'></colspec>
402 <colspec colname='c4'></colspec>
403 <colspec colname='c5'></colspec>
407 <entry><filename class="headerfile">tr1/ccomplex</filename></entry>
408 <entry><filename class="headerfile">tr1/cfenv</filename></entry>
409 <entry><filename class="headerfile">tr1/cfloat</filename></entry>
410 <entry><filename class="headerfile">tr1/cmath</filename></entry>
411 <entry><filename class="headerfile">tr1/cinttypes</filename></entry>
414 <entry><filename class="headerfile">tr1/climits</filename></entry>
415 <entry><filename class="headerfile">tr1/cstdarg</filename></entry>
416 <entry><filename class="headerfile">tr1/cstdbool</filename></entry>
417 <entry><filename class="headerfile">tr1/cstdint</filename></entry>
418 <entry><filename class="headerfile">tr1/cstdio</filename></entry>
421 <entry><filename class="headerfile">tr1/cstdlib</filename></entry>
422 <entry><filename class="headerfile">tr1/ctgmath</filename></entry>
423 <entry><filename class="headerfile">tr1/ctime</filename></entry>
424 <entry><filename class="headerfile">tr1/cwchar</filename></entry>
425 <entry><filename class="headerfile">tr1/cwctype</filename></entry>
433 Also included are files for the C++ ABI interface:
437 <title>C++ ABI Headers</title>
438 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
439 <colspec colname='c1'></colspec>
440 <colspec colname='c2'></colspec>
442 <row><entry><filename class="headerfile">cxxabi.h</filename></entry><entry><filename class="headerfile">cxxabi_forced.h</filename></entry></row>
448 And a large variety of extensions.
452 <title>Extension Headers</title>
453 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
454 <colspec colname='c1'></colspec>
455 <colspec colname='c2'></colspec>
456 <colspec colname='c3'></colspec>
457 <colspec colname='c4'></colspec>
458 <colspec colname='c5'></colspec>
462 <entry><filename class="headerfile">ext/algorithm</filename></entry>
463 <entry><filename class="headerfile">ext/atomicity.h</filename></entry>
464 <entry><filename class="headerfile">ext/array_allocator.h</filename></entry>
465 <entry><filename class="headerfile">ext/bitmap_allocator.h</filename></entry>
466 <entry><filename class="headerfile">ext/cast.h</filename></entry>
469 <entry><filename class="headerfile">ext/codecvt_specializations.h</filename></entry>
470 <entry><filename class="headerfile">ext/concurrence.h</filename></entry>
471 <entry><filename class="headerfile">ext/debug_allocator.h</filename></entry>
472 <entry><filename class="headerfile">ext/enc_filebuf.h</filename></entry>
473 <entry><filename class="headerfile">ext/extptr_allocator.h</filename></entry>
476 <entry><filename class="headerfile">ext/functional</filename></entry>
477 <entry><filename class="headerfile">ext/iterator</filename></entry>
478 <entry><filename class="headerfile">ext/malloc_allocator.h</filename></entry>
479 <entry><filename class="headerfile">ext/memory</filename></entry>
480 <entry><filename class="headerfile">ext/mt_allocator.h</filename></entry>
483 <entry><filename class="headerfile">ext/new_allocator.h</filename></entry>
484 <entry><filename class="headerfile">ext/numeric</filename></entry>
485 <entry><filename class="headerfile">ext/numeric_traits.h</filename></entry>
486 <entry><filename class="headerfile">ext/pb_ds/assoc_container.h</filename></entry>
487 <entry><filename class="headerfile">ext/pb_ds/priority_queue.h</filename></entry>
490 <entry><filename class="headerfile">ext/pod_char_traits.h</filename></entry>
491 <entry><filename class="headerfile">ext/pool_allocator.h</filename></entry>
492 <entry><filename class="headerfile">ext/rb_tree</filename></entry>
493 <entry><filename class="headerfile">ext/rope</filename></entry>
494 <entry><filename class="headerfile">ext/slist</filename></entry>
497 <entry><filename class="headerfile">ext/stdio_filebuf.h</filename></entry>
498 <entry><filename class="headerfile">ext/stdio_sync_filebuf.h</filename></entry>
499 <entry><filename class="headerfile">ext/throw_allocator.h</filename></entry>
500 <entry><filename class="headerfile">ext/typelist.h</filename></entry>
501 <entry><filename class="headerfile">ext/type_traits.h</filename></entry>
504 <entry><filename class="headerfile">ext/vstring.h</filename></entry>
514 <title>Extension Debug Headers</title>
515 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
516 <colspec colname='c1'></colspec>
517 <colspec colname='c2'></colspec>
518 <colspec colname='c3'></colspec>
519 <colspec colname='c4'></colspec>
520 <colspec colname='c5'></colspec>
524 <entry><filename class="headerfile">debug/bitset</filename></entry>
525 <entry><filename class="headerfile">debug/deque</filename></entry>
526 <entry><filename class="headerfile">debug/list</filename></entry>
527 <entry><filename class="headerfile">debug/map</filename></entry>
528 <entry><filename class="headerfile">debug/set</filename></entry>
532 <entry><filename class="headerfile">debug/string</filename></entry>
533 <entry><filename class="headerfile">debug/unordered_map</filename></entry>
534 <entry><filename class="headerfile">debug/unordered_set</filename></entry>
535 <entry><filename class="headerfile">debug/vector</filename></entry>
545 <title>Extension Parallel Headers</title>
546 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
547 <colspec colname='c1'></colspec>
548 <colspec colname='c2'></colspec>
551 <entry><filename class="headerfile">parallel/algorithm</filename></entry>
552 <entry><filename class="headerfile">parallel/numeric</filename></entry>
560 <sect2 id="manual.intro.using.headers.mixing" xreflabel="Mixing Headers">
561 <title>Mixing Headers</title>
563 <para> A few simple rules.
566 <para>First, mixing different dialects of the standard headers is not
567 possible. It's an all-or-nothing affair. Thus, code like
571 #include <array>
572 #include <functional>
575 <para>Implies C++0x mode. To use the entities in <array>, the C++0x
576 compilation mode must be used, which implies the C++0x functionality
577 (and deprecations) in <functional> will be present.
580 <para>Second, the other headers can be included with either dialect of
581 the standard headers, although features and types specific to C++0x
582 are still only enabled when in C++0x compilation mode. So, to use
583 rvalue references with <code>__gnu_cxx::vstring</code>, or to use the
584 debug-mode versions of <code>std::unordered_map</code>, one must use
585 the <code>std=gnu++0x</code> compiler flag. (Or <code>std=c++0x</code>, of course.)
588 <para>A special case of the second rule is the mixing of TR1 and C++0x
589 facilities. It is possible (although not especially prudent) to
590 include both the TR1 version and the C++0x version of header in the
591 same translation unit:
595 #include <tr1/type_traits>
596 #include <type_traits>
599 <para> Several parts of C++0x diverge quite substantially from TR1 predecessors.
603 <sect2 id="manual.intro.using.headers.cheaders" xreflabel="C Headers and">
604 <title>The C Headers and <code>namespace std</code></title>
607 The standard specifies that if one includes the C-style header
608 (<math.h> in this case), the symbols will be available
609 in the global namespace and perhaps in
610 namespace <code>std::</code> (but this is no longer a firm
611 requirement.) One the other hand, including the C++-style
612 header (<cmath>) guarantees that the entities will be
613 found in namespace std and perhaps in the global namespace.
617 Usage of C++-style headers is recommended, as then
618 C-linkage names can be disambiguated by explicit qualification, such
619 as by <code>std::abort</code>. In addition, the C++-style headers can
620 use function overloading to provide a simpler interface to certain
621 families of C-functions. For instance in <cmath>, the
622 function <code>std::sin</code> has overloads for all the builtin
623 floating-point types. This means that <code>std::sin</code> can be
624 used uniformly, instead of a combination
625 of <code>std::sinf</code>, <code>std::sin</code>,
626 and <code>std::sinl</code>.
630 <sect2 id="manual.intro.using.headers.pre" xreflabel="Precompiled Headers">
631 <title>Precompiled Headers</title>
634 <para>There are three base header files that are provided. They can be
635 used to precompile the standard headers and extensions into binary
636 files that may the be used to speed compiles that use these headers.
642 <para>stdc++.h</para>
643 <para>Includes all standard headers. Actual content varies depending on
649 <para>stdtr1c++.h</para>
650 <para>Includes all of <stdc++.h>, and adds all the TR1 headers.
654 <listitem><para>extc++.h</para>
655 <para>Includes all of <stdtr1c++.h>, and adds all the Extension headers.
659 <para>How to construct a .gch file from one of these base header files.</para>
661 <para>First, find the include directory for the compiler. One way to do
667 #include <...> search starts here:
668 /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
674 <para>Then, create a precompiled header file with the same flags that
675 will be used to compile other projects.</para>
678 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
681 <para>The resulting file will be quite large: the current size is around
682 thirty megabytes. </para>
684 <para>How to use the resulting file.</para>
687 g++ -I. -include stdc++.h -H -g -O2 hello.cc
690 <para>Verification that the PCH file is being used is easy:</para>
693 g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
695 . /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
696 . /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string
699 <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>
702 <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>.
709 <sect1 id="manual.intro.using.namespaces" xreflabel="Namespaces">
710 <?dbhtml filename="using_namespaces.html"?>
711 <title>Namespaces</title>
713 <sect2 id="manual.intro.using.namespaces.all" xreflabel="Available Namespaces">
714 <title>Available Namespaces</title>
718 <para> There are three main namespaces.
722 <listitem><para>std</para>
723 <para>The ISO C++ standards specify that "all library entities are defined
724 within namespace std." This includes namespaces nested
725 within <code>namespace std</code>, such as <code>namespace
729 <listitem><para>abi</para>
730 <para>Specified by the C++ ABI. This ABI specifies a number of type and
731 function APIs supplemental to those required by the ISO C++ Standard,
732 but necessary for interoperability.
736 <listitem><para>__gnu_</para>
737 <para>Indicating one of several GNU extensions. Choices
738 include <code>__gnu_cxx</code>, <code>__gnu_debug</code>, <code>__gnu_parallel</code>,
739 and <code>__gnu_pbds</code>.
743 <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>.
749 <sect2 id="manual.intro.using.namespaces.std" xreflabel="namespace std">
750 <title>namespace std</title>
754 One standard requirement is that the library components are defined
755 in <code>namespace std::</code>. Thus, in order to use these types or
756 functions, one must do one of two things:
760 <listitem><para>put a kind of <emphasis>using-declaration</emphasis> in your source
761 (either <code>using namespace std;</code> or i.e. <code>using
762 std::string;</code>) This approach works well for individual source files, but
763 should not be used in a global context, like header files.
764 </para></listitem> <listitem><para>use a <emphasis>fully
765 qualified name</emphasis>for each library symbol
766 (i.e. <code>std::string</code>, <code>std::cout</code>) Always can be
767 used, and usually enhanced, by strategic use of typedefs. (In the
768 cases where the qualified verbiage becomes unwieldy.)
775 <sect2 id="manual.intro.using.namespaces.comp" xreflabel="Using Namespace Composition">
776 <title>Using Namespace Composition</title>
779 Best practice in programming suggests sequestering new data or
780 functionality in a sanely-named, unique namespace whenever
781 possible. This is considered an advantage over dumping everything in
782 the global namespace, as then name look-up can be explicitly enabled or
783 disabled as above, symbols are consistently mangled without repetitive
784 naming prefixes or macros, etc.
787 <para>For instance, consider a project that defines most of its classes in <code>namespace gtk</code>. It is possible to
788 adapt <code>namespace gtk</code> to <code>namespace std</code> by using a C++-feature called
789 <emphasis>namespace composition</emphasis>. This is what happens if
790 a <emphasis>using</emphasis>-declaration is put into a
791 namespace-definition: the imported symbol(s) gets imported into the
792 currently active namespace(s). For example:
798 using std::tr1::array;
800 class Window { ... };
804 In this example, <code>std::string</code> gets imported into
805 <code>namespace gtk</code>. The result is that use of
806 <code>std::string</code> inside namespace gtk can just use <code>string</code>, without the explicit qualification.
808 <code>std::string</code> does not get imported into
809 the global namespace. Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
810 <code>using</code>-declarations can wrapped in macros that
811 are set based on autoconf-tests to either "" or i.e. <code>using
812 std::string;</code> (depending on whether the system has
813 libstdc++ in <code>std::</code> or not). (ideas from
814 <email>llewelly@dbritsch.dsl.xmission.com</email>, Karl Nelson <email>kenelson@ece.ucdavis.edu</email>)
821 <sect1 id="manual.intro.using.macros" xreflabel="Macros">
822 <?dbhtml filename="using_macros.html"?>
823 <title>Macros</title>
825 <para>All pre-processor switches and configurations are all gathered
826 in the file <code>c++config.h</code>, which is generated during
827 the libstdc++ configuration and build process, and included by
828 files part of the public libstdc++ API. Most of these macros
829 should not be used by consumers of libstdc++, and are reserved
830 for internal implementation use. <emphasis>These macros cannot be
831 redefined</emphasis>. However, a select handful of these macro
832 control libstdc++ extensions and extra features, or provide
833 versioning information for the API, and are able to be used.
836 <para>All library macros begin with <code>_GLIBCXX_</code> (except for
837 versions 3.1.x to 3.3.x, which use <code>_GLIBCPP_</code>).
840 <para>Below is the macro which users may check for library version
845 <term><code>__GLIBCXX__</code></term>
847 <para>The current version of
848 libstdc++ in compressed ISO date format, form of an unsigned
849 long. For details on the value of this particular macro for a
850 particular release, please consult this <ulink url="abi.html">
857 <para>Below are the macros which users may change with #define/#undef or
858 with -D/-U compiler flags. The default state of the symbol is
861 <para><quote>Configurable</quote> (or <quote>Not configurable</quote>) means
862 that the symbol is initially chosen (or not) based on
863 --enable/--disable options at library build and configure time
864 (documented <link linkend="manual.intro.setup.configure">here</link>), with the
865 various --enable/--disable choices being translated to
869 <para> <acronym>ABI</acronym> means that changing from the default value may
870 mean changing the <acronym>ABI</acronym> of compiled code. In other words, these
871 choices control code which has already been compiled (i.e., in a
872 binary such as libstdc++.a/.so). If you explicitly #define or
873 #undef these macros, the <emphasis>headers</emphasis> may see different code
874 paths, but the <emphasis>libraries</emphasis> which you link against will not.
875 Experimenting with different values with the expectation of
876 consistent linkage requires changing the config headers before
877 building/installing the library.
881 <varlistentry><term><code>_GLIBCXX_DEPRECATED</code></term>
884 Defined by default. Not configurable. ABI-changing. Turning this off
885 removes older ARM-style iostreams code, and other anachronisms
886 from the API. This macro is dependent on the version of the
887 standard being tracked, and as a result may give different results for
888 <code>-std=c++98</code> and <code>-std=c++0x</code>. This may
889 be useful in updating old C++ code which no longer meet the
890 requirements of the language, or for checking current code
891 against new language standards.
893 </listitem></varlistentry>
895 <varlistentry><term><code>_GLIBCXX_FORCE_NEW</code></term>
898 Undefined by default. When defined, memory allocation and
899 allocators controlled by libstdc++ call operator new/delete
900 without caching and pooling. Configurable via
901 <code>--enable-libstdcxx-allocator</code>. ABI-changing.
903 </listitem></varlistentry>
906 <varlistentry><term><code>_GLIBCXX_CONCEPT_CHECKS</code></term>
909 Undefined by default. Configurable via
910 <code>--enable-concept-checks</code>. When defined, performs
911 compile-time checking on certain template instantiations to
912 detect violations of the requirements of the standard. This
913 is described in more detail <ulink
914 url="../19_diagnostics/howto.html#3">here</ulink>.
916 </listitem></varlistentry>
918 <varlistentry><term><code>_GLIBCXX_DEBUG</code></term>
921 Undefined by default. When defined, compiles
922 user code using the <ulink url="../ext/debug.html#safe">libstdc++ debug
925 </listitem></varlistentry>
926 <varlistentry><term><code>_GLIBCXX_DEBUG_PEDANTIC</code></term>
929 Undefined by default. When defined while
930 compiling with the <ulink url="../ext/debug.html#safe">libstdc++ debug
931 mode</ulink>, makes the debug mode extremely picky by making the use
932 of libstdc++ extensions and libstdc++-specific behavior into
935 </listitem></varlistentry>
936 <varlistentry><term><code>_GLIBCXX_PARALLEL</code></term>
938 <para>Undefined by default. When defined, compiles
939 user code using the <ulink url="../ext/parallel_mode.html">libstdc++ parallel
942 </listitem></varlistentry>
948 <sect1 id="manual.intro.using.concurrency" xreflabel="Concurrency">
949 <?dbhtml filename="using_concurrency.html"?>
950 <title>Concurrency</title>
952 <para>This section discusses issues surrounding the proper compilation
953 of multithreaded applications which use the Standard C++
954 library. This information is GCC-specific since the C++
955 standard does not address matters of multithreaded applications.
958 <sect2 id="manual.intro.using.concurrency.prereq" xreflabel="Thread Prereq">
959 <title>Prerequisites</title>
961 <para>All normal disclaimers aside, multithreaded C++ application are
962 only supported when libstdc++ and all user code was built with
963 compilers which report (via <code> gcc/g++ -v </code>) the same thread
964 model and that model is not <emphasis>single</emphasis>. As long as your
965 final application is actually single-threaded, then it should be
966 safe to mix user code built with a thread model of
967 <emphasis>single</emphasis> with a libstdc++ and other C++ libraries built
968 with another thread model useful on the platform. Other mixes
969 may or may not work but are not considered supported. (Thus, if
970 you distribute a shared C++ library in binary form only, it may
971 be best to compile it with a GCC configured with
972 --enable-threads for maximal interchangeability and usefulness
973 with a user population that may have built GCC with either
974 --enable-threads or --disable-threads.)
976 <para>When you link a multithreaded application, you will probably
977 need to add a library or flag to g++. This is a very
978 non-standardized area of GCC across ports. Some ports support a
979 special flag (the spelling isn't even standardized yet) to add
980 all required macros to a compilation (if any such flags are
981 required then you must provide the flag for all compilations not
982 just linking) and link-library additions and/or replacements at
983 link time. The documentation is weak. Here is a quick summary
984 to display how ad hoc this is: On Solaris, both -pthreads and
985 -threads (with subtly different meanings) are honored. On OSF,
986 -pthread and -threads (with subtly different meanings) are
987 honored. On Linux/i386, -pthread is honored. On FreeBSD,
988 -pthread is honored. Some other ports use other switches.
989 AFAIK, none of this is properly documented anywhere other than
990 in ``gcc -dumpspecs'' (look at lib and cpp entries).
995 <sect2 id="manual.intro.using.concurrency.thread_safety" xreflabel="Thread Safety">
996 <title>Thread Safety</title>
1000 We currently use the <ulink url="http://www.sgi.com/tech/stl/thread_safety.html">SGI STL</ulink> definition of thread safety.
1004 <para>The library strives to be thread-safe when all of the following
1009 <para>The system's libc is itself thread-safe,
1014 The compiler in use reports a thread model other than
1015 'single'. This can be tested via output from <code>gcc
1016 -v</code>. Multi-thread capable versions of gcc output
1017 something like this:
1021 Using built-in specs.
1024 gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
1027 <para>Look for "Thread model" lines that aren't equal to "single."</para>
1031 Requisite command-line flags are used for atomic operations
1032 and threading. Examples of this include <code>-pthread</code>
1033 and <code>-march=native</code>, although specifics vary
1034 depending on the host environment. See <ulink
1035 url="http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html">Machine
1036 Dependent Options</ulink>.
1041 An implementation of atomicity.h functions
1042 exists for the architecture in question. See the internals documentation for more <ulink url="../ext/concurrence.html">details</ulink>.
1047 <para>The user-code must guard against concurrent method calls which may
1048 access any particular library object's state. Typically, the
1049 application programmer may infer what object locks must be held
1050 based on the objects referenced in a method call. Without getting
1051 into great detail, here is an example which requires user-level
1055 library_class_a shared_object_a;
1058 library_class_b *object_b = new library_class_b;
1059 shared_object_a.add_b (object_b); // must hold lock for shared_object_a
1060 shared_object_a.mutate (); // must hold lock for shared_object_a
1063 // Multiple copies of thread_main() are started in independent threads.</programlisting>
1064 <para>Under the assumption that object_a and object_b are never exposed to
1065 another thread, here is an example that should not require any
1070 library_class_a object_a;
1071 library_class_b *object_b = new library_class_b;
1072 object_a.add_b (object_b);
1075 <para>All library objects are safe to use in a multithreaded program as
1076 long as each thread carefully locks out access by any other
1077 thread while it uses any object visible to another thread, i.e.,
1078 treat library objects like any other shared resource. In general,
1079 this requirement includes both read and write access to objects;
1080 unless otherwise documented as safe, do not assume that two threads
1081 may access a shared standard library object at the same time.
1083 <para>See chapters <ulink url="../17_intro/howto.html#3">17</ulink> (library
1084 introduction), <ulink url="../23_containers/howto.html#3">23</ulink>
1085 (containers), and <ulink url="../27_io/howto.html#9">27</ulink> (I/O) for
1091 <sect2 id="manual.intro.using.concurrency.atomics" xreflabel="Atomics">
1092 <title>Atomics</title>
1097 <sect2 id="manual.intro.using.concurrency.io" xreflabel="IO">
1099 <para>I'll assume that you have already read the
1100 <ulink url="../17_intro/howto.html#3">general notes on library threads</ulink>,
1102 <ulink url="../23_containers/howto.html#3">notes on threaded container
1103 access</ulink> (you might not think of an I/O stream as a container, but
1104 the points made there also hold here). If you have not read them,
1107 <para>This gets a bit tricky. Please read carefully, and bear with me.
1110 <sect3 id="concurrency.io.structure" xreflabel="Structure">
1111 <title>Structure</title>
1113 type called <code>__basic_file</code> provides our abstraction layer
1114 for the <code>std::filebuf</code> classes. Nearly all decisions dealing
1115 with actual input and output must be made in <code>__basic_file</code>.
1117 <para>A generic locking mechanism is somewhat in place at the filebuf layer,
1118 but is not used in the current code. Providing locking at any higher
1119 level is akin to providing locking within containers, and is not done
1120 for the same reasons (see the links above).
1124 <sect3 id="concurrency.io.defaults" xreflabel="Defaults">
1125 <title>Defaults</title>
1126 <para>The __basic_file type is simply a collection of small wrappers around
1127 the C stdio layer (again, see the link under Structure). We do no
1128 locking ourselves, but simply pass through to calls to <code>fopen</code>,
1129 <code>fwrite</code>, and so forth.
1131 <para>So, for 3.0, the question of "is multithreading safe for I/O"
1132 must be answered with, "is your platform's C library threadsafe
1133 for I/O?" Some are by default, some are not; many offer multiple
1134 implementations of the C library with varying tradeoffs of threadsafety
1135 and efficiency. You, the programmer, are always required to take care
1136 with multiple threads.
1138 <para>(As an example, the POSIX standard requires that C stdio FILE*
1139 operations are atomic. POSIX-conforming C libraries (e.g, on Solaris
1140 and GNU/Linux) have an internal mutex to serialize operations on
1141 FILE*s. However, you still need to not do stupid things like calling
1142 <code>fclose(fs)</code> in one thread followed by an access of
1143 <code>fs</code> in another.)
1145 <para>So, if your platform's C library is threadsafe, then your
1146 <code>fstream</code> I/O operations will be threadsafe at the lowest
1147 level. For higher-level operations, such as manipulating the data
1148 contained in the stream formatting classes (e.g., setting up callbacks
1149 inside an <code>std::ofstream</code>), you need to guard such accesses
1150 like any other critical shared resource.
1154 <sect3 id="concurrency.io.future" xreflabel="Future">
1155 <title>Future</title>
1157 second choice may be available for I/O implementations: libio. This is
1158 disabled by default, and in fact will not currently work due to other
1159 issues. It will be revisited, however.
1161 <para>The libio code is a subset of the guts of the GNU libc (glibc) I/O
1162 implementation. When libio is in use, the <code>__basic_file</code>
1163 type is basically derived from FILE. (The real situation is more
1164 complex than that... it's derived from an internal type used to
1165 implement FILE. See libio/libioP.h to see scary things done with
1166 vtbls.) The result is that there is no "layer" of C stdio
1167 to go through; the filebuf makes calls directly into the same
1168 functions used to implement <code>fread</code>, <code>fwrite</code>,
1169 and so forth, using internal data structures. (And when I say
1170 "makes calls directly," I mean the function is literally
1171 replaced by a jump into an internal function. Fast but frightening.
1174 <para>Also, the libio internal locks are used. This requires pulling in
1175 large chunks of glibc, such as a pthreads implementation, and is one
1176 of the issues preventing widespread use of libio as the libstdc++
1177 cstdio implementation.
1179 <para>But we plan to make this work, at least as an option if not a future
1180 default. Platforms running a copy of glibc with a recent-enough
1181 version will see calls from libstdc++ directly into the glibc already
1182 installed. For other platforms, a copy of the libio subsection will
1183 be built and included in libstdc++.
1187 <sect3 id="concurrency.io.alt" xreflabel="Alt">
1188 <title>Alternatives</title>
1189 <para>Don't forget that other cstdio implementations are possible. You could
1190 easily write one to perform your own forms of locking, to solve your
1191 "interesting" problems.
1197 <sect2 id="manual.intro.using.concurrency.containers" xreflabel="Containers">
1198 <title>Containers</title>
1200 <para>This section discusses issues surrounding the design of
1201 multithreaded applications which use Standard C++ containers.
1202 All information in this section is current as of the gcc 3.0
1203 release and all later point releases. Although earlier gcc
1204 releases had a different approach to threading configuration and
1205 proper compilation, the basic code design rules presented here
1206 were similar. For information on all other aspects of
1207 multithreading as it relates to libstdc++, including details on
1208 the proper compilation of threaded code (and compatibility between
1209 threaded and non-threaded code), see Chapter 17.
1211 <para>Two excellent pages to read when working with the Standard C++
1212 containers and threads are
1213 <ulink url="http://www.sgi.com/tech/stl/thread_safety.html">SGI's
1214 http://www.sgi.com/tech/stl/thread_safety.html</ulink> and
1215 <ulink url="http://www.sgi.com/tech/stl/Allocators.html">SGI's
1216 http://www.sgi.com/tech/stl/Allocators.html</ulink>.
1218 <para><emphasis>However, please ignore all discussions about the user-level
1219 configuration of the lock implementation inside the STL
1220 container-memory allocator on those pages. For the sake of this
1221 discussion, libstdc++ configures the SGI STL implementation,
1222 not you. This is quite different from how gcc pre-3.0 worked.
1223 In particular, past advice was for people using g++ to
1224 explicitly define _PTHREADS or other macros or port-specific
1225 compilation options on the command line to get a thread-safe
1226 STL. This is no longer required for any port and should no
1227 longer be done unless you really know what you are doing and
1228 assume all responsibility.</emphasis>
1230 <para>Since the container implementation of libstdc++ uses the SGI
1231 code, we use the same definition of thread safety as SGI when
1232 discussing design. A key point that beginners may miss is the
1233 fourth major paragraph of the first page mentioned above
1234 ("For most clients,"...), which points out that
1235 locking must nearly always be done outside the container, by
1236 client code (that'd be you, not us). There is a notable
1237 exceptions to this rule. Allocators called while a container or
1238 element is constructed uses an internal lock obtained and
1239 released solely within libstdc++ code (in fact, this is the
1240 reason STL requires any knowledge of the thread configuration).
1242 <para>For implementing a container which does its own locking, it is
1243 trivial to provide a wrapper class which obtains the lock (as
1244 SGI suggests), performs the container operation, and then
1245 releases the lock. This could be templatized <emphasis>to a certain
1246 extent</emphasis>, on the underlying container and/or a locking
1247 mechanism. Trying to provide a catch-all general template
1248 solution would probably be more trouble than it's worth.
1250 <para>The STL implementation is currently configured to use the
1251 high-speed caching memory allocator. Some people like to
1252 test and/or normally run threaded programs with a different
1253 default. For all details about how to globally override this
1254 at application run-time see <ulink url="../ext/howto.html#3">here</ulink>.
1256 <para>There is a better way (not standardized yet): It is possible to
1257 force the malloc-based allocator on a per-case-basis for some
1258 application code. The library team generally believes that this
1259 is a better way to tune an application for high-speed using this
1260 implementation of the STL. There is
1261 <ulink url="../ext/howto.html#3">more information on allocators here</ulink>.
1267 <sect1 id="manual.intro.using.exception" xreflabel="Exceptions">
1268 <?dbhtml filename="using_exceptions.html"?>
1269 <title>Exceptions</title>
1271 <sect2 id="intro.using.exception.propagating" xreflabel="Propagating Exceptions">
1273 <title>Propagating Exceptions aka Exception Neutrality</title>
1278 <sect2 id="intro.using.exception.safety" xreflabel="Exception Safety">
1279 <title>Exception Safety</title>
1284 <sect2 id="intro.using.exception.no" xreflabel="-fno-exceptions">
1285 <title>Support for <literal>-fno-exceptions</literal></title>
1292 <!-- Section 0x : Debug -->
1293 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
1294 parse="xml" href="debug.xml">