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