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 <title>Headers</title>
83 <sect2 id="manual.intro.using.headers.all" xreflabel="Header Files">
84 <title>Header Files</title>
87 The C++ standard specifies the entire set of header files that
88 must be available to all hosted implementations. Actually, the
89 word "files" is a misnomer, since the contents of the
90 headers don't necessarily have to be in any kind of external
91 file. The only rule is that when one <code>#include</code>'s a
92 header, the contents of that header become available, no matter
97 That said, in practice files are used.
101 There are two main types of include files: header files related
102 to a specific version of the ISO C++ standard (called Standard
103 Headers), and all others (TR1, C++ ABI, and Extensions).
107 Two dialects of standard headers are supported, corresponding to
108 the 1998 standard as updated for 2003, and the draft of the
109 upcoming 200x standard.
113 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>.
117 <title>C++ 1998 Library Headers</title>
118 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
119 <colspec colname='c1'></colspec>
120 <colspec colname='c2'></colspec>
121 <colspec colname='c3'></colspec>
122 <colspec colname='c4'></colspec>
124 <row><entry><filename class="headerfile">algorithm</filename></entry><entry><filename class="headerfile">iomanip</filename></entry><entry><filename class="headerfile">list</filename></entry><entry><filename class="headerfile">ostream</filename></entry><entry><filename class="headerfile">streambuf</filename></entry></row>
125 <row><entry><filename class="headerfile">bitset</filename></entry><entry><filename class="headerfile">ios</filename></entry><entry><filename class="headerfile">locale</filename></entry><entry><filename class="headerfile">queue</filename></entry><entry><filename class="headerfile">string</filename></entry></row>
126 <row><entry><filename class="headerfile">complex</filename></entry><entry><filename class="headerfile">iosfwd</filename></entry><entry><filename class="headerfile">map</filename></entry><entry><filename class="headerfile">set</filename></entry><entry><filename class="headerfile">typeinfo</filename></entry></row>
127 <row><entry><filename class="headerfile">deque</filename></entry><entry><filename class="headerfile">iostream</filename></entry><entry><filename class="headerfile">memory</filename></entry><entry><filename class="headerfile">sstream</filename></entry><entry><filename class="headerfile">utility</filename></entry></row>
128 <row><entry><filename class="headerfile">exception</filename></entry><entry><filename class="headerfile">istream</filename></entry><entry><filename class="headerfile">new</filename></entry><entry><filename class="headerfile">stack</filename></entry><entry><filename class="headerfile">valarray</filename></entry></row>
129 <row><entry><filename class="headerfile">fstream</filename></entry><entry><filename class="headerfile">iterator</filename></entry><entry><filename class="headerfile">numeric</filename></entry><entry><filename class="headerfile">stdexcept</filename></entry><entry><filename class="headerfile">vector</filename></entry></row>
130 <row><entry><filename class="headerfile">functional</filename></entry><entry><filename class="headerfile">limits</filename></entry></row>
137 <title>C++ 1998 Library Headers for C Library Facilities</title>
138 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
139 <colspec colname='c1'></colspec>
140 <colspec colname='c2'></colspec>
141 <colspec colname='c3'></colspec>
142 <colspec colname='c4'></colspec>
144 <row><entry><filename class="headerfile">cassert</filename></entry><entry><filename class="headerfile">ciso646</filename></entry><entry><filename class="headerfile">csetjmp</filename></entry><entry><filename class="headerfile">cstdio</filename></entry><entry><filename class="headerfile">ctime</filename></entry></row>
145 <row><entry><filename class="headerfile">cctype</filename></entry><entry><filename class="headerfile">climits</filename></entry><entry><filename class="headerfile">csignal</filename></entry><entry><filename class="headerfile">cstdlib</filename></entry><entry><filename class="headerfile">cwchar</filename></entry></row>
146 <row><entry><filename class="headerfile">cerrno</filename></entry><entry><filename class="headerfile">clocale</filename></entry><entry><filename class="headerfile">cstdarg</filename></entry><entry><filename class="headerfile">cstring</filename></entry><entry><filename class="headerfile">cwctype</filename></entry></row>
147 <row><entry><filename class="headerfile">cfloat</filename></entry><entry><filename class="headerfile">cmath</filename></entry><entry><filename class="headerfile">cstddef</filename></entry></row>
152 <para>C++0x include files. These are only available in C++0x compilation mode, i.e. <code>-std=c++0x</code> or <code>-std=gnu++0x</code>.
157 <title>C++ 200x Library Headers</title>
158 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
159 <colspec colname='c1'></colspec>
160 <colspec colname='c2'></colspec>
161 <colspec colname='c3'></colspec>
162 <colspec colname='c4'></colspec>
164 <row><entry><filename class="headerfile">algorithm</filename></entry><entry><filename class="headerfile">iomanip</filename></entry><entry><filename class="headerfile">locale</filename></entry><entry><filename class="headerfile">regex</filename></entry><entry><filename class="headerfile">tuple</filename></entry></row>
165 <row><entry><filename class="headerfile">array</filename></entry><entry><filename class="headerfile">ios</filename></entry><entry><filename class="headerfile">map</filename></entry><entry><filename class="headerfile">set</filename></entry><entry><filename class="headerfile">typeinfo</filename></entry></row>
166 <row><entry><filename class="headerfile">bitset</filename></entry><entry><filename class="headerfile">iosfwd</filename></entry><entry><filename class="headerfile">memory</filename></entry><entry><filename class="headerfile">sstream</filename></entry><entry><filename class="headerfile">type_traits</filename></entry></row>
167 <row><entry><filename class="headerfile">complex</filename></entry><entry><filename class="headerfile">iostream</filename></entry><entry><filename class="headerfile">new</filename></entry><entry><filename class="headerfile">stack</filename></entry><entry><filename class="headerfile">unordered_map</filename></entry></row>
168 <row><entry><filename class="headerfile">deque</filename></entry><entry><filename class="headerfile">istream</filename></entry><entry><filename class="headerfile">numeric</filename></entry><entry><filename class="headerfile">stdexcept</filename></entry><entry><filename class="headerfile">unordered_set</filename></entry></row>
169 <row><entry><filename class="headerfile">exception</filename></entry><entry><filename class="headerfile">iterator</filename></entry><entry><filename class="headerfile">ostream</filename></entry><entry><filename class="headerfile">streambuf</filename></entry><entry><filename class="headerfile">utility</filename></entry></row>
170 <row><entry><filename class="headerfile">fstream</filename></entry><entry><filename class="headerfile">limits</filename></entry><entry><filename class="headerfile">queue</filename></entry><entry><filename class="headerfile">string</filename></entry><entry><filename class="headerfile">valarray</filename></entry></row>
171 <row><entry><filename class="headerfile">functional</filename></entry><entry><filename class="headerfile">list</filename></entry><entry><filename class="headerfile">random</filename></entry><entry><filename class="headerfile">system_error</filename></entry><entry><filename class="headerfile">vector</filename></entry></row>
179 <title>C++ 200x Library Headers for C Library Facilities</title>
180 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
181 <colspec colname='c1'></colspec>
182 <colspec colname='c2'></colspec>
183 <colspec colname='c3'></colspec>
184 <colspec colname='c4'></colspec>
185 <colspec colname='c5'></colspec>
187 <row><entry><filename class="headerfile">cassert</filename></entry><entry><filename class="headerfile">cfloat</filename></entry><entry><filename class="headerfile">cmath</filename></entry><entry><filename class="headerfile">cstddef</filename></entry><entry><filename class="headerfile">ctgmath</filename></entry></row>
188 <row><entry><filename class="headerfile">ccomplex</filename></entry><entry><filename class="headerfile">cinttypes</filename></entry><entry><filename class="headerfile">csetjmp</filename></entry><entry><filename class="headerfile">cstdint</filename></entry><entry><filename class="headerfile">ctime</filename></entry></row>
189 <row><entry><filename class="headerfile">cctype</filename></entry><entry><filename class="headerfile">ciso646</filename></entry><entry><filename class="headerfile">csignal</filename></entry><entry><filename class="headerfile">cstdio</filename></entry><entry><filename class="headerfile">cuchar</filename></entry></row>
190 <row><entry><filename class="headerfile">cerrno</filename></entry><entry><filename class="headerfile">climits</filename></entry><entry><filename class="headerfile">cstdarg</filename></entry><entry><filename class="headerfile">cstdlib</filename></entry><entry><filename class="headerfile">cwchar</filename></entry></row>
191 <row><entry><filename class="headerfile">cfenv</filename></entry><entry><filename class="headerfile">clocale</filename></entry><entry><filename class="headerfile">cstdbool</filename></entry><entry><filename class="headerfile">cstring</filename></entry><entry><filename class="headerfile">cwctype</filename></entry></row>
198 In addition, TR1 includes as:
202 <title>C++ TR1 Library Headers</title>
203 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
204 <colspec colname='c1'></colspec>
205 <colspec colname='c2'></colspec>
206 <colspec colname='c3'></colspec>
207 <colspec colname='c4'></colspec>
208 <colspec colname='c5'></colspec>
211 <row><entry><filename class="headerfile">tr1/array</filename></entry><entry><filename class="headerfile">tr1/memory</filename></entry><entry><filename class="headerfile">tr1/regex</filename></entry><entry><filename class="headerfile">tr1/type_traits</filename></entry><entry><filename class="headerfile">tr1/unordered_set</filename></entry></row>
212 <row><entry><filename class="headerfile">tr1/complex</filename></entry><entry><filename class="headerfile">tr1/random</filename></entry><entry><filename class="headerfile">tr1/tuple</filename></entry><entry><filename class="headerfile">tr1/unordered_map</filename></entry><entry><filename class="headerfile">tr1/utility</filename></entry></row>
213 <row><entry><filename class="headerfile">tr1/functional</filename></entry></row>
222 <title>C++ TR1 Library Headers for C Library Facilities</title>
223 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
224 <colspec colname='c1'></colspec>
225 <colspec colname='c2'></colspec>
226 <colspec colname='c3'></colspec>
227 <colspec colname='c4'></colspec>
228 <colspec colname='c5'></colspec>
231 <row><entry><filename class="headerfile">tr1/cmath</filename></entry><entry><filename class="headerfile">tr1/cfloat</filename></entry><entry><filename class="headerfile">tr1/cstdarg</filename></entry><entry><filename class="headerfile">tr1/cstdio</filename></entry><entry><filename class="headerfile">tr1/ctime</filename></entry></row>
232 <row><entry><filename class="headerfile">tr1/ccomplex</filename></entry><entry><filename class="headerfile">tr1/cinttypes</filename></entry><entry><filename class="headerfile">tr1/cstdbool</filename></entry><entry><filename class="headerfile">tr1/cstdlib</filename></entry><entry><filename class="headerfile">tr1/cwchar</filename></entry></row>
233 <row><entry><filename class="headerfile">tr1/cfenv</filename></entry><entry><filename class="headerfile">tr1/climits</filename></entry><entry><filename class="headerfile">tr1/cstdint</filename></entry><entry><filename class="headerfile">tr1/ctgmath</filename></entry><entry><filename class="headerfile">tr1/cwctype</filename></entry></row>
239 Also included are files for the C++ ABI interface:
243 <title>C++ ABI Headers</title>
244 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
245 <colspec colname='c1'></colspec>
246 <colspec colname='c2'></colspec>
248 <row><entry><filename class="headerfile">cxxabi.h</filename></entry><entry><filename class="headerfile">cxxabi_forced.h</filename></entry></row>
254 And a large variety of extensions.
258 <title>Extension Headers</title>
259 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
260 <colspec colname='c1'></colspec>
261 <colspec colname='c2'></colspec>
262 <colspec colname='c3'></colspec>
263 <colspec colname='c4'></colspec>
264 <colspec colname='c5'></colspec>
267 <row><entry><filename class="headerfile">ext/algorithm</filename></entry><entry><filename class="headerfile">ext/debug_allocator.h</filename></entry><entry><filename class="headerfile">ext/mt_allocator.h</filename></entry><entry><filename class="headerfile">ext/pod_char_traits.h</filename></entry><entry><filename class="headerfile">ext/stdio_sync_filebuf.h</filename></entry></row>
268 <row><entry><filename class="headerfile">ext/array_allocator.h</filename></entry><entry><filename class="headerfile">ext/enc_filebuf.h</filename></entry><entry><filename class="headerfile">ext/new_allocator.h</filename></entry><entry><filename class="headerfile">ext/pool_allocator.h</filename></entry><entry><filename class="headerfile">ext/throw_allocator.h</filename></entry></row>
269 <row><entry><filename class="headerfile">ext/atomicity.h</filename></entry><entry><filename class="headerfile">ext/functional</filename></entry><entry><filename class="headerfile">ext/numeric</filename></entry><entry><filename class="headerfile">ext/rb_tree</filename></entry><entry><filename class="headerfile">ext/typelist.h</filename></entry></row>
270 <row><entry><filename class="headerfile">ext/bitmap_allocator.h</filename></entry><entry><filename class="headerfile">ext/iterator</filename></entry><entry><filename class="headerfile">ext/numeric_traits.h</filename></entry><entry><filename class="headerfile">ext/rope</filename></entry><entry><filename class="headerfile">ext/type_traits.h</filename></entry></row>
271 <row><entry><filename class="headerfile">ext/codecvt_specializations.h</filename></entry><entry><filename class="headerfile">ext/malloc_allocator.h</filename></entry><entry><filename class="headerfile">ext/pb_ds/assoc_container.h</filename></entry><entry><filename class="headerfile">ext/slist</filename></entry><entry><filename class="headerfile">ext/vstring.h</filename></entry></row>
272 <row><entry><filename class="headerfile">ext/concurrence.h</filename></entry><entry><filename class="headerfile">ext/memory</filename></entry><entry><filename class="headerfile">ext/pb_ds/priority_queue.h</filename></entry><entry><filename class="headerfile">ext/stdio_filebuf.h</filename></entry></row>
280 <title>Extension Debug Headers</title>
281 <tgroup cols='5' align='left' colsep='1' rowsep='1'>
282 <colspec colname='c1'></colspec>
283 <colspec colname='c2'></colspec>
284 <colspec colname='c3'></colspec>
285 <colspec colname='c4'></colspec>
286 <colspec colname='c5'></colspec>
289 <row><entry><filename class="headerfile">debug/bitset</filename></entry><entry><filename class="headerfile">debug/list</filename></entry><entry><filename class="headerfile">debug/set</filename></entry><entry><filename class="headerfile">debug/unordered_map</filename></entry><entry><filename class="headerfile">debug/vector</filename></entry></row>
290 <row><entry><filename class="headerfile">debug/deque</filename></entry><entry><filename class="headerfile">debug/map</filename></entry><entry><filename class="headerfile">debug/string</filename></entry><entry><filename class="headerfile">debug/unordered_set</filename></entry></row>
298 <title>Extension Parallel Headers</title>
299 <tgroup cols='2' align='left' colsep='1' rowsep='1'>
300 <colspec colname='c1'></colspec>
301 <colspec colname='c2'></colspec>
303 <row><entry><filename class="headerfile">parallel/algorithm</filename></entry><entry><filename class="headerfile">parallel/numeric</filename></entry></row>
310 <sect2 id="manual.intro.using.headers.mixing" xreflabel="Mixing Headers">
311 <title>Mixing Headers</title>
313 <para> A few simple rules.
316 <para>First, mixing different dialects of the standard headers is not
317 possible. It's an all-or-nothing affair. Thus, code like
321 #include <array>
322 #include <functional>
325 <para>Implies C++0x mode. To use the entities in <array>, the C++0x
326 compilation mode must be used, which implies the C++0x functionality
327 (and deprecations) in <functional> will be present.
330 <para>Second, the other headers can be included with either dialect of
331 the standard headers, although features and types specific to C++0x
332 are still only enabled when in C++0x compilation mode. So, to use
333 rvalue references with <code>__gnu_cxx::vstring</code>, or to use the
334 debug-mode versions of <code>std::unordered_map</code>, one must use
335 the <code>std=gnu++0x</code> compiler flag. (Or <code>std=c++0x</code>, of course.)
338 <para>A special case of the second rule is the mixing of TR1 and C++0x
339 facilities. It is possible (although not especially prudent) to
340 include both the TR1 version and the C++0x version of header in the
341 same translation unit:
345 #include <tr1/type_traits>
346 #include <type_traits>
349 <para> Several parts of C++0x diverge quite substantially from TR1 predecessors.
353 <sect2 id="manual.intro.using.headers.cheaders" xreflabel="C Headers and">
354 <title>The C Headers and <code>namespace std</code></title>
357 The standard specifies that if one includes the C-style header
358 (<math.h> in this case), the symbols will be available
359 in the global namespace and perhaps in
360 namespace <code>std::</code> (but this is no longer a firm
361 requirement.) One the other hand, including the C++-style
362 header (<cmath>) guarantees that the entities will be
363 found in namespace std and perhaps in the global namespace.
367 Usage of C++-style headers is recommended, as then
368 C-linkage names can be disambiguated by explicit qualification, such
369 as by <code>std::abort</code>. In addition, the C++-style headers can
370 use function overloading to provide a simpler interface to certain
371 families of C-functions. For instance in <cmath>, the
372 function <code>std::sin</code> has overloads for all the builtin
373 floating-point types. This means that <code>std::sin</code> can be
374 used uniformly, instead of a combination
375 of <code>std::sinf</code>, <code>std::sin</code>,
376 and <code>std::sinl</code>.
380 <sect2 id="manual.intro.using.headers.pre" xreflabel="Precompiled Headers">
381 <title>Precompiled Headers</title>
384 <para>There are three base header files that are provided. They can be
385 used to precompile the standard headers and extensions into binary
386 files that may the be used to speed compiles that use these headers.
392 <para>stdc++.h</para>
393 <para>Includes all standard headers. Actual content varies depending on
399 <para>stdtr1c++.h</para>
400 <para>Includes all of <stdc++.h>, and adds all the TR1 headers.
404 <listitem><para>extc++.h</para>
405 <para>Includes all of <stdtr1c++.h>, and adds all the Extension headers.
409 <para>How to construct a .gch file from one of these base header files.</para>
411 <para>First, find the include directory for the compiler. One way to do
417 #include <...> search starts here:
418 /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
424 <para>Then, create a precompiled header file with the same flags that
425 will be used to compile other projects.</para>
428 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
431 <para>The resulting file will be quite large: the current size is around
432 thirty megabytes. </para>
434 <para>How to use the resulting file.</para>
437 g++ -I. -include stdc++.h -H -g -O2 hello.cc
440 <para>Verification that the PCH file is being used is easy:</para>
443 g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
445 . /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
446 . /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string
449 <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>
452 <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>.
459 <sect1 id="manual.intro.using.namespaces" xreflabel="Namespaces">
460 <title>Namespaces</title>
462 <sect2 id="manual.intro.using.namespaces.all" xreflabel="Available Namespaces">
463 <title>Available Namespaces</title>
467 <para> There are three main namespaces.
471 <listitem><para>std</para>
472 <para>The ISO C++ standards specify that "all library entities are defined
473 within namespace std." This includes namespaces nested
474 within <code>namespace std</code>, such as <code>namespace
478 <listitem><para>abi</para>
479 <para>Specified by the C++ ABI. This ABI specifies a number of type and
480 function APIs supplemental to those required by the ISO C++ Standard,
481 but necessary for interoperability.
485 <listitem><para>__gnu_</para>
486 <para>Indicating one of several GNU extensions. Choices
487 include <code>__gnu_cxx</code>, <code>__gnu_debug</code>, <code>__gnu_parallel</code>,
488 and <code>__gnu_pbds</code>.
492 <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>.
498 <sect2 id="manual.intro.using.namespaces.std" xreflabel="namespace std">
499 <title>namespace std</title>
503 One standard requirement is that the library components are defined
504 in <code>namespace std::</code>. Thus, in order to use these types or
505 functions, one must do one of two things:
509 <listitem><para>put a kind of <emphasis>using-declaration</emphasis> in your source
510 (either <code>using namespace std;</code> or i.e. <code>using
511 std::string;</code>) This approach works well for individual source files, but
512 should not be used in a global context, like header files.
513 </para></listitem> <listitem><para>use a <emphasis>fully
514 qualified name</emphasis>for each library symbol
515 (i.e. <code>std::string</code>, <code>std::cout</code>) Always can be
516 used, and usually enhanced, by strategic use of typedefs. (In the
517 cases where the qualified verbiage becomes unwieldy.)
524 <sect2 id="manual.intro.using.namespaces.comp" xreflabel="Using Namespace Composition">
525 <title>Using Namespace Composition</title>
528 Best practice in programming suggests sequestering new data or
529 functionality in a sanely-named, unique namespace whenever
530 possible. This is considered an advantage over dumping everything in
531 the global namespace, as then name look-up can be explicitly enabled or
532 disabled as above, symbols are consistently mangled without repetitive
533 naming prefixes or macros, etc.
536 <para>For instance, consider a project that defines most of its classes in <code>namespace gtk</code>. It is possible to
537 adapt <code>namespace gtk</code> to <code>namespace std</code> by using a C++-feature called
538 <emphasis>namespace composition</emphasis>. This is what happens if
539 a <emphasis>using</emphasis>-declaration is put into a
540 namespace-definition: the imported symbol(s) gets imported into the
541 currently active namespace(s). For example:
547 using std::tr1::array;
549 class Window { ... };
553 In this example, <code>std::string</code> gets imported into
554 <code>namespace gtk</code>. The result is that use of
555 <code>std::string</code> inside namespace gtk can just use <code>string</code>, without the explicit qualification.
557 <code>std::string</code> does not get imported into
558 the global namespace. Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
559 <code>using</code>-declarations can wrapped in macros that
560 are set based on autoconf-tests to either "" or i.e. <code>using
561 std::string;</code> (depending on whether the system has
562 libstdc++ in <code>std::</code> or not). (ideas from
563 <email>llewelly@dbritsch.dsl.xmission.com</email>, Karl Nelson <email>kenelson@ece.ucdavis.edu</email>)
570 <sect1 id="manual.intro.using.macros" xreflabel="Macros">
571 <title>Macros</title>
573 <para>All pre-processor switches and configurations are all gathered
574 in the file <code>c++config.h</code>, which is generated during
575 the libstdc++ configuration and build process, and included by
576 files part of the public libstdc++ API. Most of these macros
577 should not be used by consumers of libstdc++, and are reserved
578 for internal implementation use. <emphasis>These macros cannot be
579 redefined</emphasis>. However, a select handful of these macro
580 control libstdc++ extensions and extra features, or provide
581 versioning information for the API, and are able to be used.
584 <para>All library macros begin with <code>_GLIBCXX_</code> (except for
585 versions 3.1.x to 3.3.x, which use <code>_GLIBCPP_</code>).
588 <para>Below is the macro which users may check for library version
593 <term><code>__GLIBCXX__</code></term>
595 <para>The current version of
596 libstdc++ in compressed ISO date format, form of an unsigned
597 long. For details on the value of this particular macro for a
598 particular release, please consult this <ulink url="abi.html">
605 <para>Below are the macros which users may change with #define/#undef or
606 with -D/-U compiler flags. The default state of the symbol is
609 <para><quote>Configurable</quote> (or <quote>Not configurable</quote>) means
610 that the symbol is initially chosen (or not) based on
611 --enable/--disable options at library build and configure time
612 (documented <link linkend="manual.intro.setup.configure">here</link>), with the
613 various --enable/--disable choices being translated to
617 <para> <acronym>ABI</acronym> means that changing from the default value may
618 mean changing the <acronym>ABI</acronym> of compiled code. In other words, these
619 choices control code which has already been compiled (i.e., in a
620 binary such as libstdc++.a/.so). If you explicitly #define or
621 #undef these macros, the <emphasis>headers</emphasis> may see different code
622 paths, but the <emphasis>libraries</emphasis> which you link against will not.
623 Experimenting with different values with the expectation of
624 consistent linkage requires changing the config headers before
625 building/installing the library.
629 <varlistentry><term><code>_GLIBCXX_DEPRECATED</code></term>
632 Defined by default. Not configurable. ABI-changing. Turning this off
633 removes older ARM-style iostreams code, and other anachronisms
634 from the API. This macro is dependent on the version of the
635 standard being tracked, and as a result may give different results for
636 <code>-std=c++98</code> and <code>-std=c++0x</code>. This may
637 be useful in updating old C++ code which no longer meet the
638 requirements of the language, or for checking current code
639 against new language standards.
641 </listitem></varlistentry>
643 <varlistentry><term><code>_GLIBCXX_FORCE_NEW</code></term>
646 Undefined by default. When defined, memory allocation and
647 allocators controlled by libstdc++ call operator new/delete
648 without caching and pooling. Configurable via
649 <code>--enable-libstdcxx-allocator</code>. ABI-changing.
651 </listitem></varlistentry>
654 <varlistentry><term><code>_GLIBCXX_CONCEPT_CHECKS</code></term>
657 Undefined by default. Configurable via
658 <code>--enable-concept-checks</code>. When defined, performs
659 compile-time checking on certain template instantiations to
660 detect violations of the requirements of the standard. This
661 is described in more detail <ulink
662 url="../19_diagnostics/howto.html#3">here</ulink>.
664 </listitem></varlistentry>
666 <varlistentry><term><code>_GLIBCXX_DEBUG</code></term>
669 Undefined by default. When defined, compiles
670 user code using the <ulink url="../ext/debug.html#safe">libstdc++ debug
673 </listitem></varlistentry>
674 <varlistentry><term><code>_GLIBCXX_DEBUG_PEDANTIC</code></term>
677 Undefined by default. When defined while
678 compiling with the <ulink url="../ext/debug.html#safe">libstdc++ debug
679 mode</ulink>, makes the debug mode extremely picky by making the use
680 of libstdc++ extensions and libstdc++-specific behavior into
683 </listitem></varlistentry>
684 <varlistentry><term><code>_GLIBCXX_PARALLEL</code></term>
686 <para>Undefined by default. When defined, compiles
687 user code using the <ulink url="../ext/parallel_mode.html">libstdc++ parallel
690 </listitem></varlistentry>
696 <sect1 id="manual.intro.using.concurrency" xreflabel="Concurrency">
697 <title>Concurrency</title>
699 <para>This section discusses issues surrounding the proper compilation
700 of multithreaded applications which use the Standard C++
701 library. This information is GCC-specific since the C++
702 standard does not address matters of multithreaded applications.
705 <sect2 id="manual.intro.using.concurrency.prereq" xreflabel="Thread Prereq">
706 <title>Prerequisites</title>
708 <para>All normal disclaimers aside, multithreaded C++ application are
709 only supported when libstdc++ and all user code was built with
710 compilers which report (via <code> gcc/g++ -v </code>) the same thread
711 model and that model is not <emphasis>single</emphasis>. As long as your
712 final application is actually single-threaded, then it should be
713 safe to mix user code built with a thread model of
714 <emphasis>single</emphasis> with a libstdc++ and other C++ libraries built
715 with another thread model useful on the platform. Other mixes
716 may or may not work but are not considered supported. (Thus, if
717 you distribute a shared C++ library in binary form only, it may
718 be best to compile it with a GCC configured with
719 --enable-threads for maximal interchangeability and usefulness
720 with a user population that may have built GCC with either
721 --enable-threads or --disable-threads.)
723 <para>When you link a multithreaded application, you will probably
724 need to add a library or flag to g++. This is a very
725 non-standardized area of GCC across ports. Some ports support a
726 special flag (the spelling isn't even standardized yet) to add
727 all required macros to a compilation (if any such flags are
728 required then you must provide the flag for all compilations not
729 just linking) and link-library additions and/or replacements at
730 link time. The documentation is weak. Here is a quick summary
731 to display how ad hoc this is: On Solaris, both -pthreads and
732 -threads (with subtly different meanings) are honored. On OSF,
733 -pthread and -threads (with subtly different meanings) are
734 honored. On Linux/i386, -pthread is honored. On FreeBSD,
735 -pthread is honored. Some other ports use other switches.
736 AFAIK, none of this is properly documented anywhere other than
737 in ``gcc -dumpspecs'' (look at lib and cpp entries).
742 <sect2 id="manual.intro.using.concurrency.thread_safety" xreflabel="Thread Safety">
743 <title>Thread Safety</title>
747 We currently use the <ulink url="http://www.sgi.com/tech/stl/thread_safety.html">SGI STL</ulink> definition of thread safety.
751 <para>The library strives to be thread-safe when all of the following
756 <para>The system's libc is itself thread-safe,
761 The compiler in use reports a thread model other than
762 'single'. This can be tested via output from <code>gcc
763 -v</code>. Multi-thread capable versions of gcc output
768 Using built-in specs.
771 gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
774 <para>Look for "Thread model" lines that aren't equal to "single."</para>
778 Requisite command-line flags are used for atomic operations
779 and threading. Examples of this include <code>-pthread</code>
780 and <code>-march=native</code>, although specifics vary
781 depending on the host environment. See <ulink
782 url="http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html">Machine
783 Dependent Options</ulink>.
788 An implementation of atomicity.h functions
789 exists for the architecture in question. See the internals documentation for more <ulink url="../ext/concurrence.html">details</ulink>.
794 <para>The user-code must guard against concurrent method calls which may
795 access any particular library object's state. Typically, the
796 application programmer may infer what object locks must be held
797 based on the objects referenced in a method call. Without getting
798 into great detail, here is an example which requires user-level
802 library_class_a shared_object_a;
805 library_class_b *object_b = new library_class_b;
806 shared_object_a.add_b (object_b); // must hold lock for shared_object_a
807 shared_object_a.mutate (); // must hold lock for shared_object_a
810 // Multiple copies of thread_main() are started in independent threads.</programlisting>
811 <para>Under the assumption that object_a and object_b are never exposed to
812 another thread, here is an example that should not require any
817 library_class_a object_a;
818 library_class_b *object_b = new library_class_b;
819 object_a.add_b (object_b);
822 <para>All library objects are safe to use in a multithreaded program as
823 long as each thread carefully locks out access by any other
824 thread while it uses any object visible to another thread, i.e.,
825 treat library objects like any other shared resource. In general,
826 this requirement includes both read and write access to objects;
827 unless otherwise documented as safe, do not assume that two threads
828 may access a shared standard library object at the same time.
830 <para>See chapters <ulink url="../17_intro/howto.html#3">17</ulink> (library
831 introduction), <ulink url="../23_containers/howto.html#3">23</ulink>
832 (containers), and <ulink url="../27_io/howto.html#9">27</ulink> (I/O) for
838 <sect2 id="manual.intro.using.concurrency.atomics" xreflabel="Atomics">
839 <title>Atomics</title>
844 <sect2 id="manual.intro.using.concurrency.io" xreflabel="IO">
846 <para>I'll assume that you have already read the
847 <ulink url="../17_intro/howto.html#3">general notes on library threads</ulink>,
849 <ulink url="../23_containers/howto.html#3">notes on threaded container
850 access</ulink> (you might not think of an I/O stream as a container, but
851 the points made there also hold here). If you have not read them,
854 <para>This gets a bit tricky. Please read carefully, and bear with me.
857 <sect3 id="concurrency.io.structure" xreflabel="Structure">
858 <title>Structure</title>
860 type called <code>__basic_file</code> provides our abstraction layer
861 for the <code>std::filebuf</code> classes. Nearly all decisions dealing
862 with actual input and output must be made in <code>__basic_file</code>.
864 <para>A generic locking mechanism is somewhat in place at the filebuf layer,
865 but is not used in the current code. Providing locking at any higher
866 level is akin to providing locking within containers, and is not done
867 for the same reasons (see the links above).
871 <sect3 id="concurrency.io.defaults" xreflabel="Defaults">
872 <title>Defaults</title>
873 <para>The __basic_file type is simply a collection of small wrappers around
874 the C stdio layer (again, see the link under Structure). We do no
875 locking ourselves, but simply pass through to calls to <code>fopen</code>,
876 <code>fwrite</code>, and so forth.
878 <para>So, for 3.0, the question of "is multithreading safe for I/O"
879 must be answered with, "is your platform's C library threadsafe
880 for I/O?" Some are by default, some are not; many offer multiple
881 implementations of the C library with varying tradeoffs of threadsafety
882 and efficiency. You, the programmer, are always required to take care
883 with multiple threads.
885 <para>(As an example, the POSIX standard requires that C stdio FILE*
886 operations are atomic. POSIX-conforming C libraries (e.g, on Solaris
887 and GNU/Linux) have an internal mutex to serialize operations on
888 FILE*s. However, you still need to not do stupid things like calling
889 <code>fclose(fs)</code> in one thread followed by an access of
890 <code>fs</code> in another.)
892 <para>So, if your platform's C library is threadsafe, then your
893 <code>fstream</code> I/O operations will be threadsafe at the lowest
894 level. For higher-level operations, such as manipulating the data
895 contained in the stream formatting classes (e.g., setting up callbacks
896 inside an <code>std::ofstream</code>), you need to guard such accesses
897 like any other critical shared resource.
901 <sect3 id="concurrency.io.future" xreflabel="Future">
902 <title>Future</title>
904 second choice may be available for I/O implementations: libio. This is
905 disabled by default, and in fact will not currently work due to other
906 issues. It will be revisited, however.
908 <para>The libio code is a subset of the guts of the GNU libc (glibc) I/O
909 implementation. When libio is in use, the <code>__basic_file</code>
910 type is basically derived from FILE. (The real situation is more
911 complex than that... it's derived from an internal type used to
912 implement FILE. See libio/libioP.h to see scary things done with
913 vtbls.) The result is that there is no "layer" of C stdio
914 to go through; the filebuf makes calls directly into the same
915 functions used to implement <code>fread</code>, <code>fwrite</code>,
916 and so forth, using internal data structures. (And when I say
917 "makes calls directly," I mean the function is literally
918 replaced by a jump into an internal function. Fast but frightening.
921 <para>Also, the libio internal locks are used. This requires pulling in
922 large chunks of glibc, such as a pthreads implementation, and is one
923 of the issues preventing widespread use of libio as the libstdc++
924 cstdio implementation.
926 <para>But we plan to make this work, at least as an option if not a future
927 default. Platforms running a copy of glibc with a recent-enough
928 version will see calls from libstdc++ directly into the glibc already
929 installed. For other platforms, a copy of the libio subsection will
930 be built and included in libstdc++.
934 <sect3 id="concurrency.io.alt" xreflabel="Alt">
935 <title>Alternatives</title>
936 <para>Don't forget that other cstdio implementations are possible. You could
937 easily write one to perform your own forms of locking, to solve your
938 "interesting" problems.
944 <sect2 id="manual.intro.using.concurrency.containers" xreflabel="Containers">
945 <title>Containers</title>
947 <para>This section discusses issues surrounding the design of
948 multithreaded applications which use Standard C++ containers.
949 All information in this section is current as of the gcc 3.0
950 release and all later point releases. Although earlier gcc
951 releases had a different approach to threading configuration and
952 proper compilation, the basic code design rules presented here
953 were similar. For information on all other aspects of
954 multithreading as it relates to libstdc++, including details on
955 the proper compilation of threaded code (and compatibility between
956 threaded and non-threaded code), see Chapter 17.
958 <para>Two excellent pages to read when working with the Standard C++
959 containers and threads are
960 <ulink url="http://www.sgi.com/tech/stl/thread_safety.html">SGI's
961 http://www.sgi.com/tech/stl/thread_safety.html</ulink> and
962 <ulink url="http://www.sgi.com/tech/stl/Allocators.html">SGI's
963 http://www.sgi.com/tech/stl/Allocators.html</ulink>.
965 <para><emphasis>However, please ignore all discussions about the user-level
966 configuration of the lock implementation inside the STL
967 container-memory allocator on those pages. For the sake of this
968 discussion, libstdc++ configures the SGI STL implementation,
969 not you. This is quite different from how gcc pre-3.0 worked.
970 In particular, past advice was for people using g++ to
971 explicitly define _PTHREADS or other macros or port-specific
972 compilation options on the command line to get a thread-safe
973 STL. This is no longer required for any port and should no
974 longer be done unless you really know what you are doing and
975 assume all responsibility.</emphasis>
977 <para>Since the container implementation of libstdc++ uses the SGI
978 code, we use the same definition of thread safety as SGI when
979 discussing design. A key point that beginners may miss is the
980 fourth major paragraph of the first page mentioned above
981 ("For most clients,"...), which points out that
982 locking must nearly always be done outside the container, by
983 client code (that'd be you, not us). There is a notable
984 exceptions to this rule. Allocators called while a container or
985 element is constructed uses an internal lock obtained and
986 released solely within libstdc++ code (in fact, this is the
987 reason STL requires any knowledge of the thread configuration).
989 <para>For implementing a container which does its own locking, it is
990 trivial to provide a wrapper class which obtains the lock (as
991 SGI suggests), performs the container operation, and then
992 releases the lock. This could be templatized <emphasis>to a certain
993 extent</emphasis>, on the underlying container and/or a locking
994 mechanism. Trying to provide a catch-all general template
995 solution would probably be more trouble than it's worth.
997 <para>The STL implementation is currently configured to use the
998 high-speed caching memory allocator. Some people like to
999 test and/or normally run threaded programs with a different
1000 default. For all details about how to globally override this
1001 at application run-time see <ulink url="../ext/howto.html#3">here</ulink>.
1003 <para>There is a better way (not standardized yet): It is possible to
1004 force the malloc-based allocator on a per-case-basis for some
1005 application code. The library team generally believes that this
1006 is a better way to tune an application for high-speed using this
1007 implementation of the STL. There is
1008 <ulink url="../ext/howto.html#3">more information on allocators here</ulink>.
1014 <sect1 id="manual.intro.using.exception_safety" xreflabel="Exception Safety">
1015 <title>Exception Safety</title>
1019 <!-- Section 0x : Debug -->
1020 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
1021 parse="xml" href="debug.xml">