10 Usually, libc++ is packaged and shipped by a vendor through some delivery vehicle
11 (operating system distribution, SDK, toolchain, etc) and users don't need to do
12 anything special in order to use the library.
14 This page contains information about configuration knobs that can be used by
15 users when they know libc++ is used by their toolchain, and how to use libc++
16 when it is not the default library used by their toolchain.
19 Using a different version of the C++ Standard
20 =============================================
22 Libc++ implements the various versions of the C++ Standard. Changing the version of
23 the standard can be done by passing ``-std=c++XY`` to the compiler. Libc++ will
24 automatically detect what Standard is being used and will provide functionality that
25 matches that Standard in the library.
29 $ clang++ -std=c++17 test.cpp
32 Using ``-std=c++XY`` with a version of the Standard that has not been ratified yet
33 is considered unstable. Libc++ reserves the right to make breaking changes to the
34 library until the standard has been ratified.
37 Enabling experimental C++ Library features
38 ==========================================
40 Libc++ provides implementations of some experimental features. Experimental features
41 are either Technical Specifications (TSes) or official features that were voted to
42 the Standard but whose implementation is not complete or stable yet in libc++. Those
43 are disabled by default because they are neither API nor ABI stable. However, the
44 ``-fexperimental-library`` compiler flag can be defined to turn those features on.
47 Experimental libraries are experimental.
48 * The contents of the ``<experimental/...>`` headers and the associated static
49 library will not remain compatible between versions.
50 * No guarantees of API or ABI stability are provided.
51 * When the standardized version of an experimental feature is implemented,
52 the experimental feature is removed two releases after the non-experimental
53 version has shipped. The full policy is explained :ref:`here <experimental features>`.
56 On compilers that do not support the ``-fexperimental-library`` flag, users can
57 define the ``_LIBCPP_ENABLE_EXPERIMENTAL`` macro and manually link against the
58 appropriate static library (usually shipped as ``libc++experimental.a``) to get
59 access to experimental library features.
62 Using libc++ when it is not the system default
63 ==============================================
65 On systems where libc++ is provided but is not the default, Clang provides a flag
66 called ``-stdlib=`` that can be used to decide which standard library is used.
67 Using ``-stdlib=libc++`` will select libc++:
71 $ clang++ -stdlib=libc++ test.cpp
73 On systems where libc++ is the library in use by default such as macOS and FreeBSD,
74 this flag is not required.
79 Using a custom built libc++
80 ===========================
82 Most compilers provide a way to disable the default behavior for finding the
83 standard library and to override it with custom paths. With Clang, this can
88 $ clang++ -nostdinc++ -nostdlib++ \
89 -isystem <install>/include/c++/v1 \
91 -Wl,-rpath,<install>/lib \
95 The option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path,
96 which causes the system's dynamic linker to look for libc++ in ``<install>/lib``
97 whenever the program is loaded.
99 GCC does not support the ``-nostdlib++`` flag, so one must use ``-nodefaultlibs``
100 instead. Since that removes all the standard system libraries and not just libc++,
101 the system libraries must be re-added manually. For example:
105 $ g++ -nostdinc++ -nodefaultlibs \
106 -isystem <install>/include/c++/v1 \
108 -Wl,-rpath,<install>/lib \
109 -lc++ -lc++abi -lm -lc -lgcc_s -lgcc \
113 GDB Pretty printers for libc++
114 ==============================
116 GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does
117 provide pretty-printers itself. Those can be used as:
121 $ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \
122 -ex "python register_libcxx_printer_loader()" \
128 Enabling the "safe libc++" mode
129 ===============================
131 Libc++ contains a number of assertions whose goal is to catch undefined behavior in the
132 library, usually caused by precondition violations. Those assertions do not aim to be
133 exhaustive -- instead they aim to provide a good balance between safety and performance.
134 In particular, these assertions do not change the complexity of algorithms. However, they
135 might, in some cases, interfere with compiler optimizations.
137 By default, these assertions are turned off. Vendors can decide to turn them on while building
138 the compiled library by defining ``LIBCXX_ENABLE_ASSERTIONS=ON`` at CMake configuration time.
139 When ``LIBCXX_ENABLE_ASSERTIONS`` is used, the compiled library will be built with assertions
140 enabled, **and** user code will be built with assertions enabled by default. If
141 ``LIBCXX_ENABLE_ASSERTIONS=OFF`` at CMake configure time, the compiled library will not contain
142 assertions and the default when building user code will be to have assertions disabled.
143 As a user, you can consult your vendor to know whether assertions are enabled by default.
145 Furthermore, independently of any vendor-selected default, users can always control whether
146 assertions are enabled in their code by defining ``_LIBCPP_ENABLE_ASSERTIONS=0|1`` before
147 including any libc++ header (we recommend passing ``-D_LIBCPP_ENABLE_ASSERTIONS=X`` to the
148 compiler). Note that if the compiled library was built by the vendor without assertions,
149 functions compiled inside the static or shared library won't have assertions enabled even
150 if the user defines ``_LIBCPP_ENABLE_ASSERTIONS=1`` (the same is true for the inverse case
151 where the static or shared library was compiled **with** assertions but the user tries to
152 disable them). However, most of the code in libc++ is in the headers, so the user-selected
153 value for ``_LIBCPP_ENABLE_ASSERTIONS`` (if any) will usually be respected.
155 When an assertion fails, the program is aborted through a special verbose termination function. The
156 library provides a default function that prints an error message and calls ``std::abort()``. Note
157 that this function is provided by the static or shared library, so it is only available when deploying
158 to a platform where the compiled library is sufficiently recent. On older platforms, the program will
159 terminate in an unspecified unsuccessful manner, but the quality of diagnostics won't be great.
160 However, users can also override that function with their own, which can be useful to either provide
161 custom behavior or when deploying to an older platform where the default function isn't available.
163 Replacing the default verbose termination function is done by defining the
164 ``_LIBCPP_AVAILABILITY_CUSTOM_VERBOSE_ABORT_PROVIDED`` macro in all translation units of your program
165 and defining the following function in exactly one translation unit:
169 void __libcpp_verbose_abort(char const* format, ...)
171 This mechanism is similar to how one can replace the default definition of ``operator new``
172 and ``operator delete``. For example:
176 // In HelloWorldHandler.cpp
177 #include <version> // must include any libc++ header before defining the function (C compatibility headers excluded)
179 void std::__libcpp_verbose_abort(char const* format, ...) {
181 va_start(list, format);
182 std::vfprintf(stderr, format, list);
193 int& x = v[0]; // Your termination function will be called here if _LIBCPP_ENABLE_ASSERTIONS=1
196 Also note that the verbose termination function should never return. Since assertions in libc++
197 catch undefined behavior, your code will proceed with undefined behavior if your function is called
200 Furthermore, exceptions should not be thrown from the function. Indeed, many functions in the
201 library are ``noexcept``, and any exception thrown from the termination function will result
202 in ``std::terminate`` being called.
204 Libc++ Configuration Macros
205 ===========================
207 Libc++ provides a number of configuration macros which can be used to enable
208 or disable extended libc++ behavior, including enabling "debug mode" or
209 thread safety annotations.
211 **_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**:
212 This macro is used to enable -Wthread-safety annotations on libc++'s
213 ``std::mutex`` and ``std::lock_guard``. By default, these annotations are
214 disabled and must be manually enabled by the user.
216 **_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**:
217 This macro is used to disable all visibility annotations inside libc++.
218 Defining this macro and then building libc++ with hidden visibility gives a
219 build of libc++ which does not export any symbols, which can be useful when
220 building statically for inclusion into another library.
222 **_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS**:
223 This macro disables the additional diagnostics generated by libc++ using the
224 `diagnose_if` attribute. These additional diagnostics include checks for:
226 * Giving `set`, `map`, `multiset`, `multimap` and their `unordered_`
227 counterparts a comparator which is not const callable.
228 * Giving an unordered associative container a hasher that is not const
231 **_LIBCPP_NO_VCRUNTIME**:
232 Microsoft's C and C++ headers are fairly entangled, and some of their C++
233 headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled
234 in from a lot of other headers and provides definitions which clash with
235 libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so
236 there's no way for libc++ to provide a compatible definition, since you can't
237 have multiple definitions).
239 By default, libc++ solves this problem by deferring to Microsoft's vcruntime
240 headers where needed. However, it may be undesirable to depend on vcruntime
241 headers, since they may not always be available in cross-compilation setups,
242 or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro
243 prevents libc++ from depending on vcruntime headers. Consequently, it also
244 prevents libc++ headers from being interoperable with vcruntime headers (from
245 the aforementioned clashes), so users of this macro are promising to not
246 attempt to combine libc++ headers with the problematic vcruntime headers. This
247 macro also currently prevents certain `operator new`/`operator delete`
248 replacement scenarios from working, e.g. replacing `operator new` and
249 expecting a non-replaced `operator new[]` to call the replaced `operator new`.
251 **_LIBCPP_ENABLE_NODISCARD**:
252 Allow the library to add ``[[nodiscard]]`` attributes to entities not specified
253 as ``[[nodiscard]]`` by the current language dialect. This includes
254 backporting applications of ``[[nodiscard]]`` from newer dialects and
255 additional extended applications at the discretion of the library. All
256 additional applications of ``[[nodiscard]]`` are disabled by default.
257 See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>` for
260 **_LIBCPP_DISABLE_NODISCARD_EXT**:
261 This macro prevents the library from applying ``[[nodiscard]]`` to entities
262 purely as an extension. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
263 for more information.
265 **_LIBCPP_DISABLE_DEPRECATION_WARNINGS**:
266 This macro disables warnings when using deprecated components. For example,
267 using `std::auto_ptr` when compiling in C++11 mode will normally trigger a
268 warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
269 no warning will be emitted. By default, this macro is not defined.
271 C++17 Specific Configuration Macros
272 -----------------------------------
273 **_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:
274 This macro is used to re-enable all the features removed in C++17. The effect
275 is equivalent to manually defining each macro listed below.
277 **_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**:
278 This macro is used to re-enable `auto_ptr`.
280 **_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**:
281 This macro is used to re-enable the `binder1st`, `binder2nd`,
282 `pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`,
283 `mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`,
284 `const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t`
285 class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`,
286 and `ptr_fun` functions.
288 **_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**:
289 This macro is used to re-enable the `random_shuffle` algorithm.
291 **_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**:
292 This macro is used to re-enable `set_unexpected`, `get_unexpected`, and
295 C++20 Specific Configuration Macros
296 -----------------------------------
297 **_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17**:
298 This macro can be used to disable diagnostics emitted from functions marked
299 ``[[nodiscard]]`` in dialects after C++17. See :ref:`Extended Applications of [[nodiscard]] <nodiscard extension>`
300 for more information.
302 **_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**:
303 This macro is used to re-enable all the features removed in C++20. The effect
304 is equivalent to manually defining each macro listed below.
306 **_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS**:
307 This macro is used to re-enable redundant members of `allocator<T>`,
308 including `pointer`, `reference`, `rebind`, `address`, `max_size`,
309 `construct`, `destroy`, and the two-argument overload of `allocate`.
311 **_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION**:
312 This macro is used to re-enable the library-provided specializations of
313 `allocator<void>` and `allocator<const void>`.
314 Use it in conjunction with `_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS`
315 to ensure that removed members of `allocator<void>` can be accessed.
317 **_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**:
318 This macro is used to re-enable the `argument_type`, `result_type`,
319 `first_argument_type`, and `second_argument_type` members of class
320 templates such as `plus`, `logical_not`, `hash`, and `owner_less`.
322 **_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**:
323 This macro is used to re-enable `not1`, `not2`, `unary_negate`,
326 **_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**:
327 This macro is used to re-enable `raw_storage_iterator`.
329 **_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS**:
330 This macro is used to re-enable `is_literal_type`, `is_literal_type_v`,
331 `result_of` and `result_of_t`.
337 This section documents various extensions provided by libc++, how they're
338 provided, and any information regarding how to use them.
340 .. _nodiscard extension:
342 Extended applications of ``[[nodiscard]]``
343 ------------------------------------------
345 The ``[[nodiscard]]`` attribute is intended to help users find bugs where
346 function return values are ignored when they shouldn't be. After C++17 the
347 C++ standard has started to declared such library functions as ``[[nodiscard]]``.
348 However, this application is limited and applies only to dialects after C++17.
349 Users who want help diagnosing misuses of STL functions may desire a more
350 liberal application of ``[[nodiscard]]``.
352 For this reason libc++ provides an extension that does just that! The
353 extension must be enabled by defining ``_LIBCPP_ENABLE_NODISCARD``. The extended
354 applications of ``[[nodiscard]]`` takes two forms:
356 1. Backporting ``[[nodiscard]]`` to entities declared as such by the
357 standard in newer dialects, but not in the present one.
359 2. Extended applications of ``[[nodiscard]]``, at the library's discretion,
360 applied to entities never declared as such by the standard.
362 Users may also opt-out of additional applications ``[[nodiscard]]`` using
365 Applications of the first form, which backport ``[[nodiscard]]`` from a newer
366 dialect, may be disabled using macros specific to the dialect in which it was
367 added. For example, ``_LIBCPP_DISABLE_NODISCARD_AFTER_CXX17``.
369 Applications of the second form, which are pure extensions, may be disabled
370 by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
373 Entities declared with ``_LIBCPP_NODISCARD_EXT``
374 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
376 This section lists all extended applications of ``[[nodiscard]]`` to entities
377 which no dialect declares as such (See the second form described above).
393 * ``get_temporary_buffer``
399 * ``is_sorted_until``
401 * ``lexicographical_compare``
417 * ``lock_guard``'s constructors
422 * ``move_if_noexcept``
423 * ``identity::operator()``
427 Additional types supported in random distributions
428 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
430 The `C++ Standard <http://eel.is/c++draft/rand#req.genl-1.5>`_ mentions that instantiating several random number
431 distributions with types other than ``short``, ``int``, ``long``, ``long long``, and their unsigned versions is
432 undefined. As an extension, libc++ supports instantiating ``binomial_distribution``, ``discrete_distribution``,
433 ``geometric_distribution``, ``negative_binomial_distribution``, ``poisson_distribution``, and ``uniform_int_distribution``
434 with ``int8_t``, ``__int128_t`` and their unsigned versions.
436 Extended integral type support
437 ------------------------------
439 Several platforms support the 128-bit integral types ``__int128_t`` and
440 ``__uint128_t``. When these types are present they can be used in the headers
441 as required by the Standard:
448 As an extension these types can be used in the following headers:
453 Extensions to ``<format>``
454 --------------------------
456 The exposition only type ``basic-format-string`` and its typedefs
457 ``format-string`` and ``wformat-string`` became ``basic_format_string``,
458 ``format_string``, and ``wformat_string`` in C++23. Libc++ makes these types
459 available in C++20 as an extension.