xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/doc/xml/manual/using.xml (revision 6cd39ddb8550f6fa1bff3fed32053d7f19fd0453)
1<chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
2	 xml:id="manual.intro.using" xreflabel="Using">
3  <info><title>Using</title></info>
4  <?dbhtml filename="using.html"?>
5
6  <section xml:id="manual.intro.using.flags" xreflabel="Flags"><info><title>Command Options</title></info>
7
8    <para>
9      The set of features available in the GNU C++ library is shaped by
10      several <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Invoking-GCC.html">GCC
11      Command Options</link>. Options that impact libstdc++ are
12      enumerated and detailed in the table below.
13    </para>
14
15    <para>
16      By default, <command>g++</command> is equivalent to  <command>g++ -std=gnu++98</command>. The standard library also defaults to this dialect.
17    </para>
18
19 <table frame="all" xml:id="table.cmd_options">
20<title>C++ Command Options</title>
21
22<tgroup cols="2" align="left" colsep="1" rowsep="1">
23<colspec colname="c1"/>
24<colspec colname="c2"/>
25
26  <thead>
27    <row>
28      <entry>Option Flags</entry>
29      <entry>Description</entry>
30    </row>
31  </thead>
32
33  <tbody>
34    <row>
35      <entry><literal>-std=c++98</literal></entry>
36      <entry>Use the 1998 ISO C++ standard plus amendments.</entry>
37    </row>
38
39    <row>
40      <entry><literal>-std=gnu++98</literal></entry>
41      <entry>As directly above, with GNU extensions.</entry>
42    </row>
43
44    <row>
45      <entry><literal>-std=c++11</literal></entry>
46      <entry>Use the 2011 ISO C++ standard.</entry>
47    </row>
48
49    <row>
50      <entry><literal>-std=gnu++11</literal></entry>
51      <entry>As directly above, with GNU extensions.</entry>
52    </row>
53
54    <row>
55      <entry><literal>-fexceptions</literal></entry>
56      <entry>See <link linkend="intro.using.exception.no">exception-free dialect</link></entry>
57    </row>
58
59    <row>
60      <entry><literal>-frtti</literal></entry>
61      <entry>As above, but RTTI-free dialect.</entry>
62    </row>
63
64    <row>
65      <entry><literal>-pthread</literal> or <literal>-pthreads</literal></entry>
66      <entry>For ISO C++11
67        <filename class="headerfile">&lt;thread&gt;</filename>,
68        <filename class="headerfile">&lt;future&gt;</filename>,
69        <filename class="headerfile">&lt;mutex&gt;</filename>,
70        or <filename class="headerfile">&lt;condition_variable&gt;</filename>.
71      </entry>
72    </row>
73
74    <row>
75      <entry><literal>-latomic</literal></entry>
76      <entry>Linking to <filename class="libraryfile">libatomic</filename>
77        is required for some uses of ISO C++11
78        <filename class="headerfile">&lt;atomic&gt;</filename>.
79      </entry>
80    </row>
81
82    <row>
83      <entry><literal>-fopenmp</literal></entry>
84      <entry>For <link linkend="manual.ext.parallel_mode">parallel</link> mode.</entry>
85    </row>
86  </tbody>
87
88</tgroup>
89</table>
90
91  </section>
92
93  <section xml:id="manual.intro.using.headers" xreflabel="Headers"><info><title>Headers</title></info>
94    <?dbhtml filename="using_headers.html"?>
95
96
97    <section xml:id="manual.intro.using.headers.all" xreflabel="Header Files"><info><title>Header Files</title></info>
98
99
100   <para>
101     The C++ standard specifies the entire set of header files that
102     must be available to all hosted implementations.  Actually, the
103     word "files" is a misnomer, since the contents of the
104     headers don't necessarily have to be in any kind of external
105     file.  The only rule is that when one <code>#include</code>'s a
106     header, the contents of that header become available, no matter
107     how.
108   </para>
109
110   <para>
111   That said, in practice files are used.
112   </para>
113
114   <para>
115     There are two main types of include files: header files related
116     to a specific version of the ISO C++ standard (called Standard
117     Headers), and all others (TR1, C++ ABI, and Extensions).
118   </para>
119
120   <para>
121     Two dialects of standard headers are supported, corresponding to
122     the 1998 standard as updated for 2003, and the current 2011 standard.
123   </para>
124
125   <para>
126     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>.
127   </para>
128
129<table frame="all" xml:id="table.cxx98_headers">
130<title>C++ 1998 Library Headers</title>
131
132<tgroup cols="5" align="left" colsep="1" rowsep="1">
133<colspec colname="c1"/>
134<colspec colname="c2"/>
135<colspec colname="c3"/>
136<colspec colname="c4"/>
137<colspec colname="c5"/>
138<tbody>
139<row>
140<entry><filename class="headerfile">algorithm</filename></entry>
141<entry><filename class="headerfile">bitset</filename></entry>
142<entry><filename class="headerfile">complex</filename></entry>
143<entry><filename class="headerfile">deque</filename></entry>
144<entry><filename class="headerfile">exception</filename></entry>
145</row>
146<row>
147<entry><filename class="headerfile">fstream</filename></entry>
148<entry><filename class="headerfile">functional</filename></entry>
149<entry><filename class="headerfile">iomanip</filename></entry>
150<entry><filename class="headerfile">ios</filename></entry>
151<entry><filename class="headerfile">iosfwd</filename></entry>
152</row>
153<row>
154<entry><filename class="headerfile">iostream</filename></entry>
155<entry><filename class="headerfile">istream</filename></entry>
156<entry><filename class="headerfile">iterator</filename></entry>
157<entry><filename class="headerfile">limits</filename></entry>
158<entry><filename class="headerfile">list</filename></entry>
159</row>
160<row>
161<entry><filename class="headerfile">locale</filename></entry>
162<entry><filename class="headerfile">map</filename></entry>
163<entry><filename class="headerfile">memory</filename></entry>
164<entry><filename class="headerfile">new</filename></entry>
165<entry><filename class="headerfile">numeric</filename></entry>
166</row>
167<row>
168<entry><filename class="headerfile">ostream</filename></entry>
169<entry><filename class="headerfile">queue</filename></entry>
170<entry><filename class="headerfile">set</filename></entry>
171<entry><filename class="headerfile">sstream</filename></entry>
172<entry><filename class="headerfile">stack</filename></entry>
173</row>
174<row>
175<entry><filename class="headerfile">stdexcept</filename></entry>
176<entry><filename class="headerfile">streambuf</filename></entry>
177<entry><filename class="headerfile">string</filename></entry>
178<entry><filename class="headerfile">utility</filename></entry>
179<entry><filename class="headerfile">typeinfo</filename></entry>
180</row>
181<row>
182<entry><filename class="headerfile">valarray</filename></entry>
183<entry><filename class="headerfile">vector</filename></entry>
184</row>
185</tbody>
186</tgroup>
187</table>
188
189<para/>
190<table frame="all" xml:id="table.cxx98_cheaders">
191<title>C++ 1998 Library Headers for C Library Facilities</title>
192
193<tgroup cols="5" align="left" colsep="1" rowsep="1">
194<colspec colname="c1"/>
195<colspec colname="c2"/>
196<colspec colname="c3"/>
197<colspec colname="c4"/>
198<colspec colname="c5"/>
199<tbody>
200<row>
201<entry><filename class="headerfile">cassert</filename></entry>
202<entry><filename class="headerfile">cerrno</filename></entry>
203<entry><filename class="headerfile">cctype</filename></entry>
204<entry><filename class="headerfile">cfloat</filename></entry>
205<entry><filename class="headerfile">ciso646</filename></entry>
206</row>
207<row>
208<entry><filename class="headerfile">climits</filename></entry>
209<entry><filename class="headerfile">clocale</filename></entry>
210<entry><filename class="headerfile">cmath</filename></entry>
211<entry><filename class="headerfile">csetjmp</filename></entry>
212<entry><filename class="headerfile">csignal</filename></entry>
213</row>
214<row>
215<entry><filename class="headerfile">cstdarg</filename></entry>
216<entry><filename class="headerfile">cstddef</filename></entry>
217<entry><filename class="headerfile">cstdio</filename></entry>
218<entry><filename class="headerfile">cstdlib</filename></entry>
219<entry><filename class="headerfile">cstring</filename></entry>
220</row>
221<row>
222<entry><filename class="headerfile">ctime</filename></entry>
223<entry><filename class="headerfile">cwchar</filename></entry>
224<entry><filename class="headerfile">cwctype</filename></entry>
225</row>
226</tbody>
227</tgroup>
228</table>
229
230<para>
231C++11 include files. These are only available in C++11 compilation
232mode, i.e. <literal>-std=c++11</literal> or <literal>-std=gnu++11</literal>.
233</para>
234
235<para/>
236<table frame="all" xml:id="table.cxx11_headers">
237<title>C++ 2011 Library Headers</title>
238
239<tgroup cols="5" align="left" colsep="1" rowsep="1">
240<colspec colname="c1"/>
241<colspec colname="c2"/>
242<colspec colname="c3"/>
243<colspec colname="c4"/>
244<colspec colname="c5"/>
245<tbody>
246
247<row>
248<entry><filename class="headerfile">algorithm</filename></entry>
249<entry><filename class="headerfile">array</filename></entry>
250<entry><filename class="headerfile">bitset</filename></entry>
251<entry><filename class="headerfile">chrono</filename></entry>
252<entry><filename class="headerfile">complex</filename></entry>
253</row>
254<row>
255<entry><filename class="headerfile">condition_variable</filename></entry>
256<entry><filename class="headerfile">deque</filename></entry>
257<entry><filename class="headerfile">exception</filename></entry>
258<entry><filename class="headerfile">forward_list</filename></entry>
259<entry><filename class="headerfile">fstream</filename></entry>
260</row>
261<row>
262<entry><filename class="headerfile">functional</filename></entry>
263<entry><filename class="headerfile">future</filename></entry>
264<entry><filename class="headerfile">initalizer_list</filename></entry>
265<entry><filename class="headerfile">iomanip</filename></entry>
266<entry><filename class="headerfile">ios</filename></entry>
267</row>
268<row>
269<entry><filename class="headerfile">iosfwd</filename></entry>
270<entry><filename class="headerfile">iostream</filename></entry>
271<entry><filename class="headerfile">istream</filename></entry>
272<entry><filename class="headerfile">iterator</filename></entry>
273<entry><filename class="headerfile">limits</filename></entry>
274</row>
275<row>
276<entry><filename class="headerfile">list</filename></entry>
277<entry><filename class="headerfile">locale</filename></entry>
278<entry><filename class="headerfile">map</filename></entry>
279<entry><filename class="headerfile">memory</filename></entry>
280<entry><filename class="headerfile">mutex</filename></entry>
281</row>
282<row>
283<entry><filename class="headerfile">new</filename></entry>
284<entry><filename class="headerfile">numeric</filename></entry>
285<entry><filename class="headerfile">ostream</filename></entry>
286<entry><filename class="headerfile">queue</filename></entry>
287<entry><filename class="headerfile">random</filename></entry>
288</row>
289<row>
290<entry><filename class="headerfile">ratio</filename></entry>
291<entry><filename class="headerfile">regex</filename></entry>
292<entry><filename class="headerfile">set</filename></entry>
293<entry><filename class="headerfile">sstream</filename></entry>
294<entry><filename class="headerfile">stack</filename></entry>
295</row>
296<row>
297<entry><filename class="headerfile">stdexcept</filename></entry>
298<entry><filename class="headerfile">streambuf</filename></entry>
299<entry><filename class="headerfile">string</filename></entry>
300<entry><filename class="headerfile">system_error</filename></entry>
301<entry><filename class="headerfile">thread</filename></entry>
302</row>
303<row>
304<entry><filename class="headerfile">tuple</filename></entry>
305<entry><filename class="headerfile">type_traits</filename></entry>
306<entry><filename class="headerfile">typeinfo</filename></entry>
307<entry><filename class="headerfile">unordered_map</filename></entry>
308<entry><filename class="headerfile">unordered_set</filename></entry>
309</row>
310<row>
311<entry><filename class="headerfile">utility</filename></entry>
312<entry><filename class="headerfile">valarray</filename></entry>
313<entry><filename class="headerfile">vector</filename></entry>
314</row>
315
316</tbody>
317</tgroup>
318</table>
319
320<para/>
321
322<table frame="all" xml:id="table.cxx11_cheaders">
323<title>C++ 2011 Library Headers for C Library Facilities</title>
324
325<tgroup cols="5" align="left" colsep="1" rowsep="1">
326<colspec colname="c1"/>
327<colspec colname="c2"/>
328<colspec colname="c3"/>
329<colspec colname="c4"/>
330<colspec colname="c5"/>
331<tbody>
332<row>
333<entry><filename class="headerfile">cassert</filename></entry>
334<entry><filename class="headerfile">ccomplex</filename></entry>
335<entry><filename class="headerfile">cctype</filename></entry>
336<entry><filename class="headerfile">cerrno</filename></entry>
337<entry><filename class="headerfile">cfenv</filename></entry>
338</row>
339<row>
340<entry><filename class="headerfile">cfloat</filename></entry>
341<entry><filename class="headerfile">cinttypes</filename></entry>
342<entry><filename class="headerfile">ciso646</filename></entry>
343<entry><filename class="headerfile">climits</filename></entry>
344<entry><filename class="headerfile">clocale</filename></entry>
345</row>
346<row>
347<entry><filename class="headerfile">cmath</filename></entry>
348<entry><filename class="headerfile">csetjmp</filename></entry>
349<entry><filename class="headerfile">csignal</filename></entry>
350<entry><filename class="headerfile">cstdarg</filename></entry>
351<entry><filename class="headerfile">cstdbool</filename></entry>
352</row>
353<row>
354<entry><filename class="headerfile">cstddef</filename></entry>
355<entry><filename class="headerfile">cstdint</filename></entry>
356<entry><filename class="headerfile">cstdlib</filename></entry>
357<entry><filename class="headerfile">cstdio</filename></entry>
358<entry><filename class="headerfile">cstring</filename></entry>
359</row>
360<row>
361<entry><filename class="headerfile">ctgmath</filename></entry>
362<entry><filename class="headerfile">ctime</filename></entry>
363<entry><filename class="headerfile">cuchar</filename></entry>
364<entry><filename class="headerfile">cwchar</filename></entry>
365<entry><filename class="headerfile">cwctype</filename></entry>
366</row>
367</tbody>
368</tgroup>
369</table>
370
371
372<para>
373  In addition, TR1 includes as:
374</para>
375
376<table frame="all" xml:id="table.tr1_headers">
377<title>C++ TR 1 Library Headers</title>
378
379<tgroup cols="5" align="left" colsep="1" rowsep="1">
380<colspec colname="c1"/>
381<colspec colname="c2"/>
382<colspec colname="c3"/>
383<colspec colname="c4"/>
384<colspec colname="c5"/>
385<tbody>
386
387<row>
388<entry><filename class="headerfile">tr1/array</filename></entry>
389<entry><filename class="headerfile">tr1/complex</filename></entry>
390<entry><filename class="headerfile">tr1/memory</filename></entry>
391<entry><filename class="headerfile">tr1/functional</filename></entry>
392<entry><filename class="headerfile">tr1/random</filename></entry>
393</row>
394<row>
395<entry><filename class="headerfile">tr1/regex</filename></entry>
396<entry><filename class="headerfile">tr1/tuple</filename></entry>
397<entry><filename class="headerfile">tr1/type_traits</filename></entry>
398<entry><filename class="headerfile">tr1/unordered_map</filename></entry>
399<entry><filename class="headerfile">tr1/unordered_set</filename></entry>
400</row>
401<row>
402<entry><filename class="headerfile">tr1/utility</filename></entry>
403</row>
404
405</tbody>
406</tgroup>
407</table>
408
409<para/>
410
411
412<table frame="all" xml:id="table.tr1_cheaders">
413<title>C++ TR 1 Library Headers for C Library Facilities</title>
414
415<tgroup cols="5" align="left" colsep="1" rowsep="1">
416<colspec colname="c1"/>
417<colspec colname="c2"/>
418<colspec colname="c3"/>
419<colspec colname="c4"/>
420<colspec colname="c5"/>
421<tbody>
422
423<row>
424<entry><filename class="headerfile">tr1/ccomplex</filename></entry>
425<entry><filename class="headerfile">tr1/cfenv</filename></entry>
426<entry><filename class="headerfile">tr1/cfloat</filename></entry>
427<entry><filename class="headerfile">tr1/cmath</filename></entry>
428<entry><filename class="headerfile">tr1/cinttypes</filename></entry>
429</row>
430<row>
431<entry><filename class="headerfile">tr1/climits</filename></entry>
432<entry><filename class="headerfile">tr1/cstdarg</filename></entry>
433<entry><filename class="headerfile">tr1/cstdbool</filename></entry>
434<entry><filename class="headerfile">tr1/cstdint</filename></entry>
435<entry><filename class="headerfile">tr1/cstdio</filename></entry>
436</row>
437<row>
438<entry><filename class="headerfile">tr1/cstdlib</filename></entry>
439<entry><filename class="headerfile">tr1/ctgmath</filename></entry>
440<entry><filename class="headerfile">tr1/ctime</filename></entry>
441<entry><filename class="headerfile">tr1/cwchar</filename></entry>
442<entry><filename class="headerfile">tr1/cwctype</filename></entry>
443</row>
444
445</tbody>
446</tgroup>
447</table>
448
449
450<para>Decimal floating-point arithmetic is available if the C++
451compiler supports scalar decimal floating-point types defined via
452<code>__attribute__((mode(SD|DD|LD)))</code>.
453</para>
454
455<table frame="all" xml:id="table.decfp_headers">
456<title>C++ TR 24733 Decimal Floating-Point Header</title>
457
458<tgroup cols="1" align="left" colsep="1" rowsep="1">
459<colspec colname="c1"/>
460<tbody>
461<row>
462<entry><filename class="headerfile">decimal/decimal</filename></entry>
463</row>
464</tbody>
465</tgroup>
466</table>
467
468<para>
469  Also included are files for the C++ ABI interface:
470</para>
471
472<table frame="all" xml:id="table.abi_headers">
473<title>C++ ABI Headers</title>
474
475<tgroup cols="2" align="left" colsep="1" rowsep="1">
476<colspec colname="c1"/>
477<colspec colname="c2"/>
478<tbody>
479<row><entry><filename class="headerfile">cxxabi.h</filename></entry><entry><filename class="headerfile">cxxabi_forced.h</filename></entry></row>
480</tbody>
481</tgroup>
482</table>
483
484<para>
485  And a large variety of extensions.
486</para>
487
488<table frame="all" xml:id="table.ext_headers">
489<title>Extension Headers</title>
490
491<tgroup cols="5" align="left" colsep="1" rowsep="1">
492<colspec colname="c1"/>
493<colspec colname="c2"/>
494<colspec colname="c3"/>
495<colspec colname="c4"/>
496<colspec colname="c5"/>
497<tbody>
498
499<row>
500<entry><filename class="headerfile">ext/algorithm</filename></entry>
501<entry><filename class="headerfile">ext/atomicity.h</filename></entry>
502<entry><filename class="headerfile">ext/array_allocator.h</filename></entry>
503<entry><filename class="headerfile">ext/bitmap_allocator.h</filename></entry>
504<entry><filename class="headerfile">ext/cast.h</filename></entry>
505</row>
506<row>
507<entry><filename class="headerfile">ext/codecvt_specializations.h</filename></entry>
508<entry><filename class="headerfile">ext/concurrence.h</filename></entry>
509<entry><filename class="headerfile">ext/debug_allocator.h</filename></entry>
510<entry><filename class="headerfile">ext/enc_filebuf.h</filename></entry>
511<entry><filename class="headerfile">ext/extptr_allocator.h</filename></entry>
512</row>
513<row>
514<entry><filename class="headerfile">ext/functional</filename></entry>
515<entry><filename class="headerfile">ext/iterator</filename></entry>
516<entry><filename class="headerfile">ext/malloc_allocator.h</filename></entry>
517<entry><filename class="headerfile">ext/memory</filename></entry>
518<entry><filename class="headerfile">ext/mt_allocator.h</filename></entry>
519</row>
520<row>
521<entry><filename class="headerfile">ext/new_allocator.h</filename></entry>
522<entry><filename class="headerfile">ext/numeric</filename></entry>
523<entry><filename class="headerfile">ext/numeric_traits.h</filename></entry>
524<entry><filename class="headerfile">ext/pb_ds/assoc_container.h</filename></entry>
525<entry><filename class="headerfile">ext/pb_ds/priority_queue.h</filename></entry>
526</row>
527<row>
528<entry><filename class="headerfile">ext/pod_char_traits.h</filename></entry>
529<entry><filename class="headerfile">ext/pool_allocator.h</filename></entry>
530<entry><filename class="headerfile">ext/rb_tree</filename></entry>
531<entry><filename class="headerfile">ext/rope</filename></entry>
532<entry><filename class="headerfile">ext/slist</filename></entry>
533</row>
534<row>
535<entry><filename class="headerfile">ext/stdio_filebuf.h</filename></entry>
536<entry><filename class="headerfile">ext/stdio_sync_filebuf.h</filename></entry>
537<entry><filename class="headerfile">ext/throw_allocator.h</filename></entry>
538<entry><filename class="headerfile">ext/typelist.h</filename></entry>
539<entry><filename class="headerfile">ext/type_traits.h</filename></entry>
540</row>
541<row>
542<entry><filename class="headerfile">ext/vstring.h</filename></entry>
543</row>
544
545</tbody>
546</tgroup>
547</table>
548
549<para/>
550
551<table frame="all" xml:id="table.debug_headers">
552<title>Extension Debug Headers</title>
553
554<tgroup cols="5" align="left" colsep="1" rowsep="1">
555<colspec colname="c1"/>
556<colspec colname="c2"/>
557<colspec colname="c3"/>
558<colspec colname="c4"/>
559<colspec colname="c5"/>
560<tbody>
561
562<row>
563<entry><filename class="headerfile">debug/bitset</filename></entry>
564<entry><filename class="headerfile">debug/deque</filename></entry>
565<entry><filename class="headerfile">debug/list</filename></entry>
566<entry><filename class="headerfile">debug/map</filename></entry>
567<entry><filename class="headerfile">debug/set</filename></entry>
568</row>
569
570<row>
571<entry><filename class="headerfile">debug/string</filename></entry>
572<entry><filename class="headerfile">debug/unordered_map</filename></entry>
573<entry><filename class="headerfile">debug/unordered_set</filename></entry>
574<entry><filename class="headerfile">debug/vector</filename></entry>
575</row>
576
577</tbody>
578</tgroup>
579</table>
580
581<para/>
582
583<table frame="all" xml:id="table.profile_headers">
584<title>Extension Profile Headers</title>
585
586<tgroup cols="4" align="left" colsep="1" rowsep="1">
587<colspec colname="c1"/>
588<colspec colname="c2"/>
589<colspec colname="c3"/>
590<colspec colname="c4"/>
591<tbody>
592
593<row>
594<entry><filename class="headerfile">profile/bitset</filename></entry>
595<entry><filename class="headerfile">profile/deque</filename></entry>
596<entry><filename class="headerfile">profile/list</filename></entry>
597<entry><filename class="headerfile">profile/map</filename></entry>
598</row>
599
600<row>
601<entry><filename class="headerfile">profile/set</filename></entry>
602<entry><filename class="headerfile">profile/unordered_map</filename></entry>
603<entry><filename class="headerfile">profile/unordered_set</filename></entry>
604<entry><filename class="headerfile">profile/vector</filename></entry>
605</row>
606
607</tbody>
608</tgroup>
609</table>
610
611<para/>
612
613<table frame="all" xml:id="table.parallel_headers">
614<title>Extension Parallel Headers</title>
615
616<tgroup cols="2" align="left" colsep="1" rowsep="1">
617<colspec colname="c1"/>
618<colspec colname="c2"/>
619<tbody>
620<row>
621<entry><filename class="headerfile">parallel/algorithm</filename></entry>
622<entry><filename class="headerfile">parallel/numeric</filename></entry>
623</row>
624</tbody>
625</tgroup>
626</table>
627
628    </section>
629
630    <section xml:id="manual.intro.using.headers.mixing" xreflabel="Mixing Headers"><info><title>Mixing Headers</title></info>
631
632
633<para> A few simple rules.
634</para>
635
636<para>First, mixing different dialects of the standard headers is not
637possible. It's an all-or-nothing affair. Thus, code like
638</para>
639
640<programlisting>
641#include &lt;array&gt;
642#include &lt;functional&gt;
643</programlisting>
644
645<para>Implies C++11 mode. To use the entities in &lt;array&gt;, the C++11
646compilation mode must be used, which implies the C++11 functionality
647(and deprecations) in &lt;functional&gt; will be present.
648</para>
649
650<para>Second, the other headers can be included with either dialect of
651the standard headers, although features and types specific to C++11
652are still only enabled when in C++11 compilation mode. So, to use
653rvalue references with <code>__gnu_cxx::vstring</code>, or to use the
654debug-mode versions of <code>std::unordered_map</code>, one must use
655the <code>std=gnu++11</code> compiler flag. (Or <code>std=c++11</code>, of course.)
656</para>
657
658<para>A special case of the second rule is the mixing of TR1 and C++11
659facilities. It is possible (although not especially prudent) to
660include both the TR1 version and the C++11 version of header in the
661same translation unit:
662</para>
663
664<programlisting>
665#include &lt;tr1/type_traits&gt;
666#include &lt;type_traits&gt;
667</programlisting>
668
669<para> Several parts of C++11 diverge quite substantially from TR1 predecessors.
670</para>
671    </section>
672
673    <section xml:id="manual.intro.using.headers.cheaders" xreflabel="C Headers and"><info><title>The C Headers and <code>namespace std</code></title></info>
674
675
676<para>
677	The standard specifies that if one includes the C-style header
678	(&lt;math.h&gt; in this case), the symbols will be available
679	in the global namespace and perhaps in
680	namespace <code>std::</code> (but this is no longer a firm
681	requirement.) On the other hand, including the C++-style
682	header (&lt;cmath&gt;) guarantees that the entities will be
683	found in namespace std and perhaps in the global namespace.
684      </para>
685
686<para>
687Usage of C++-style headers is recommended, as then
688C-linkage names can be disambiguated by explicit qualification, such
689as by <code>std::abort</code>. In addition, the C++-style headers can
690use function overloading to provide a simpler interface to certain
691families of C-functions. For instance in &lt;cmath&gt;, the
692function <code>std::sin</code> has overloads for all the builtin
693floating-point types. This means that <code>std::sin</code> can be
694used uniformly, instead of a combination
695of <code>std::sinf</code>, <code>std::sin</code>,
696and <code>std::sinl</code>.
697</para>
698    </section>
699
700    <section xml:id="manual.intro.using.headers.pre" xreflabel="Precompiled Headers"><info><title>Precompiled Headers</title></info>
701
702
703
704<para>There are three base header files that are provided. They can be
705used to precompile the standard headers and extensions into binary
706files that may the be used to speed compiles that use these headers.
707</para>
708
709
710<itemizedlist>
711<listitem>
712  <para>stdc++.h</para>
713<para>Includes all standard headers. Actual content varies depending on
714language dialect.
715</para>
716</listitem>
717
718<listitem>
719  <para>stdtr1c++.h</para>
720<para>Includes all of &lt;stdc++.h&gt;, and adds all the TR1 headers.
721</para>
722</listitem>
723
724<listitem><para>extc++.h</para>
725<para>Includes all of &lt;stdtr1c++.h&gt;, and adds all the Extension headers.
726</para></listitem>
727</itemizedlist>
728
729<para>How to construct a .gch file from one of these base header files.</para>
730
731<para>First, find the include directory for the compiler. One way to do
732this is:</para>
733
734<programlisting>
735g++ -v hello.cc
736
737#include &lt;...&gt; search starts here:
738 /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
739...
740End of search list.
741</programlisting>
742
743
744<para>Then, create a precompiled header file with the same flags that
745will be used to compile other projects.</para>
746
747<programlisting>
748g++ -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
749</programlisting>
750
751<para>The resulting file will be quite large: the current size is around
752thirty megabytes. </para>
753
754<para>How to use the resulting file.</para>
755
756<programlisting>
757g++ -I. -include stdc++.h  -H -g -O2 hello.cc
758</programlisting>
759
760<para>Verification that the PCH file is being used is easy:</para>
761
762<programlisting>
763g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
764! ./stdc++.h.gch
765. /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
766. /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string
767</programlisting>
768
769<para>The exclamation point to the left of the <code>stdc++.h.gch</code> listing means that the generated PCH file was used.</para>
770<para/>
771
772<para> Detailed information about creating precompiled header files can be found in the GCC <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html">documentation</link>.
773</para>
774
775    </section>
776  </section>
777
778
779  <section xml:id="manual.intro.using.macros" xreflabel="Macros"><info><title>Macros</title></info>
780    <?dbhtml filename="using_macros.html"?>
781
782
783   <para>
784     All library macros begin with <code>_GLIBCXX_</code>.
785   </para>
786
787   <para>
788     Furthermore, all pre-processor macros, switches, and
789      configuration options are gathered in the
790      file <filename class="headerfile">c++config.h</filename>, which
791      is generated during the libstdc++ configuration and build
792      process. This file is then included when needed by files part of
793      the public libstdc++ API, like
794      <filename class="headerfile">&lt;ios&gt;</filename>. Most of these
795      macros should not be used by consumers of libstdc++, and are reserved
796      for internal implementation use. <emphasis>These macros cannot
797      be redefined</emphasis>.
798   </para>
799
800   <para>
801     A select handful of macros control libstdc++ extensions and extra
802      features, or provide versioning information for the API.  Only
803      those macros listed below are offered for consideration by the
804      general public.
805   </para>
806
807   <para>Below is the macro which users may check for library version
808      information. </para>
809
810    <variablelist>
811    <varlistentry>
812      <term><code>__GLIBCXX__</code></term>
813      <listitem>
814	<para>The current version of
815    libstdc++ in compressed ISO date format, as an unsigned
816    long. For details on the value of this particular macro for a
817    particular release, please consult the <link linkend="appendix.porting.abi">
818    ABI Policy and Guidelines</link> appendix.
819    </para>
820    </listitem>
821    </varlistentry>
822    </variablelist>
823
824   <para>Below are the macros which users may change with #define/#undef or
825      with -D/-U compiler flags.  The default state of the symbol is
826      listed.</para>
827
828   <para><quote>Configurable</quote> (or <quote>Not configurable</quote>) means
829      that the symbol is initially chosen (or not) based on
830      --enable/--disable options at library build and configure time
831      (documented in
832      <link linkend="manual.intro.setup.configure">Configure</link>),
833      with the various --enable/--disable choices being translated to
834      #define/#undef).
835   </para>
836
837   <para> <acronym>ABI</acronym> means that changing from the default value may
838  mean changing the <acronym>ABI</acronym> of compiled code. In other words,
839  these choices control code which has already been compiled (i.e., in a
840  binary such as libstdc++.a/.so).  If you explicitly #define or
841  #undef these macros, the <emphasis>headers</emphasis> may see different code
842  paths, but the <emphasis>libraries</emphasis> which you link against will not.
843  Experimenting with different values with the expectation of
844  consistent linkage requires changing the config headers before
845  building/installing the library.
846   </para>
847
848    <variablelist>
849    <varlistentry><term><code>_GLIBCXX_USE_DEPRECATED</code></term>
850    <listitem>
851      <para>
852	Defined by default. Not configurable. ABI-changing. Turning this off
853	removes older ARM-style iostreams code, and other anachronisms
854	from the API.  This macro is dependent on the version of the
855	standard being tracked, and as a result may give different results for
856	<code>-std=c++98</code> and <code>-std=c++11</code>. This may
857	be useful in updating old C++ code which no longer meet the
858	requirements of the language, or for checking current code
859	against new language standards.
860    </para>
861    </listitem></varlistentry>
862
863    <varlistentry><term><code>_GLIBCXX_USE_CXX11_ABI</code></term>
864    <listitem>
865      <para>
866        Defined to the value <literal>1</literal> by default.
867        Configurable via  <code>--disable-libstdcxx-dual-abi</code>
868        and/or <code>--with-default-libstdcxx-abi</code>.
869        ABI-changing.
870        When defined to a non-zero value the library headers will use the
871        new C++11-conforming ABI introduced in GCC 5, rather than the older
872        ABI introduced in GCC 3.4. This changes the definition of several
873        class templates, including <classname>std:string</classname>,
874        <classname>std::list</classname> and some locale facets.
875        For more details see <xref linkend="manual.intro.using.abi"/>.
876    </para>
877    </listitem></varlistentry>
878
879    <varlistentry><term><code>_GLIBCXX_CONCEPT_CHECKS</code></term>
880    <listitem>
881      <para>
882	Undefined by default.  Configurable via
883	<code>--enable-concept-checks</code>.  When defined, performs
884	compile-time checking on certain template instantiations to
885	detect violations of the requirements of the standard.  This
886	is described in more detail in
887	<link linkend="manual.ext.compile_checks">Compile Time Checks</link>.
888      </para>
889    </listitem></varlistentry>
890
891    <varlistentry><term><code>_GLIBCXX_DEBUG</code></term>
892    <listitem>
893      <para>
894	Undefined by default. When defined, compiles user code using
895	the <link linkend="manual.ext.debug_mode">debug mode</link>.
896      </para>
897    </listitem></varlistentry>
898    <varlistentry><term><code>_GLIBCXX_DEBUG_PEDANTIC</code></term>
899    <listitem>
900      <para>
901	Undefined by default. When defined while compiling with
902	the <link linkend="manual.ext.debug_mode">debug mode</link>, makes
903	the debug mode extremely picky by making the use of libstdc++
904	extensions and libstdc++-specific behavior into errors.
905      </para>
906    </listitem></varlistentry>
907    <varlistentry><term><code>_GLIBCXX_PARALLEL</code></term>
908    <listitem>
909      <para>Undefined by default. When defined, compiles user code
910	using the <link linkend="manual.ext.parallel_mode">parallel
911	mode</link>.
912      </para>
913    </listitem></varlistentry>
914
915    <varlistentry><term><code>_GLIBCXX_PROFILE</code></term>
916    <listitem>
917      <para>Undefined by default. When defined, compiles user code
918	using the <link linkend="manual.ext.profile_mode">profile
919	mode</link>.
920      </para>
921    </listitem></varlistentry>
922    </variablelist>
923
924  </section>
925
926<section xml:id="manual.intro.using.abi" xreflabel="Dual ABI">
927  <info><title>Dual ABI</title></info>
928  <?dbhtml filename="using_dual_abi.html"?>
929
930<para> In the GCC 5.1 release libstdc++ introduced a new library ABI that
931  includes new implementations of <classname>std::string</classname> and
932  <classname>std::list</classname>. These changes were necessary to conform
933  to the 2011 C++ standard which forbids Copy-On-Write strings and requires
934  lists to keep track of their size.
935</para>
936
937<para> In order to maintain backwards compatibility for existing code linked
938  to libstdc++ the library's soname has not changed and the old
939  implementations are still supported in parallel with the new ones.
940  This is achieved by defining the new implementations in an inline namespace
941  so they have different names for linkage purposes, e.g. the new version of
942  <classname>std::list&lt;int&gt;</classname> is actually defined as
943  <classname>std::__cxx11::list&lt;int&gt;</classname>. Because the symbols
944  for the new implementations have different names the definitions for both
945  versions can be present in the same library.
946</para>
947
948<para> The <symbol>_GLIBCXX_USE_CXX11_ABI</symbol> macro (see
949<xref linkend="manual.intro.using.macros"/>) controls whether
950  the declarations in the library headers use the old or new ABI.
951  So the decision of which ABI to use can be made separately for each
952  source file being compiled.
953  Using the default configuration options for GCC the default value
954  of the macro is <literal>1</literal> which causes the new ABI to be active,
955  so to use the old ABI you must explicitly define the macro to
956  <literal>0</literal> before including any library headers.
957  (Be aware that some GNU/Linux distributions configure GCC 5 differently so
958  that the default value of the macro is <literal>0</literal> and users must
959  define it to <literal>1</literal> to enable the new ABI.)
960</para>
961
962<para> Although the changes were made for C++11 conformance, the choice of ABI
963  to use is independent of the <option>-std</option> option used to compile
964  your code, i.e. for a given GCC build the default value of the
965  <symbol>_GLIBCXX_USE_CXX11_ABI</symbol> macro is the same for all dialects.
966  This ensures that the <option>-std</option> does not change the ABI, so
967  that it is straightforward to link C++03 and C++11 code together.
968</para>
969
970<para> Because <classname>std::string</classname> is used extensively
971  throughout the library a number of other types are also defined twice,
972  including the stringstream classes and several facets used by
973  <classname>std::locale</classname>. The standard facets which are always
974  installed in a locale may be present twice, with both ABIs, to ensure that
975  code like
976  <code>std::use_facet&lt;std::time_get&lt;char&gt;&gt;(locale);</code>
977  will work correctly for both <classname>std::time_get</classname> and
978  <classname>std::__cxx11::time_get</classname> (even if a user-defined
979  facet that derives from one or other version of
980  <classname>time_get</classname> is installed in the locale).
981</para>
982
983<para> Although the standard exception types defined in
984  <filename class="headerfile">&lt;stdexcept&gt;</filename> use strings, they
985  are not defined twice, so that a <classname>std::out_of_range</classname>
986  exception thrown in one file can always be caught by a suitable handler in
987  another file, even if the two files are compiled with different ABIs.
988</para>
989
990<section xml:id="manual.intro.using.abi.trouble" xreflabel="Dual ABI Troubleshooting"><info><title>Troubleshooting</title></info>
991
992<para> If you get linker errors about undefined references to symbols
993  that involve types in the <code>std::__cxx11</code> namespace or the tag
994  <code>[abi:cxx11]</code> then it probably indicates that you are trying to
995  link together object files that were compiled with different values for the
996  <symbol>_GLIBCXX_USE_CXX11_ABI</symbol> macro. This commonly happens when
997  linking to a third-party library that was compiled with an older version
998  of GCC. If the third-party library cannot be rebuilt with the new ABI then
999  you will need to recompile your code with the old ABI.
1000</para>
1001
1002<para> Not all uses of the new ABI will cause changes in symbol names, for
1003  example a class with a <classname>std::string</classname> member variable
1004  will have the same mangled name whether compiled with the old or new ABI.
1005  In order to detect such problems the new types and functions are
1006  annotated with the <property>abi_tag</property> attribute, allowing the
1007  compiler to warn about potential ABI incompatibilities in code using them.
1008  Those warnings can be enabled with the <option>-Wabi-tag</option> option.
1009</para>
1010
1011</section>
1012</section>
1013
1014  <section xml:id="manual.intro.using.namespaces" xreflabel="Namespaces"><info><title>Namespaces</title></info>
1015    <?dbhtml filename="using_namespaces.html"?>
1016
1017
1018    <section xml:id="manual.intro.using.namespaces.all" xreflabel="Available Namespaces"><info><title>Available Namespaces</title></info>
1019
1020
1021
1022
1023<para> There are three main namespaces.
1024</para>
1025
1026<itemizedlist>
1027  <listitem><para>std</para>
1028<para>The ISO C++ standards specify that "all library entities are defined
1029within namespace std." This includes namespaces nested
1030within <code>namespace std</code>, such as <code>namespace
1031std::tr1</code>.
1032</para>
1033</listitem>
1034<listitem><para>abi</para>
1035<para>Specified by the C++ ABI. This ABI specifies a number of type and
1036function APIs supplemental to those required by the ISO C++ Standard,
1037but necessary for interoperability.
1038</para>
1039</listitem>
1040
1041<listitem><para>__gnu_</para>
1042<para>Indicating one of several GNU extensions. Choices
1043include <code>__gnu_cxx</code>, <code>__gnu_debug</code>, <code>__gnu_parallel</code>,
1044and <code>__gnu_pbds</code>.
1045</para></listitem>
1046</itemizedlist>
1047
1048<para> The library uses a number of inline namespaces as implementation
1049details that are not intended for users to refer to directly, these include
1050<code>std::__detail</code>, <code>std::__cxx11</code> and <code>std::_V2</code>.
1051</para>
1052
1053<para> A complete list of implementation namespaces (including namespace contents) is available in the generated source <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaces.html">documentation</link>.
1054</para>
1055
1056
1057    </section>
1058
1059    <section xml:id="manual.intro.using.namespaces.std" xreflabel="namespace std"><info><title>namespace std</title></info>
1060
1061
1062
1063<para>
1064      One standard requirement is that the library components are defined
1065      in <code>namespace std::</code>. Thus, in order to use these types or
1066      functions, one must do one of two things:
1067</para>
1068
1069<itemizedlist>
1070  <listitem><para>put a kind of <emphasis>using-declaration</emphasis> in your source
1071(either <code>using namespace std;</code> or i.e. <code>using
1072std::string;</code>) This approach works well for individual source files, but
1073should not be used in a global context, like header files.
1074	  </para></listitem> <listitem><para>use a <emphasis>fully
1075qualified name</emphasis> for each library symbol
1076(i.e. <code>std::string</code>, <code>std::cout</code>) Always can be
1077used, and usually enhanced, by strategic use of typedefs. (In the
1078cases where the qualified verbiage becomes unwieldy.)
1079	  </para>
1080	</listitem>
1081</itemizedlist>
1082
1083    </section>
1084
1085    <section xml:id="manual.intro.using.namespaces.comp" xreflabel="Using Namespace Composition"><info><title>Using Namespace Composition</title></info>
1086
1087
1088<para>
1089Best practice in programming suggests sequestering new data or
1090functionality in a sanely-named, unique namespace whenever
1091possible. This is considered an advantage over dumping everything in
1092the global namespace, as then name look-up can be explicitly enabled or
1093disabled as above, symbols are consistently mangled without repetitive
1094naming prefixes or macros, etc.
1095</para>
1096
1097<para>For instance, consider a project that defines most of its classes in <code>namespace gtk</code>. It is possible to
1098	adapt <code>namespace gtk</code> to <code>namespace std</code> by using a C++-feature called
1099	<emphasis>namespace composition</emphasis>. This is what happens if
1100	a <emphasis>using</emphasis>-declaration is put into a
1101	namespace-definition: the imported symbol(s) gets imported into the
1102	currently active namespace(s). For example:
1103</para>
1104<programlisting>
1105namespace gtk
1106{
1107  using std::string;
1108  using std::tr1::array;
1109
1110  class Window { ... };
1111}
1112</programlisting>
1113<para>
1114	In this example, <code>std::string</code> gets imported into
1115	<code>namespace gtk</code>.  The result is that use of
1116	<code>std::string</code> inside namespace gtk can just use <code>string</code>, without the explicit qualification.
1117	As an added bonus,
1118	<code>std::string</code> does not get imported into
1119	the global namespace.  Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
1120	<code>using</code>-declarations can wrapped in macros that
1121	are set based on autoconf-tests to either "" or i.e. <code>using
1122	  std::string;</code> (depending on whether the system has
1123	libstdc++ in <code>std::</code> or not).  (ideas from
1124	Llewelly and Karl Nelson)
1125</para>
1126
1127
1128    </section>
1129  </section>
1130
1131  <section xml:id="manual.intro.using.linkage" xreflabel="Linkage"><info><title>Linking</title></info>
1132    <?dbhtml filename="using_dynamic_or_shared.html"?>
1133
1134
1135    <section xml:id="manual.intro.using.linkage.freestanding" xreflabel="Freestanding"><info><title>Almost Nothing</title></info>
1136
1137      <para>
1138	Or as close as it gets: freestanding. This is a minimal
1139	configuration, with only partial support for the standard
1140	library. Assume only the following header files can be used:
1141      </para>
1142
1143      <itemizedlist>
1144	<listitem>
1145	  <para>
1146	    <filename class="headerfile">cstdarg</filename>
1147	  </para>
1148	</listitem>
1149
1150	<listitem>
1151	  <para>
1152	  <filename class="headerfile">cstddef</filename>
1153	  </para>
1154	</listitem>
1155
1156	<listitem>
1157	  <para>
1158	  <filename class="headerfile">cstdlib</filename>
1159	  </para>
1160	</listitem>
1161
1162	<listitem>
1163	  <para>
1164	  <filename class="headerfile">exception</filename>
1165	  </para>
1166	</listitem>
1167
1168	<listitem>
1169	  <para>
1170	  <filename class="headerfile">limits</filename>
1171	  </para>
1172	</listitem>
1173
1174	<listitem>
1175	  <para>
1176	  <filename class="headerfile">new</filename>
1177	  </para>
1178	</listitem>
1179
1180	<listitem>
1181	  <para>
1182	  <filename class="headerfile">exception</filename>
1183	  </para>
1184	</listitem>
1185
1186	<listitem>
1187	  <para>
1188	  <filename class="headerfile">typeinfo</filename>
1189	  </para>
1190	</listitem>
1191      </itemizedlist>
1192
1193      <para>
1194	In addition, throw in
1195      </para>
1196
1197      <itemizedlist>
1198	<listitem>
1199	  <para>
1200	  <filename class="headerfile">cxxabi.h</filename>.
1201	  </para>
1202	</listitem>
1203      </itemizedlist>
1204
1205      <para>
1206	In the
1207	C++11 <link linkend="manual.intro.using.flags">dialect</link> add
1208      </para>
1209
1210      <itemizedlist>
1211	<listitem>
1212	  <para>
1213	  <filename class="headerfile">initializer_list</filename>
1214	  </para>
1215	</listitem>
1216	<listitem>
1217	  <para>
1218	  <filename class="headerfile">type_traits</filename>
1219	  </para>
1220	</listitem>
1221      </itemizedlist>
1222
1223      <para> There exists a library that offers runtime support for
1224	just these headers, and it is called
1225	<filename class="libraryfile">libsupc++.a</filename>. To use it, compile with <command>gcc</command> instead of <command>g++</command>, like so:
1226      </para>
1227
1228      <para>
1229	<command>gcc foo.cc -lsupc++</command>
1230      </para>
1231
1232      <para>
1233	No attempt is made to verify that only the minimal subset
1234	identified above is actually used at compile time. Violations
1235	are diagnosed as undefined symbols at link time.
1236      </para>
1237    </section>
1238
1239    <section xml:id="manual.intro.using.linkage.dynamic" xreflabel="Dynamic and Shared"><info><title>Finding Dynamic or Shared Libraries</title></info>
1240
1241
1242    <para>
1243      If the only library built is the static library
1244      (<filename class="libraryfile">libstdc++.a</filename>), or if
1245      specifying static linking, this section is can be skipped.  But
1246      if building or using a shared library
1247      (<filename class="libraryfile">libstdc++.so</filename>), then
1248      additional location information will need to be provided.
1249    </para>
1250    <para>
1251      But how?
1252    </para>
1253    <para>
1254A quick read of the relevant part of the GCC
1255      manual, <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b">Compiling
1256      C++ Programs</link>, specifies linking against a C++
1257      library. More details from the
1258      GCC <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/faq.html#rpath">FAQ</link>,
1259      which states <emphasis>GCC does not, by default, specify a
1260      location so that the dynamic linker can find dynamic libraries at
1261      runtime.</emphasis>
1262    </para>
1263    <para>
1264      Users will have to provide this information.
1265    </para>
1266    <para>
1267      Methods vary for different platforms and different styles, and
1268      are printed to the screen during installation. To summarize:
1269    </para>
1270    <itemizedlist>
1271      <listitem>
1272	<para>
1273	  At runtime set <literal>LD_LIBRARY_PATH</literal> in your
1274	  environment correctly, so that the shared library for
1275	  libstdc++ can be found and loaded.  Be certain that you
1276	  understand all of the other implications and behavior
1277	  of <literal>LD_LIBRARY_PATH</literal> first.
1278	</para>
1279
1280      </listitem>
1281      <listitem>
1282	<para>
1283	  Compile the path to find the library at runtime into the
1284	  program.  This can be done by passing certain options to
1285	  <command>g++</command>, which will in turn pass them on to
1286	  the linker.  The exact format of the options is dependent on
1287	  which linker you use:
1288	</para>
1289	<itemizedlist>
1290	  <listitem>
1291	    <para>
1292	      GNU ld (default on GNU/Linux):
1293              <literal>-Wl,-rpath,</literal><filename class="directory">destdir/lib</filename>
1294	    </para>
1295	  </listitem>
1296	  <listitem>
1297	  <para>
1298	    Solaris ld:
1299            <literal>-Wl,-R</literal><filename class="directory">destdir/lib</filename>
1300	  </para>
1301	  </listitem>
1302	</itemizedlist>
1303      </listitem>
1304      <listitem>
1305	<para>
1306	  Some linkers allow you to specify the path to the library by
1307	  setting <literal>LD_RUN_PATH</literal> in your environment
1308	  when linking.
1309	</para>
1310      </listitem>
1311      <listitem>
1312	<para>
1313	  On some platforms the system administrator can configure the
1314	  dynamic linker to always look for libraries in
1315	  <filename class="directory">destdir/lib</filename>, for example
1316	  by using the <command>ldconfig</command> utility on GNU/Linux
1317	  or the <command>crle</command> utility on Solaris. This is a
1318	  system-wide change which can make the system unusable so if you
1319	  are unsure then use one of the other methods described above.
1320	</para>
1321      </listitem>
1322    </itemizedlist>
1323    <para>
1324      Use the <command>ldd</command> utility on the linked executable
1325      to show
1326      which <filename class="libraryfile">libstdc++.so</filename>
1327      library the system will get at runtime.
1328    </para>
1329    <para>
1330      A <filename class="libraryfile">libstdc++.la</filename> file is
1331      also installed, for use with Libtool.  If you use Libtool to
1332      create your executables, these details are taken care of for
1333      you.
1334    </para>
1335    </section>
1336  </section>
1337
1338
1339  <section xml:id="manual.intro.using.concurrency" xreflabel="Concurrency"><info><title>Concurrency</title></info>
1340    <?dbhtml filename="using_concurrency.html"?>
1341
1342
1343   <para>This section discusses issues surrounding the proper compilation
1344      of multithreaded applications which use the Standard C++
1345      library.  This information is GCC-specific since the C++
1346      standard does not address matters of multithreaded applications.
1347   </para>
1348
1349    <section xml:id="manual.intro.using.concurrency.prereq" xreflabel="Thread Prereq"><info><title>Prerequisites</title></info>
1350
1351
1352   <para>All normal disclaimers aside, multithreaded C++ application are
1353      only supported when libstdc++ and all user code was built with
1354      compilers which report (via <code> gcc/g++ -v </code>) the same thread
1355      model and that model is not <emphasis>single</emphasis>.  As long as your
1356      final application is actually single-threaded, then it should be
1357      safe to mix user code built with a thread model of
1358      <emphasis>single</emphasis> with a libstdc++ and other C++ libraries built
1359      with another thread model useful on the platform.  Other mixes
1360      may or may not work but are not considered supported.  (Thus, if
1361      you distribute a shared C++ library in binary form only, it may
1362      be best to compile it with a GCC configured with
1363      --enable-threads for maximal interchangeability and usefulness
1364      with a user population that may have built GCC with either
1365      --enable-threads or --disable-threads.)
1366   </para>
1367   <para>When you link a multithreaded application, you will probably
1368      need to add a library or flag to g++.  This is a very
1369      non-standardized area of GCC across ports.  Some ports support a
1370      special flag (the spelling isn't even standardized yet) to add
1371      all required macros to a compilation (if any such flags are
1372      required then you must provide the flag for all compilations not
1373      just linking) and link-library additions and/or replacements at
1374      link time.  The documentation is weak.  On several targets (including
1375      GNU/Linux, Solaris and various BSDs) -pthread is honored.
1376      Some other ports use other switches.
1377      This is not well documented anywhere other than
1378      in "gcc -dumpspecs" (look at the 'lib' and 'cpp' entries).
1379   </para>
1380
1381   <para>
1382     Some uses of <classname>std::atomic</classname> also require linking
1383     to <filename class="libraryfile">libatomic</filename>.
1384   </para>
1385
1386    </section>
1387
1388    <section xml:id="manual.intro.using.concurrency.thread_safety" xreflabel="Thread Safety"><info><title>Thread Safety</title></info>
1389
1390
1391<para>
1392In the terms of the 2011 C++ standard a thread-safe program is one which
1393does not perform any conflicting non-atomic operations on memory locations
1394and so does not contain any data races.
1395The standard places requirements on the library to ensure that no data
1396races are caused by the library itself or by programs which use the
1397library correctly (as described below).
1398The C++11 memory model and library requirements are a more formal version
1399of the <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.sgi.com/tech/stl/thread_safety.html">SGI STL</link> definition of thread safety, which the library used
1400prior to the 2011 standard.
1401</para>
1402
1403
1404      <para>The library strives to be thread-safe when all of the following
1405	 conditions are met:
1406      </para>
1407      <itemizedlist>
1408       <listitem>
1409       <para>The system's libc is itself thread-safe,
1410       </para>
1411       </listitem>
1412       <listitem>
1413	 <para>
1414	   The compiler in use reports a thread model other than
1415	   'single'. This can be tested via output from <code>gcc
1416	   -v</code>. Multi-thread capable versions of gcc output
1417	   something like this:
1418	 </para>
1419<programlisting>
1420%gcc -v
1421Using built-in specs.
1422...
1423Thread model: posix
1424gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
1425</programlisting>
1426
1427<para>Look for "Thread model" lines that aren't equal to "single."</para>
1428       </listitem>
1429       <listitem>
1430       <para>
1431	 Requisite command-line flags are used for atomic operations
1432	 and threading. Examples of this include <code>-pthread</code>
1433	 and <code>-march=native</code>, although specifics vary
1434	 depending on the host environment. See
1435	 <link linkend="manual.intro.using.flags">Command Options</link> and
1436	 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html">Machine
1437	 Dependent Options</link>.
1438       </para>
1439       </listitem>
1440       <listitem>
1441	 <para>
1442	   An implementation of the
1443	   <filename class="headerfile">atomicity.h</filename> functions
1444	   exists for the architecture in question. See the
1445	   <link linkend="internals.thread_safety">internals
1446	   documentation</link> for more details.
1447       </para>
1448       </listitem>
1449
1450      </itemizedlist>
1451
1452      <para>The user code must guard against concurrent function calls which
1453         access any particular library object's state when one or more of
1454         those accesses modifies the state. An object will be modified by
1455         invoking a non-const member function on it or passing it as a
1456         non-const argument to a library function. An object will not be
1457         modified by invoking a const member function on it or passing it to
1458         a function as a pointer- or reference-to-const.
1459         Typically, the application
1460         programmer may infer what object locks must be held based on the
1461         objects referenced in a function call and whether the objects are
1462         accessed as const or non-const.  Without getting
1463	 into great detail, here is an example which requires user-level
1464	 locks:
1465      </para>
1466      <programlisting>
1467     library_class_a shared_object_a;
1468
1469     void thread_main () {
1470       library_class_b *object_b = new library_class_b;
1471       shared_object_a.add_b (object_b);   // must hold lock for shared_object_a
1472       shared_object_a.mutate ();          // must hold lock for shared_object_a
1473     }
1474
1475     // Multiple copies of thread_main() are started in independent threads.</programlisting>
1476      <para>Under the assumption that object_a and object_b are never exposed to
1477	 another thread, here is an example that does not require any
1478	 user-level locks:
1479      </para>
1480      <programlisting>
1481     void thread_main () {
1482       library_class_a object_a;
1483       library_class_b *object_b = new library_class_b;
1484       object_a.add_b (object_b);
1485       object_a.mutate ();
1486     } </programlisting>
1487
1488      <para>All library types are safe to use in a multithreaded program
1489         if objects are not shared between threads or as
1490	 long each thread carefully locks out access by any other
1491	 thread while it modifies any object visible to another thread.
1492	 Unless otherwise documented, the only exceptions to these rules
1493         are atomic operations on the types in
1494         <filename class="headerfile">&lt;atomic&gt;</filename>
1495         and lock/unlock operations on the standard mutex types in
1496         <filename class="headerfile">&lt;mutex&gt;</filename>. These
1497         atomic operations allow concurrent accesses to the same object
1498         without introducing data races.
1499      </para>
1500
1501      <para>The following member functions of standard containers can be
1502         considered to be const for the purposes of avoiding data races:
1503         <code>begin</code>, <code>end</code>, <code>rbegin</code>, <code>rend</code>,
1504         <code>front</code>, <code>back</code>, <code>data</code>,
1505         <code>find</code>, <code>lower_bound</code>, <code>upper_bound</code>,
1506         <code>equal_range</code>, <code>at</code>
1507         and, except in associative or unordered associative containers,
1508         <code>operator[]</code>. In other words, although they are non-const
1509         so that they can return mutable iterators, those member functions
1510         will not modify the container.
1511         Accessing an iterator might cause a non-modifying access to
1512         the container the iterator refers to (for example incrementing a
1513         list iterator must access the pointers between nodes, which are part
1514         of the container and so conflict with other accesses to the container).
1515      </para>
1516
1517      <para>Programs which follow the rules above will not encounter data
1518         races in library code, even when using library types which share
1519         state between distinct objects.  In the example below the
1520         <code>shared_ptr</code> objects share a reference count, but
1521         because the code does not perform any non-const operations on the
1522         globally-visible object, the library ensures that the reference
1523         count updates are atomic and do not introduce data races:
1524      </para>
1525      <programlisting>
1526    std::shared_ptr&lt;int&gt; global_sp;
1527
1528    void thread_main() {
1529      auto local_sp = global_sp;  // OK, copy constructor's parameter is reference-to-const
1530
1531      int i = *global_sp;         // OK, operator* is const
1532      int j = *local_sp;          // OK, does not operate on global_sp
1533
1534      // *global_sp = 2;          // NOT OK, modifies int visible to other threads
1535      // *local_sp = 2;           // NOT OK, modifies int visible to other threads
1536
1537      // global_sp.reset();       // NOT OK, reset is non-const
1538      local_sp.reset();           // OK, does not operate on global_sp
1539    }
1540
1541    int main() {
1542      global_sp.reset(new int(1));
1543      std::thread t1(thread_main);
1544      std::thread t2(thread_main);
1545      t1.join();
1546      t2.join();
1547    }
1548      </programlisting>
1549
1550      <para>For further details of the C++11 memory model see Hans-J. Boehm's
1551      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/user-faq.html">Threads
1552      and memory model for C++</link> pages, particularly the <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/threadsintro.html">introduction</link>
1553      and <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/user-faq.html">FAQ</link>.
1554      </para>
1555
1556  </section>
1557  <section xml:id="manual.intro.using.concurrency.atomics" xreflabel="Atomics"><info><title>Atomics</title></info>
1558
1559    <para>
1560    </para>
1561  </section>
1562
1563    <section xml:id="manual.intro.using.concurrency.io" xreflabel="IO"><info><title>IO</title></info>
1564
1565     <para>This gets a bit tricky.  Please read carefully, and bear with me.
1566   </para>
1567
1568    <section xml:id="concurrency.io.structure" xreflabel="Structure"><info><title>Structure</title></info>
1569
1570   <para>A wrapper
1571      type called <code>__basic_file</code> provides our abstraction layer
1572      for the <code>std::filebuf</code> classes.  Nearly all decisions dealing
1573      with actual input and output must be made in <code>__basic_file</code>.
1574   </para>
1575   <para>A generic locking mechanism is somewhat in place at the filebuf layer,
1576      but is not used in the current code.  Providing locking at any higher
1577      level is akin to providing locking within containers, and is not done
1578      for the same reasons (see the links above).
1579   </para>
1580    </section>
1581
1582    <section xml:id="concurrency.io.defaults" xreflabel="Defaults"><info><title>Defaults</title></info>
1583
1584   <para>The __basic_file type is simply a collection of small wrappers around
1585      the C stdio layer (again, see the link under Structure).  We do no
1586      locking ourselves, but simply pass through to calls to <code>fopen</code>,
1587      <code>fwrite</code>, and so forth.
1588   </para>
1589   <para>So, for 3.0, the question of "is multithreading safe for I/O"
1590      must be answered with, "is your platform's C library threadsafe
1591      for I/O?"  Some are by default, some are not; many offer multiple
1592      implementations of the C library with varying tradeoffs of threadsafety
1593      and efficiency.  You, the programmer, are always required to take care
1594      with multiple threads.
1595   </para>
1596   <para>(As an example, the POSIX standard requires that C stdio FILE*
1597       operations are atomic.  POSIX-conforming C libraries (e.g, on Solaris
1598       and GNU/Linux) have an internal mutex to serialize operations on
1599       FILE*s.  However, you still need to not do stupid things like calling
1600       <code>fclose(fs)</code> in one thread followed by an access of
1601       <code>fs</code> in another.)
1602   </para>
1603   <para>So, if your platform's C library is threadsafe, then your
1604      <code>fstream</code> I/O operations will be threadsafe at the lowest
1605      level.  For higher-level operations, such as manipulating the data
1606      contained in the stream formatting classes (e.g., setting up callbacks
1607      inside an <code>std::ofstream</code>), you need to guard such accesses
1608      like any other critical shared resource.
1609   </para>
1610    </section>
1611
1612    <section xml:id="concurrency.io.future" xreflabel="Future"><info><title>Future</title></info>
1613
1614   <para> A
1615      second choice may be available for I/O implementations:  libio.  This is
1616      disabled by default, and in fact will not currently work due to other
1617      issues.  It will be revisited, however.
1618   </para>
1619   <para>The libio code is a subset of the guts of the GNU libc (glibc) I/O
1620      implementation.  When libio is in use, the <code>__basic_file</code>
1621      type is basically derived from FILE.  (The real situation is more
1622      complex than that... it's derived from an internal type used to
1623      implement FILE.  See libio/libioP.h to see scary things done with
1624      vtbls.)  The result is that there is no "layer" of C stdio
1625      to go through; the filebuf makes calls directly into the same
1626      functions used to implement <code>fread</code>, <code>fwrite</code>,
1627      and so forth, using internal data structures.  (And when I say
1628      "makes calls directly," I mean the function is literally
1629      replaced by a jump into an internal function.  Fast but frightening.
1630      *grin*)
1631   </para>
1632   <para>Also, the libio internal locks are used.  This requires pulling in
1633      large chunks of glibc, such as a pthreads implementation, and is one
1634      of the issues preventing widespread use of libio as the libstdc++
1635      cstdio implementation.
1636   </para>
1637   <para>But we plan to make this work, at least as an option if not a future
1638      default.  Platforms running a copy of glibc with a recent-enough
1639      version will see calls from libstdc++ directly into the glibc already
1640      installed.  For other platforms, a copy of the libio subsection will
1641      be built and included in libstdc++.
1642   </para>
1643    </section>
1644
1645    <section xml:id="concurrency.io.alt" xreflabel="Alt"><info><title>Alternatives</title></info>
1646
1647   <para>Don't forget that other cstdio implementations are possible.  You could
1648      easily write one to perform your own forms of locking, to solve your
1649      "interesting" problems.
1650   </para>
1651    </section>
1652
1653    </section>
1654
1655    <section xml:id="manual.intro.using.concurrency.containers" xreflabel="Containers"><info><title>Containers</title></info>
1656
1657
1658   <para>This section discusses issues surrounding the design of
1659      multithreaded applications which use Standard C++ containers.
1660      All information in this section is current as of the gcc 3.0
1661      release and all later point releases.  Although earlier gcc
1662      releases had a different approach to threading configuration and
1663      proper compilation, the basic code design rules presented here
1664      were similar.  For information on all other aspects of
1665      multithreading as it relates to libstdc++, including details on
1666      the proper compilation of threaded code (and compatibility between
1667      threaded and non-threaded code), see Chapter 17.
1668   </para>
1669   <para>Two excellent pages to read when working with the Standard C++
1670      containers and threads are
1671      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.sgi.com/tech/stl/thread_safety.html">SGI's
1672      http://www.sgi.com/tech/stl/thread_safety.html</link> and
1673      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.sgi.com/tech/stl/Allocators.html">SGI's
1674      http://www.sgi.com/tech/stl/Allocators.html</link>.
1675   </para>
1676   <para><emphasis>However, please ignore all discussions about the user-level
1677      configuration of the lock implementation inside the STL
1678      container-memory allocator on those pages.  For the sake of this
1679      discussion, libstdc++ configures the SGI STL implementation,
1680      not you.  This is quite different from how gcc pre-3.0 worked.
1681      In particular, past advice was for people using g++ to
1682      explicitly define _PTHREADS or other macros or port-specific
1683      compilation options on the command line to get a thread-safe
1684      STL.  This is no longer required for any port and should no
1685      longer be done unless you really know what you are doing and
1686      assume all responsibility.</emphasis>
1687   </para>
1688   <para>Since the container implementation of libstdc++ uses the SGI
1689      code, we use the same definition of thread safety as SGI when
1690      discussing design.  A key point that beginners may miss is the
1691      fourth major paragraph of the first page mentioned above
1692      (<emphasis>For most clients...</emphasis>), which points out that
1693      locking must nearly always be done outside the container, by
1694      client code (that'd be you, not us).  There is a notable
1695      exceptions to this rule.  Allocators called while a container or
1696      element is constructed uses an internal lock obtained and
1697      released solely within libstdc++ code (in fact, this is the
1698      reason STL requires any knowledge of the thread configuration).
1699   </para>
1700   <para>For implementing a container which does its own locking, it is
1701      trivial to provide a wrapper class which obtains the lock (as
1702      SGI suggests), performs the container operation, and then
1703      releases the lock.  This could be templatized <emphasis>to a certain
1704      extent</emphasis>, on the underlying container and/or a locking
1705      mechanism.  Trying to provide a catch-all general template
1706      solution would probably be more trouble than it's worth.
1707   </para>
1708   <para>The library implementation may be configured to use the
1709      high-speed caching memory allocator, which complicates thread
1710      safety issues. For all details about how to globally override
1711      this at application run-time
1712      see <link linkend="manual.intro.using.macros">here</link>. Also
1713      useful are details
1714      on <link linkend="std.util.memory.allocator">allocator</link>
1715      options and capabilities.
1716   </para>
1717
1718    </section>
1719</section>
1720
1721<!-- Section 0x : Exception policies, expectations, topics -->
1722<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="xml" href="using_exceptions.xml">
1723</xi:include>
1724
1725<!-- Section 0x : Debug -->
1726<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="xml" href="debug.xml">
1727</xi:include>
1728
1729</chapter>
1730