blob: 88b287d5834c7ec3046d32ac6df2bc3927e5cf8c [file] [log] [blame]
Eric Fiselierd720d1f2015-08-22 19:40:49 +00001============
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
J. Ryan Stinnettc858d6a2019-05-30 16:46:22 +000018On macOS and FreeBSD libc++ is the default standard library
Eric Fiselierd720d1f2015-08-22 19:40:49 +000019and 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
J. Ryan Stinnettc858d6a2019-05-30 16:46:22 +000037environment variable ``LD_LIBRARY_PATH`` (``DYLD_LIBRARY_PATH`` on macOS) can
Eric Fiselierd720d1f2015-08-22 19:40:49 +000038be 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
Louis Dionne05ed4df2019-03-21 16:21:09 +000052Using ``<filesystem>``
53======================
54
55Prior to LLVM 9.0, libc++ provides the implementation of the filesystem library
56in a separate static library. Users of ``<filesystem>`` and ``<experimental/filesystem>``
57are required to link ``-lc++fs``. Prior to libc++ 7.0, users of
58``<experimental/filesystem>`` were required to link libc++experimental.
59
60Starting with LLVM 9.0, support for ``<filesystem>`` is provided in the main
61library and nothing special is required to use ``<filesystem>``.
Eric Fiselier02cea5e2018-07-27 03:07:09 +000062
Eric Fiselierfa43a5c2016-05-03 22:32:08 +000063Using libc++experimental and ``<experimental/...>``
64=====================================================
Eric Fiselierd720d1f2015-08-22 19:40:49 +000065
Eric Fiselierfa43a5c2016-05-03 22:32:08 +000066Libc++ provides implementations of experimental technical specifications
67in a separate library, ``libc++experimental.a``. Users of ``<experimental/...>``
Eric Fiselierec46d402016-05-06 04:49:30 +000068headers may be required to link ``-lc++experimental``.
Eric Fiselierfa43a5c2016-05-03 22:32:08 +000069
70.. code-block:: bash
71
72 $ clang++ -std=c++14 -stdlib=libc++ test.cpp -lc++experimental
73
74Libc++experimental.a may not always be available, even when libc++ is already
75installed. For information on building libc++experimental from source see
76:ref:`Building Libc++ <build instructions>` and
77:ref:`libc++experimental CMake Options <libc++experimental options>`.
78
79Also see the `Experimental Library Implementation Status <http://libcxx.llvm.org/ts1z_status.html>`__
80page.
81
82.. warning::
83 Experimental libraries are Experimental.
84 * The contents of the ``<experimental/...>`` headers and ``libc++experimental.a``
85 library will not remain compatible between versions.
86 * No guarantees of API or ABI stability are provided.
Louis Dionne37677132019-06-11 14:48:40 +000087 * When we implement the standardized version of an experimental feature,
88 the experimental feature is removed two releases after the non-experimental
89 version has shipped. The full policy is explained :ref:`here <experimental features>`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000090
91Using libc++ on Linux
92=====================
93
Eric Fiselier0b09dd12015-10-15 22:41:51 +000094On Linux libc++ can typically be used with only '-stdlib=libc++'. However
95some libc++ installations require the user manually link libc++abi themselves.
96If you are running into linker errors when using libc++ try adding '-lc++abi'
97to the link line. For example:
Eric Fiselierd720d1f2015-08-22 19:40:49 +000098
99.. code-block:: bash
100
101 $ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
102
103Alternately, you could just add libc++abi to your libraries list, which in
104most situations will give the same result:
105
106.. code-block:: bash
107
108 $ clang++ -stdlib=libc++ test.cpp -lc++abi
109
110
111Using libc++ with GCC
112---------------------
113
114GCC does not provide a way to switch from libstdc++ to libc++. You must manually
115configure the compile and link commands.
116
Jennifer Chukwub9788092021-04-17 20:34:06 +0530117In particular, you must tell GCC to remove the libstdc++ include directories
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000118using ``-nostdinc++`` and to not link libstdc++.so using ``-nodefaultlibs``.
119
Jennifer Chukwub9788092021-04-17 20:34:06 +0530120Note that ``-nodefaultlibs`` removes all the standard system libraries and
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000121not just libstdc++ so they must be manually linked. For example:
122
123.. code-block:: bash
124
125 $ g++ -nostdinc++ -I<libcxx-install-prefix>/include/c++/v1 \
126 test.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
Eric Fiselier41ee4312016-01-20 01:26:30 +0000127
128
129GDB Pretty printers for libc++
130------------------------------
131
132GDB does not support pretty-printing of libc++ symbols by default. Unfortunately
133libc++ does not provide pretty-printers itself. However there are 3rd
134party implementations available and although they are not officially
135supported by libc++ they may be useful to users.
136
137Known 3rd Party Implementations Include:
138
139* `Koutheir's libc++ pretty-printers <https://github.com/koutheir/libcxx-pretty-printers>`_.
Eric Fiselierb3825302016-11-13 23:00:30 +0000140
141
142Libc++ Configuration Macros
143===========================
144
145Libc++ provides a number of configuration macros which can be used to enable
146or disable extended libc++ behavior, including enabling "debug mode" or
147thread safety annotations.
148
149**_LIBCPP_DEBUG**:
Eric Fiselierfb825432016-12-28 04:58:52 +0000150 See :ref:`using-debug-mode` for more information.
Eric Fiselierb3825302016-11-13 23:00:30 +0000151
152**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
153 This macro is used to enable -Wthread-safety annotations on libc++'s
Jennifer Chukwub9788092021-04-17 20:34:06 +0530154 ``std::mutex`` and ``std::lock_guard``. By default, these annotations are
Eric Fiselierb3825302016-11-13 23:00:30 +0000155 disabled and must be manually enabled by the user.
Shoaib Meenaif36e9882016-12-05 19:40:12 +0000156
157**_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**:
158 This macro is used to disable all visibility annotations inside libc++.
159 Defining this macro and then building libc++ with hidden visibility gives a
160 build of libc++ which does not export any symbols, which can be useful when
161 building statically for inclusion into another library.
Eric Fiselier41b686e2016-12-08 23:57:08 +0000162
Shoaib Meenai2ba461c2017-04-13 20:13:32 +0000163**_LIBCPP_DISABLE_EXTERN_TEMPLATE**:
164 This macro is used to disable extern template declarations in the libc++
165 headers. The intended use case is for clients who wish to use the libc++
166 headers without taking a dependency on the libc++ library itself.
167
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000168**_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**:
169 This macro disables the additional diagnostics generated by libc++ using the
170 `diagnose_if` attribute. These additional diagnostics include checks for:
171
Louis Dionne878a3a82018-12-06 21:46:17 +0000172 * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_`
173 counterparts a comparator which is not const callable.
174 * Giving an unordered associative container a hasher that is not const
175 callable.
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000176
Shoaib Meenaicfd19602017-10-09 19:25:17 +0000177**_LIBCPP_NO_VCRUNTIME**:
178 Microsoft's C and C++ headers are fairly entangled, and some of their C++
179 headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled
180 in from a lot of other headers and provides definitions which clash with
181 libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so
182 there's no way for libc++ to provide a compatible definition, since you can't
183 have multiple definitions).
184
185 By default, libc++ solves this problem by deferring to Microsoft's vcruntime
186 headers where needed. However, it may be undesirable to depend on vcruntime
187 headers, since they may not always be available in cross-compilation setups,
188 or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro
189 prevents libc++ from depending on vcruntime headers. Consequently, it also
190 prevents libc++ headers from being interoperable with vcruntime headers (from
191 the aforementioned clashes), so users of this macro are promising to not
192 attempt to combine libc++ headers with the problematic vcruntime headers. This
193 macro also currently prevents certain `operator new`/`operator delete`
194 replacement scenarios from working, e.g. replacing `operator new` and
195 expecting a non-replaced `operator new[]` to call the replaced `operator new`.
196
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000197**_LIBCPP_ENABLE_NODISCARD**:
198 Allow the library to add ``[[nodiscard]]`` attributes to entities not specified
199 as ``[[nodiscard]]`` by the current language dialect. This includes
200 backporting applications of ``[[nodiscard]]`` from newer dialects and
201 additional extended applications at the discretion of the library. All
202 additional applications of ``[[nodiscard]]`` are disabled by default.
203 See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for
204 more information.
205
206**_LIBCPP_DISABLE_NODISCARD_EXT**:
207 This macro prevents the library from applying ``[[nodiscard]]`` to entities
208 purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
209 for more information.
210
Louis Dionneded5b772019-03-12 20:10:06 +0000211**_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
212 This macro disables warnings when using deprecated components. For example,
213 using `std::auto_ptr` when compiling in C++11 mode will normally trigger a
214 warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
215 no warning will be emitted. By default, this macro is not defined.
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000216
Eric Fiselierddd77792017-02-17 03:25:08 +0000217C++17 Specific Configuration Macros
218-----------------------------------
219**_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:
220 This macro is used to re-enable all the features removed in C++17. The effect
221 is equivalent to manually defining each macro listed below.
222
Eric Fiselier65d5b4c2017-02-17 03:30:25 +0000223**_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400224 This macro is used to re-enable `auto_ptr`.
225
226**_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**:
227 This macro is used to re-enable the `binder1st`, `binder2nd`,
228 `pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`,
229 `mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`,
230 `const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t`
231 class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`,
232 and `ptr_fun` functions.
233
234**_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**:
235 This macro is used to re-enable the `random_shuffle` algorithm.
236
237**_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
238 This macro is used to re-enable `set_unexpected`, `get_unexpected`, and
239 `unexpected`.
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000240
Marek Kurdej24b4c512021-01-07 12:29:04 +0100241C++20 Specific Configuration Macros:
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000242------------------------------------
243**_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**:
244 This macro can be used to disable diagnostics emitted from functions marked
245 ``[[nodiscard]]`` in dialects after C++17. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
246 for more information.
247
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400248**_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**:
249 This macro is used to re-enable all the features removed in C++20. The effect
250 is equivalent to manually defining each macro listed below.
251
252**_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**:
253 This macro is used to re-enable redundant members of `allocator<T>`,
254 including `pointer`, `reference`, `rebind`, `address`, `max_size`,
255 `construct`, `destroy`, and the two-argument overload of `allocate`.
256 It also re-enables the library-provided explicit specializations
257 of `allocator<void>` and `allocator<const void>`.
258
Arthur O'Dwyerf5486c82021-05-25 14:34:18 -0400259**_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**:
260 This macro is used to re-enable the `argument_type`, `result_type`,
261 `first_argument_type`, and `second_argument_type` members of class
262 templates such as `plus`, `logical_not`, `hash`, and `owner_less`.
263
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400264**_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**:
265 This macro is used to re-enable `not1`, `not2`, `unary_negate`,
266 and `binary_negate`.
267
268**_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**:
269 This macro is used to re-enable `raw_storage_iterator`.
270
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000271
272Libc++ Extensions
273=================
274
275This section documents various extensions provided by libc++, how they're
276provided, and any information regarding how to use them.
277
278.. _nodiscard extension:
279
280Extended applications of ``[[nodiscard]]``
281------------------------------------------
282
283The ``[[nodiscard]]`` attribute is intended to help users find bugs where
284function return values are ignored when they shouldn't be. After C++17 the
285C++ standard has started to declared such library functions as ``[[nodiscard]]``.
286However, this application is limited and applies only to dialects after C++17.
287Users who want help diagnosing misuses of STL functions may desire a more
288liberal application of ``[[nodiscard]]``.
289
290For this reason libc++ provides an extension that does just that! The
291extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD``. The extended
292applications of ``[[nodiscard]]`` takes two forms:
293
2941. Backporting ``[[nodiscard]]`` to entities declared as such by the
295 standard in newer dialects, but not in the present one.
296
Arthur O'Dwyer108facb2021-04-05 14:56:03 -04002972. Extended applications of ``[[nodiscard]]``, at the library's discretion,
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000298 applied to entities never declared as such by the standard.
299
300Users may also opt-out of additional applications ``[[nodiscard]]`` using
301additional macros.
302
303Applications of the first form, which backport ``[[nodiscard]]`` from a newer
Arthur O'Dwyer108facb2021-04-05 14:56:03 -0400304dialect, may be disabled using macros specific to the dialect in which it was
305added. For example, ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17``.
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000306
307Applications of the second form, which are pure extensions, may be disabled
308by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
309
310
311Entities declared with ``_LIBCPP_NODISCARD_EXT``
312~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
313
314This section lists all extended applications of ``[[nodiscard]]`` to entities
315which no dialect declares as such (See the second form described above).
316
Nico Weber471b10a2019-04-03 18:13:08 +0000317* ``adjacent_find``
318* ``all_of``
319* ``any_of``
320* ``binary_search``
321* ``clamp``
322* ``count_if``
323* ``count``
324* ``equal_range``
325* ``equal``
326* ``find_end``
327* ``find_first_of``
328* ``find_if_not``
329* ``find_if``
330* ``find``
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000331* ``get_temporary_buffer``
Nico Weber471b10a2019-04-03 18:13:08 +0000332* ``includes``
333* ``is_heap_until``
334* ``is_heap``
335* ``is_partitioned``
336* ``is_permutation``
337* ``is_sorted_until``
338* ``is_sorted``
339* ``lexicographical_compare``
340* ``lower_bound``
341* ``max_element``
342* ``max``
343* ``min_element``
344* ``min``
345* ``minmax_element``
346* ``minmax``
347* ``mismatch``
348* ``none_of``
349* ``remove_if``
350* ``remove``
351* ``search_n``
352* ``search``
353* ``unique``
354* ``upper_bound``
Louis Dionne346587e2019-08-13 11:12:28 +0000355* ``lock_guard``'s constructors
Arthur O'Dwyer108facb2021-04-05 14:56:03 -0400356* ``as_const``
357* ``forward``
358* ``move``
359* ``move_if_noexcept``
360* ``identity::operator()``
361* ``to_integer``
362* ``to_underlying``