blob: d8051164caf52fa9e9205e90663aa8ad501eb1fd [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
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
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.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000087
88Using libc++ on Linux
89=====================
90
Eric Fiselier0b09dd12015-10-15 22:41:51 +000091On Linux libc++ can typically be used with only '-stdlib=libc++'. However
92some libc++ installations require the user manually link libc++abi themselves.
93If you are running into linker errors when using libc++ try adding '-lc++abi'
94to the link line. For example:
Eric Fiselierd720d1f2015-08-22 19:40:49 +000095
96.. code-block:: bash
97
98 $ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
99
100Alternately, you could just add libc++abi to your libraries list, which in
101most situations will give the same result:
102
103.. code-block:: bash
104
105 $ clang++ -stdlib=libc++ test.cpp -lc++abi
106
107
108Using libc++ with GCC
109---------------------
110
111GCC does not provide a way to switch from libstdc++ to libc++. You must manually
112configure the compile and link commands.
113
114In particular you must tell GCC to remove the libstdc++ include directories
115using ``-nostdinc++`` and to not link libstdc++.so using ``-nodefaultlibs``.
116
117Note that ``-nodefaultlibs`` removes all of the standard system libraries and
118not just libstdc++ so they must be manually linked. For example:
119
120.. code-block:: bash
121
122 $ g++ -nostdinc++ -I<libcxx-install-prefix>/include/c++/v1 \
123 test.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
Eric Fiselier41ee4312016-01-20 01:26:30 +0000124
125
126GDB Pretty printers for libc++
127------------------------------
128
129GDB does not support pretty-printing of libc++ symbols by default. Unfortunately
130libc++ does not provide pretty-printers itself. However there are 3rd
131party implementations available and although they are not officially
132supported by libc++ they may be useful to users.
133
134Known 3rd Party Implementations Include:
135
136* `Koutheir's libc++ pretty-printers <https://github.com/koutheir/libcxx-pretty-printers>`_.
Eric Fiselierb3825302016-11-13 23:00:30 +0000137
138
139Libc++ Configuration Macros
140===========================
141
142Libc++ provides a number of configuration macros which can be used to enable
143or disable extended libc++ behavior, including enabling "debug mode" or
144thread safety annotations.
145
146**_LIBCPP_DEBUG**:
Eric Fiselierfb825432016-12-28 04:58:52 +0000147 See :ref:`using-debug-mode` for more information.
Eric Fiselierb3825302016-11-13 23:00:30 +0000148
149**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
150 This macro is used to enable -Wthread-safety annotations on libc++'s
151 ``std::mutex`` and ``std::lock_guard``. By default these annotations are
152 disabled and must be manually enabled by the user.
Shoaib Meenaif36e9882016-12-05 19:40:12 +0000153
154**_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**:
155 This macro is used to disable all visibility annotations inside libc++.
156 Defining this macro and then building libc++ with hidden visibility gives a
157 build of libc++ which does not export any symbols, which can be useful when
158 building statically for inclusion into another library.
Eric Fiselier41b686e2016-12-08 23:57:08 +0000159
Shoaib Meenai2ba461c2017-04-13 20:13:32 +0000160**_LIBCPP_DISABLE_EXTERN_TEMPLATE**:
161 This macro is used to disable extern template declarations in the libc++
162 headers. The intended use case is for clients who wish to use the libc++
163 headers without taking a dependency on the libc++ library itself.
164
Eric Fiselier41b686e2016-12-08 23:57:08 +0000165**_LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION**:
166 This macro is used to re-enable an extension in `std::tuple` which allowed
167 it to be implicitly constructed from fewer initializers than contained
168 elements. Elements without an initializer are default constructed. For example:
169
170 .. code-block:: cpp
171
172 std::tuple<std::string, int, std::error_code> foo() {
173 return {"hello world", 42}; // default constructs error_code
174 }
175
176
177 Since libc++ 4.0 this extension has been disabled by default. This macro
178 may be defined to re-enable it in order to support existing code that depends
179 on the extension. New use of this extension should be discouraged.
180 See `PR 27374 <http://llvm.org/PR27374>`_ for more information.
181
182 Note: The "reduced-arity-initialization" extension is still offered but only
183 for explicit conversions. Example:
184
185 .. code-block:: cpp
186
187 auto foo() {
188 using Tup = std::tuple<std::string, int, std::error_code>;
189 return Tup{"hello world", 42}; // explicit constructor called. OK.
190 }
Eric Fiselierd0f7cc02016-12-09 12:32:02 +0000191
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000192**_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**:
193 This macro disables the additional diagnostics generated by libc++ using the
194 `diagnose_if` attribute. These additional diagnostics include checks for:
195
Louis Dionne878a3a82018-12-06 21:46:17 +0000196 * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_`
197 counterparts a comparator which is not const callable.
198 * Giving an unordered associative container a hasher that is not const
199 callable.
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000200
Shoaib Meenaicfd19602017-10-09 19:25:17 +0000201**_LIBCPP_NO_VCRUNTIME**:
202 Microsoft's C and C++ headers are fairly entangled, and some of their C++
203 headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled
204 in from a lot of other headers and provides definitions which clash with
205 libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so
206 there's no way for libc++ to provide a compatible definition, since you can't
207 have multiple definitions).
208
209 By default, libc++ solves this problem by deferring to Microsoft's vcruntime
210 headers where needed. However, it may be undesirable to depend on vcruntime
211 headers, since they may not always be available in cross-compilation setups,
212 or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro
213 prevents libc++ from depending on vcruntime headers. Consequently, it also
214 prevents libc++ headers from being interoperable with vcruntime headers (from
215 the aforementioned clashes), so users of this macro are promising to not
216 attempt to combine libc++ headers with the problematic vcruntime headers. This
217 macro also currently prevents certain `operator new`/`operator delete`
218 replacement scenarios from working, e.g. replacing `operator new` and
219 expecting a non-replaced `operator new[]` to call the replaced `operator new`.
220
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000221**_LIBCPP_ENABLE_NODISCARD**:
222 Allow the library to add ``[[nodiscard]]`` attributes to entities not specified
223 as ``[[nodiscard]]`` by the current language dialect. This includes
224 backporting applications of ``[[nodiscard]]`` from newer dialects and
225 additional extended applications at the discretion of the library. All
226 additional applications of ``[[nodiscard]]`` are disabled by default.
227 See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for
228 more information.
229
230**_LIBCPP_DISABLE_NODISCARD_EXT**:
231 This macro prevents the library from applying ``[[nodiscard]]`` to entities
232 purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
233 for more information.
234
Louis Dionneded5b772019-03-12 20:10:06 +0000235**_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
236 This macro disables warnings when using deprecated components. For example,
237 using `std::auto_ptr` when compiling in C++11 mode will normally trigger a
238 warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
239 no warning will be emitted. By default, this macro is not defined.
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000240
Eric Fiselierddd77792017-02-17 03:25:08 +0000241C++17 Specific Configuration Macros
242-----------------------------------
243**_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:
244 This macro is used to re-enable all the features removed in C++17. The effect
245 is equivalent to manually defining each macro listed below.
246
247**_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
248 This macro is used to re-enable the `set_unexpected`, `get_unexpected`, and
Eric Fiselier65d5b4c2017-02-17 03:30:25 +0000249 `unexpected` functions, which were removed in C++17.
250
251**_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
252 This macro is used to re-enable `std::auto_ptr` in C++17.
Roman Lebedevb5959fa2018-09-22 17:54:48 +0000253
254C++2a Specific Configuration Macros:
255------------------------------------
256**_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**:
257 This macro can be used to disable diagnostics emitted from functions marked
258 ``[[nodiscard]]`` in dialects after C++17. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
259 for more information.
260
261
262Libc++ Extensions
263=================
264
265This section documents various extensions provided by libc++, how they're
266provided, and any information regarding how to use them.
267
268.. _nodiscard extension:
269
270Extended applications of ``[[nodiscard]]``
271------------------------------------------
272
273The ``[[nodiscard]]`` attribute is intended to help users find bugs where
274function return values are ignored when they shouldn't be. After C++17 the
275C++ standard has started to declared such library functions as ``[[nodiscard]]``.
276However, this application is limited and applies only to dialects after C++17.
277Users who want help diagnosing misuses of STL functions may desire a more
278liberal application of ``[[nodiscard]]``.
279
280For this reason libc++ provides an extension that does just that! The
281extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD``. The extended
282applications of ``[[nodiscard]]`` takes two forms:
283
2841. Backporting ``[[nodiscard]]`` to entities declared as such by the
285 standard in newer dialects, but not in the present one.
286
2872. Extended applications of ``[[nodiscard]]``, at the libraries discretion,
288 applied to entities never declared as such by the standard.
289
290Users may also opt-out of additional applications ``[[nodiscard]]`` using
291additional macros.
292
293Applications of the first form, which backport ``[[nodiscard]]`` from a newer
294dialect may be disabled using macros specific to the dialect it was added. For
295example ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17``.
296
297Applications of the second form, which are pure extensions, may be disabled
298by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
299
300
301Entities declared with ``_LIBCPP_NODISCARD_EXT``
302~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303
304This section lists all extended applications of ``[[nodiscard]]`` to entities
305which no dialect declares as such (See the second form described above).
306
307* ``get_temporary_buffer``