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