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