Home | History | Annotate | Line # | Download | only in docs
      1  1.1  joerg ============
      2  1.1  joerg Using libc++
      3  1.1  joerg ============
      4  1.1  joerg 
      5  1.1  joerg .. contents::
      6  1.1  joerg   :local:
      7  1.1  joerg 
      8  1.1  joerg Getting Started
      9  1.1  joerg ===============
     10  1.1  joerg 
     11  1.1  joerg If you already have libc++ installed you can use it with clang.
     12  1.1  joerg 
     13  1.1  joerg .. code-block:: bash
     14  1.1  joerg 
     15  1.1  joerg     $ clang++ -stdlib=libc++ test.cpp
     16  1.1  joerg     $ clang++ -std=c++11 -stdlib=libc++ test.cpp
     17  1.1  joerg 
     18  1.1  joerg On macOS and FreeBSD libc++ is the default standard library
     19  1.1  joerg and the ``-stdlib=libc++`` is not required.
     20  1.1  joerg 
     21  1.1  joerg .. _alternate libcxx:
     22  1.1  joerg 
     23  1.1  joerg If you want to select an alternate installation of libc++ you
     24  1.1  joerg can use the following options.
     25  1.1  joerg 
     26  1.1  joerg .. code-block:: bash
     27  1.1  joerg 
     28  1.1  joerg   $ clang++ -std=c++11 -stdlib=libc++ -nostdinc++ \
     29  1.1  joerg             -I<libcxx-install-prefix>/include/c++/v1 \
     30  1.1  joerg             -L<libcxx-install-prefix>/lib \
     31  1.1  joerg             -Wl,-rpath,<libcxx-install-prefix>/lib \
     32  1.1  joerg             test.cpp
     33  1.1  joerg 
     34  1.1  joerg The option ``-Wl,-rpath,<libcxx-install-prefix>/lib`` adds a runtime library
     35  1.1  joerg search path. Meaning that the systems dynamic linker will look for libc++ in
     36  1.1  joerg ``<libcxx-install-prefix>/lib`` whenever the program is run. Alternatively the
     37  1.1  joerg environment variable ``LD_LIBRARY_PATH`` (``DYLD_LIBRARY_PATH`` on macOS) can
     38  1.1  joerg be used to change the dynamic linkers search paths after a program is compiled.
     39  1.1  joerg 
     40  1.1  joerg An example of using ``LD_LIBRARY_PATH``:
     41  1.1  joerg 
     42  1.1  joerg .. code-block:: bash
     43  1.1  joerg 
     44  1.1  joerg   $ clang++ -stdlib=libc++ -nostdinc++ \
     45  1.1  joerg             -I<libcxx-install-prefix>/include/c++/v1
     46  1.1  joerg             -L<libcxx-install-prefix>/lib \
     47  1.1  joerg             test.cpp -o
     48  1.1  joerg   $ ./a.out # Searches for libc++ in the systems library paths.
     49  1.1  joerg   $ export LD_LIBRARY_PATH=<libcxx-install-prefix>/lib
     50  1.1  joerg   $ ./a.out # Searches for libc++ along LD_LIBRARY_PATH
     51  1.1  joerg 
     52  1.1  joerg Using ``<filesystem>``
     53  1.1  joerg ======================
     54  1.1  joerg 
     55  1.1  joerg Prior to LLVM 9.0, libc++ provides the implementation of the filesystem library
     56  1.1  joerg in a separate static library. Users of ``<filesystem>`` and ``<experimental/filesystem>``
     57  1.1  joerg are required to link ``-lc++fs``. Prior to libc++ 7.0, users of
     58  1.1  joerg ``<experimental/filesystem>`` were required to link libc++experimental.
     59  1.1  joerg 
     60  1.1  joerg Starting with LLVM 9.0, support for ``<filesystem>`` is provided in the main
     61  1.1  joerg library and nothing special is required to use ``<filesystem>``.
     62  1.1  joerg 
     63  1.1  joerg Using libc++experimental and ``<experimental/...>``
     64  1.1  joerg =====================================================
     65  1.1  joerg 
     66  1.1  joerg Libc++ provides implementations of experimental technical specifications
     67  1.1  joerg in a separate library, ``libc++experimental.a``. Users of ``<experimental/...>``
     68  1.1  joerg headers may be required to link ``-lc++experimental``.
     69  1.1  joerg 
     70  1.1  joerg .. code-block:: bash
     71  1.1  joerg 
     72  1.1  joerg   $ clang++ -std=c++14 -stdlib=libc++ test.cpp -lc++experimental
     73  1.1  joerg 
     74  1.1  joerg Libc++experimental.a may not always be available, even when libc++ is already
     75  1.1  joerg installed. For information on building libc++experimental from source see
     76  1.1  joerg :ref:`Building Libc++ <build instructions>` and
     77  1.1  joerg :ref:`libc++experimental CMake Options <libc++experimental options>`.
     78  1.1  joerg 
     79  1.1  joerg Also see the `Experimental Library Implementation Status <http://libcxx.llvm.org/ts1z_status.html>`__
     80  1.1  joerg page.
     81  1.1  joerg 
     82  1.1  joerg .. warning::
     83  1.1  joerg   Experimental libraries are Experimental.
     84  1.1  joerg     * The contents of the ``<experimental/...>`` headers and ``libc++experimental.a``
     85  1.1  joerg       library will not remain compatible between versions.
     86  1.1  joerg     * No guarantees of API or ABI stability are provided.
     87  1.1  joerg     * When we implement the standardized version of an experimental feature,
     88  1.1  joerg       the experimental feature is removed two releases after the non-experimental
     89  1.1  joerg       version has shipped. The full policy is explained :ref:`here <experimental features>`.
     90  1.1  joerg 
     91  1.1  joerg Using libc++ on Linux
     92  1.1  joerg =====================
     93  1.1  joerg 
     94  1.1  joerg On Linux libc++ can typically be used with only '-stdlib=libc++'. However
     95  1.1  joerg some libc++ installations require the user manually link libc++abi themselves.
     96  1.1  joerg If you are running into linker errors when using libc++ try adding '-lc++abi'
     97  1.1  joerg to the link line.  For example:
     98  1.1  joerg 
     99  1.1  joerg .. code-block:: bash
    100  1.1  joerg 
    101  1.1  joerg   $ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
    102  1.1  joerg 
    103  1.1  joerg Alternately, you could just add libc++abi to your libraries list, which in
    104  1.1  joerg most situations will give the same result:
    105  1.1  joerg 
    106  1.1  joerg .. code-block:: bash
    107  1.1  joerg 
    108  1.1  joerg   $ clang++ -stdlib=libc++ test.cpp -lc++abi
    109  1.1  joerg 
    110  1.1  joerg 
    111  1.1  joerg Using libc++ with GCC
    112  1.1  joerg ---------------------
    113  1.1  joerg 
    114  1.1  joerg GCC does not provide a way to switch from libstdc++ to libc++. You must manually
    115  1.1  joerg configure the compile and link commands.
    116  1.1  joerg 
    117  1.1  joerg In particular, you must tell GCC to remove the libstdc++ include directories
    118  1.1  joerg using ``-nostdinc++`` and to not link libstdc++.so using ``-nodefaultlibs``.
    119  1.1  joerg 
    120  1.1  joerg Note that ``-nodefaultlibs`` removes all the standard system libraries and
    121  1.1  joerg not just libstdc++ so they must be manually linked. For example:
    122  1.1  joerg 
    123  1.1  joerg .. code-block:: bash
    124  1.1  joerg 
    125  1.1  joerg   $ g++ -nostdinc++ -I<libcxx-install-prefix>/include/c++/v1 \
    126  1.1  joerg          test.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
    127  1.1  joerg 
    128  1.1  joerg 
    129  1.1  joerg GDB Pretty printers for libc++
    130  1.1  joerg ------------------------------
    131  1.1  joerg 
    132  1.1  joerg GDB does not support pretty-printing of libc++ symbols by default. Unfortunately
    133  1.1  joerg libc++ does not provide pretty-printers itself. However there are 3rd
    134  1.1  joerg party implementations available and although they are not officially
    135  1.1  joerg supported by libc++ they may be useful to users.
    136  1.1  joerg 
    137  1.1  joerg Known 3rd Party Implementations Include:
    138  1.1  joerg 
    139  1.1  joerg * `Koutheir's libc++ pretty-printers <https://github.com/koutheir/libcxx-pretty-printers>`_.
    140  1.1  joerg 
    141  1.1  joerg 
    142  1.1  joerg Libc++ Configuration Macros
    143  1.1  joerg ===========================
    144  1.1  joerg 
    145  1.1  joerg Libc++ provides a number of configuration macros which can be used to enable
    146  1.1  joerg or disable extended libc++ behavior, including enabling "debug mode" or
    147  1.1  joerg thread safety annotations.
    148  1.1  joerg 
    149  1.1  joerg **_LIBCPP_DEBUG**:
    150  1.1  joerg   See :ref:`using-debug-mode` for more information.
    151  1.1  joerg 
    152  1.1  joerg **_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
    153  1.1  joerg   This macro is used to enable -Wthread-safety annotations on libc++'s
    154  1.1  joerg   ``std::mutex`` and ``std::lock_guard``. By default, these annotations are
    155  1.1  joerg   disabled and must be manually enabled by the user.
    156  1.1  joerg 
    157  1.1  joerg **_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**:
    158  1.1  joerg   This macro is used to disable all visibility annotations inside libc++.
    159  1.1  joerg   Defining this macro and then building libc++ with hidden visibility gives a
    160  1.1  joerg   build of libc++ which does not export any symbols, which can be useful when
    161  1.1  joerg   building statically for inclusion into another library.
    162  1.1  joerg 
    163  1.1  joerg **_LIBCPP_DISABLE_EXTERN_TEMPLATE**:
    164  1.1  joerg   This macro is used to disable extern template declarations in the libc++
    165  1.1  joerg   headers. The intended use case is for clients who wish to use the libc++
    166  1.1  joerg   headers without taking a dependency on the libc++ library itself.
    167  1.1  joerg 
    168  1.1  joerg **_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**:
    169  1.1  joerg   This macro disables the additional diagnostics generated by libc++ using the
    170  1.1  joerg   `diagnose_if` attribute. These additional diagnostics include checks for:
    171  1.1  joerg 
    172  1.1  joerg     * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_`
    173  1.1  joerg       counterparts a comparator which is not const callable.
    174  1.1  joerg     * Giving an unordered associative container a hasher that is not const
    175  1.1  joerg       callable.
    176  1.1  joerg 
    177  1.1  joerg **_LIBCPP_NO_VCRUNTIME**:
    178  1.1  joerg   Microsoft's C and C++ headers are fairly entangled, and some of their C++
    179  1.1  joerg   headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled
    180  1.1  joerg   in from a lot of other headers and provides definitions which clash with
    181  1.1  joerg   libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so
    182  1.1  joerg   there's no way for libc++ to provide a compatible definition, since you can't
    183  1.1  joerg   have multiple definitions).
    184  1.1  joerg 
    185  1.1  joerg   By default, libc++ solves this problem by deferring to Microsoft's vcruntime
    186  1.1  joerg   headers where needed. However, it may be undesirable to depend on vcruntime
    187  1.1  joerg   headers, since they may not always be available in cross-compilation setups,
    188  1.1  joerg   or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro
    189  1.1  joerg   prevents libc++ from depending on vcruntime headers. Consequently, it also
    190  1.1  joerg   prevents libc++ headers from being interoperable with vcruntime headers (from
    191  1.1  joerg   the aforementioned clashes), so users of this macro are promising to not
    192  1.1  joerg   attempt to combine libc++ headers with the problematic vcruntime headers. This
    193  1.1  joerg   macro also currently prevents certain `operator new`/`operator delete`
    194  1.1  joerg   replacement scenarios from working, e.g. replacing `operator new` and
    195  1.1  joerg   expecting a non-replaced `operator new[]` to call the replaced `operator new`.
    196  1.1  joerg 
    197  1.1  joerg **_LIBCPP_ENABLE_NODISCARD**:
    198  1.1  joerg   Allow the library to add ``[[nodiscard]]`` attributes to entities not specified
    199  1.1  joerg   as ``[[nodiscard]]`` by the current language dialect. This includes
    200  1.1  joerg   backporting applications of ``[[nodiscard]]`` from newer dialects and
    201  1.1  joerg   additional extended applications at the discretion of the library. All
    202  1.1  joerg   additional applications of ``[[nodiscard]]`` are disabled by default.
    203  1.1  joerg   See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for
    204  1.1  joerg   more information.
    205  1.1  joerg 
    206  1.1  joerg **_LIBCPP_DISABLE_NODISCARD_EXT**:
    207  1.1  joerg   This macro prevents the library from applying ``[[nodiscard]]`` to entities
    208  1.1  joerg   purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
    209  1.1  joerg   for more information.
    210  1.1  joerg 
    211  1.1  joerg **_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
    212  1.1  joerg   This macro disables warnings when using deprecated components. For example,
    213  1.1  joerg   using `std::auto_ptr` when compiling in C++11 mode will normally trigger a
    214  1.1  joerg   warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
    215  1.1  joerg   no warning will be emitted. By default, this macro is not defined.
    216  1.1  joerg 
    217  1.1  joerg C++17 Specific Configuration Macros
    218  1.1  joerg -----------------------------------
    219  1.1  joerg **_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:
    220  1.1  joerg   This macro is used to re-enable all the features removed in C++17. The effect
    221  1.1  joerg   is equivalent to manually defining each macro listed below.
    222  1.1  joerg 
    223  1.1  joerg **_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
    224  1.1  joerg   This macro is used to re-enable the `set_unexpected`, `get_unexpected`, and
    225  1.1  joerg   `unexpected` functions, which were removed in C++17.
    226  1.1  joerg 
    227  1.1  joerg **_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
    228  1.1  joerg   This macro is used to re-enable `std::auto_ptr` in C++17.
    229  1.1  joerg 
    230  1.1  joerg C++20 Specific Configuration Macros:
    231  1.1  joerg ------------------------------------
    232  1.1  joerg **_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**:
    233  1.1  joerg   This macro can be used to disable diagnostics emitted from functions marked
    234  1.1  joerg   ``[[nodiscard]]`` in dialects after C++17.  See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
    235  1.1  joerg   for more information.
    236  1.1  joerg 
    237  1.1  joerg 
    238  1.1  joerg Libc++ Extensions
    239  1.1  joerg =================
    240  1.1  joerg 
    241  1.1  joerg This section documents various extensions provided by libc++, how they're
    242  1.1  joerg provided, and any information regarding how to use them.
    243  1.1  joerg 
    244  1.1  joerg .. _nodiscard extension:
    245  1.1  joerg 
    246  1.1  joerg Extended applications of ``[[nodiscard]]``
    247  1.1  joerg ------------------------------------------
    248  1.1  joerg 
    249  1.1  joerg The ``[[nodiscard]]`` attribute is intended to help users find bugs where
    250  1.1  joerg function return values are ignored when they shouldn't be. After C++17 the
    251  1.1  joerg C++ standard has started to declared such library functions as ``[[nodiscard]]``.
    252  1.1  joerg However, this application is limited and applies only to dialects after C++17.
    253  1.1  joerg Users who want help diagnosing misuses of STL functions may desire a more
    254  1.1  joerg liberal application of ``[[nodiscard]]``.
    255  1.1  joerg 
    256  1.1  joerg For this reason libc++ provides an extension that does just that! The
    257  1.1  joerg extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD``. The extended
    258  1.1  joerg applications of ``[[nodiscard]]`` takes two forms:
    259  1.1  joerg 
    260  1.1  joerg 1. Backporting ``[[nodiscard]]`` to entities declared as such by the
    261  1.1  joerg    standard in newer dialects, but not in the present one.
    262  1.1  joerg 
    263  1.1  joerg 2. Extended applications of ``[[nodiscard]]``, at the library's discretion,
    264  1.1  joerg    applied to entities never declared as such by the standard.
    265  1.1  joerg 
    266  1.1  joerg Users may also opt-out of additional applications ``[[nodiscard]]`` using
    267  1.1  joerg additional macros.
    268  1.1  joerg 
    269  1.1  joerg Applications of the first form, which backport ``[[nodiscard]]`` from a newer
    270  1.1  joerg dialect, may be disabled using macros specific to the dialect in which it was
    271  1.1  joerg added. For example, ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17``.
    272  1.1  joerg 
    273  1.1  joerg Applications of the second form, which are pure extensions, may be disabled
    274  1.1  joerg by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
    275  1.1  joerg 
    276  1.1  joerg 
    277  1.1  joerg Entities declared with ``_LIBCPP_NODISCARD_EXT``
    278  1.1  joerg ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    279  1.1  joerg 
    280  1.1  joerg This section lists all extended applications of ``[[nodiscard]]`` to entities
    281  1.1  joerg which no dialect declares as such (See the second form described above).
    282  1.1  joerg 
    283  1.1  joerg * ``adjacent_find``
    284  1.1  joerg * ``all_of``
    285  1.1  joerg * ``any_of``
    286  1.1  joerg * ``binary_search``
    287  1.1  joerg * ``clamp``
    288  1.1  joerg * ``count_if``
    289  1.1  joerg * ``count``
    290  1.1  joerg * ``equal_range``
    291  1.1  joerg * ``equal``
    292  1.1  joerg * ``find_end``
    293  1.1  joerg * ``find_first_of``
    294  1.1  joerg * ``find_if_not``
    295  1.1  joerg * ``find_if``
    296  1.1  joerg * ``find``
    297  1.1  joerg * ``get_temporary_buffer``
    298  1.1  joerg * ``includes``
    299  1.1  joerg * ``is_heap_until``
    300  1.1  joerg * ``is_heap``
    301  1.1  joerg * ``is_partitioned``
    302  1.1  joerg * ``is_permutation``
    303  1.1  joerg * ``is_sorted_until``
    304  1.1  joerg * ``is_sorted``
    305  1.1  joerg * ``lexicographical_compare``
    306  1.1  joerg * ``lower_bound``
    307  1.1  joerg * ``max_element``
    308  1.1  joerg * ``max``
    309  1.1  joerg * ``min_element``
    310  1.1  joerg * ``min``
    311  1.1  joerg * ``minmax_element``
    312  1.1  joerg * ``minmax``
    313  1.1  joerg * ``mismatch``
    314  1.1  joerg * ``none_of``
    315  1.1  joerg * ``remove_if``
    316  1.1  joerg * ``remove``
    317  1.1  joerg * ``search_n``
    318  1.1  joerg * ``search``
    319  1.1  joerg * ``unique``
    320  1.1  joerg * ``upper_bound``
    321  1.1  joerg * ``lock_guard``'s constructors
    322  1.1  joerg * ``as_const``
    323  1.1  joerg * ``forward``
    324  1.1  joerg * ``move``
    325  1.1  joerg * ``move_if_noexcept``
    326  1.1  joerg * ``identity::operator()``
    327  1.1  joerg * ``to_integer``
    328  1.1  joerg * ``to_underlying``
    329