| 1 |
.. _using-libcxx: |
| 2 |
|
| 3 |
============ |
| 4 |
Using libc++ |
| 5 |
============ |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 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. |
| 13 |
|
| 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. |
| 17 |
|
| 18 |
|
| 19 |
Using a different version of the C++ Standard |
| 20 |
============================================= |
| 21 |
|
| 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. |
| 26 |
|
| 27 |
.. code-block:: bash |
| 28 |
|
| 29 |
$ clang++ -std=c++17 test.cpp |
| 30 |
|
| 31 |
.. warning:: |
| 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. |
| 35 |
|
| 36 |
|
| 37 |
Enabling experimental C++ Library features |
| 38 |
========================================== |
| 39 |
|
| 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. |
| 45 |
|
| 46 |
The following features are currently considered experimental and are only provided |
| 47 |
when ``-fexperimental-library`` is passed: |
| 48 |
|
| 49 |
* The parallel algorithms library (``<execution>`` and the associated algorithms) |
| 50 |
* ``std::stop_token``, ``std::stop_source`` and ``std::stop_callback`` |
| 51 |
* ``std::jthread`` |
| 52 |
* ``std::chrono::tzdb`` and related time zone functionality |
| 53 |
|
| 54 |
.. warning:: |
| 55 |
Experimental libraries are experimental. |
| 56 |
* The contents of the ``<experimental/...>`` headers and the associated static |
| 57 |
library will not remain compatible between versions. |
| 58 |
* No guarantees of API or ABI stability are provided. |
| 59 |
* When the standardized version of an experimental feature is implemented, |
| 60 |
the experimental feature is removed two releases after the non-experimental |
| 61 |
version has shipped. The full policy is explained :ref:`here <experimental features>`. |
| 62 |
|
| 63 |
.. note:: |
| 64 |
On compilers that do not support the ``-fexperimental-library`` flag, users can |
| 65 |
define the ``_LIBCPP_ENABLE_EXPERIMENTAL`` macro and manually link against the |
| 66 |
appropriate static library (usually shipped as ``libc++experimental.a``) to get |
| 67 |
access to experimental library features. |
| 68 |
|
| 69 |
|
| 70 |
Using libc++ when it is not the system default |
| 71 |
============================================== |
| 72 |
|
| 73 |
On systems where libc++ is provided but is not the default, Clang provides a flag |
| 74 |
called ``-stdlib=`` that can be used to decide which standard library is used. |
| 75 |
Using ``-stdlib=libc++`` will select libc++: |
| 76 |
|
| 77 |
.. code-block:: bash |
| 78 |
|
| 79 |
$ clang++ -stdlib=libc++ test.cpp |
| 80 |
|
| 81 |
On systems where libc++ is the library in use by default such as macOS and FreeBSD, |
| 82 |
this flag is not required. |
| 83 |
|
| 84 |
|
| 85 |
.. _alternate libcxx: |
| 86 |
|
| 87 |
Using a custom built libc++ |
| 88 |
=========================== |
| 89 |
|
| 90 |
Most compilers provide a way to disable the default behavior for finding the |
| 91 |
standard library and to override it with custom paths. With Clang, this can |
| 92 |
be done with: |
| 93 |
|
| 94 |
.. code-block:: bash |
| 95 |
|
| 96 |
$ clang++ -nostdinc++ -nostdlib++ \ |
| 97 |
-isystem <install>/include/c++/v1 \ |
| 98 |
-L <install>/lib \ |
| 99 |
-Wl,-rpath,<install>/lib \ |
| 100 |
-lc++ \ |
| 101 |
test.cpp |
| 102 |
|
| 103 |
The option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path, |
| 104 |
which causes the system's dynamic linker to look for libc++ in ``<install>/lib`` |
| 105 |
whenever the program is loaded. |
| 106 |
|
| 107 |
GCC does not support the ``-nostdlib++`` flag, so one must use ``-nodefaultlibs`` |
| 108 |
instead. Since that removes all the standard system libraries and not just libc++, |
| 109 |
the system libraries must be re-added manually. For example: |
| 110 |
|
| 111 |
.. code-block:: bash |
| 112 |
|
| 113 |
$ g++ -nostdinc++ -nodefaultlibs \ |
| 114 |
-isystem <install>/include/c++/v1 \ |
| 115 |
-L <install>/lib \ |
| 116 |
-Wl,-rpath,<install>/lib \ |
| 117 |
-lc++ -lc++abi -lm -lc -lgcc_s -lgcc \ |
| 118 |
test.cpp |
| 119 |
|
| 120 |
|
| 121 |
GDB Pretty printers for libc++ |
| 122 |
============================== |
| 123 |
|
| 124 |
GDB does not support pretty-printing of libc++ symbols by default. However, libc++ does |
| 125 |
provide pretty-printers itself. Those can be used as: |
| 126 |
|
| 127 |
.. code-block:: bash |
| 128 |
|
| 129 |
$ gdb -ex "source <libcxx>/utils/gdb/libcxx/printers.py" \ |
| 130 |
-ex "python register_libcxx_printer_loader()" \ |
| 131 |
<args> |
| 132 |
|
| 133 |
.. _include-what-you-use: |
| 134 |
|
| 135 |
include-what-you-use (IWYU) |
| 136 |
=========================== |
| 137 |
|
| 138 |
libc++ provides an IWYU `mapping file <https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUMappings.md>`_, |
| 139 |
which drastically improves the accuracy of the tool when using libc++. To use the mapping file with |
| 140 |
IWYU, you should run the tool like so: |
| 141 |
|
| 142 |
.. code-block:: bash |
| 143 |
|
| 144 |
$ include-what-you-use -Xiwyu --mapping_file=/path/to/libcxx/include/libcxx.imp file.cpp |
| 145 |
|
| 146 |
If you would prefer to not use that flag, then you can replace ``/path/to/include-what-you-use/share/libcxx.imp`` |
| 147 |
file with the libc++-provided ``libcxx.imp`` file. |
| 148 |
|
| 149 |
Libc++ Configuration Macros |
| 150 |
=========================== |
| 151 |
|
| 152 |
Libc++ provides a number of configuration macros which can be used to enable |
| 153 |
or disable extended libc++ behavior, including enabling hardening or thread |
| 154 |
safety annotations. |
| 155 |
|
| 156 |
**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS**: |
| 157 |
This macro is used to enable -Wthread-safety annotations on libc++'s |
| 158 |
``std::mutex`` and ``std::lock_guard``. By default, these annotations are |
| 159 |
disabled and must be manually enabled by the user. |
| 160 |
|
| 161 |
**_LIBCPP_HARDENING_MODE**: |
| 162 |
This macro is used to choose the :ref:`hardening mode <using-hardening-modes>`. |
| 163 |
|
| 164 |
**_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS**: |
| 165 |
This macro is used to disable all visibility annotations inside libc++. |
| 166 |
Defining this macro and then building libc++ with hidden visibility gives a |
| 167 |
build of libc++ which does not export any symbols, which can be useful when |
| 168 |
building statically for inclusion into another library. |
| 169 |
|
| 170 |
**_LIBCPP_NO_VCRUNTIME**: |
| 171 |
Microsoft's C and C++ headers are fairly entangled, and some of their C++ |
| 172 |
headers are fairly hard to avoid. In particular, `vcruntime_new.h` gets pulled |
| 173 |
in from a lot of other headers and provides definitions which clash with |
| 174 |
libc++ headers, such as `nothrow_t` (note that `nothrow_t` is a struct, so |
| 175 |
there's no way for libc++ to provide a compatible definition, since you can't |
| 176 |
have multiple definitions). |
| 177 |
|
| 178 |
By default, libc++ solves this problem by deferring to Microsoft's vcruntime |
| 179 |
headers where needed. However, it may be undesirable to depend on vcruntime |
| 180 |
headers, since they may not always be available in cross-compilation setups, |
| 181 |
or they may clash with other headers. The `_LIBCPP_NO_VCRUNTIME` macro |
| 182 |
prevents libc++ from depending on vcruntime headers. Consequently, it also |
| 183 |
prevents libc++ headers from being interoperable with vcruntime headers (from |
| 184 |
the aforementioned clashes), so users of this macro are promising to not |
| 185 |
attempt to combine libc++ headers with the problematic vcruntime headers. This |
| 186 |
macro also currently prevents certain `operator new`/`operator delete` |
| 187 |
replacement scenarios from working, e.g. replacing `operator new` and |
| 188 |
expecting a non-replaced `operator new[]` to call the replaced `operator new`. |
| 189 |
|
| 190 |
**_LIBCPP_DISABLE_DEPRECATION_WARNINGS**: |
| 191 |
This macro disables warnings when using deprecated components. For example, |
| 192 |
using `std::auto_ptr` when compiling in C++11 mode will normally trigger a |
| 193 |
warning saying that `std::auto_ptr` is deprecated. If the macro is defined, |
| 194 |
no warning will be emitted. By default, this macro is not defined. |
| 195 |
|
| 196 |
C++17 Specific Configuration Macros |
| 197 |
----------------------------------- |
| 198 |
**_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR**: |
| 199 |
This macro is used to re-enable `auto_ptr`. |
| 200 |
|
| 201 |
**_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS**: |
| 202 |
This macro is used to re-enable the `binder1st`, `binder2nd`, |
| 203 |
`pointer_to_unary_function`, `pointer_to_binary_function`, `mem_fun_t`, |
| 204 |
`mem_fun1_t`, `mem_fun_ref_t`, `mem_fun1_ref_t`, `const_mem_fun_t`, |
| 205 |
`const_mem_fun1_t`, `const_mem_fun_ref_t`, and `const_mem_fun1_ref_t` |
| 206 |
class templates, and the `bind1st`, `bind2nd`, `mem_fun`, `mem_fun_ref`, |
| 207 |
and `ptr_fun` functions. |
| 208 |
|
| 209 |
**_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE**: |
| 210 |
This macro is used to re-enable the `random_shuffle` algorithm. |
| 211 |
|
| 212 |
**_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS**: |
| 213 |
This macro is used to re-enable `set_unexpected`, `get_unexpected`, and |
| 214 |
`unexpected`. |
| 215 |
|
| 216 |
C++20 Specific Configuration Macros |
| 217 |
----------------------------------- |
| 218 |
**_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE**: |
| 219 |
This macro is used to re-enable the function |
| 220 |
``std::shared_ptr<...>::unique()``. |
| 221 |
|
| 222 |
**_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS**: |
| 223 |
This macro is used to re-enable the `argument_type`, `result_type`, |
| 224 |
`first_argument_type`, and `second_argument_type` members of class |
| 225 |
templates such as `plus`, `logical_not`, `hash`, and `owner_less`. |
| 226 |
|
| 227 |
**_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS**: |
| 228 |
This macro is used to re-enable `not1`, `not2`, `unary_negate`, |
| 229 |
and `binary_negate`. |
| 230 |
|
| 231 |
**_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR**: |
| 232 |
This macro is used to re-enable `raw_storage_iterator`. |
| 233 |
|
| 234 |
**_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS**: |
| 235 |
This macro is used to re-enable `is_literal_type`, `is_literal_type_v`, |
| 236 |
`result_of` and `result_of_t`. |
| 237 |
|
| 238 |
|
| 239 |
C++26 Specific Configuration Macros |
| 240 |
----------------------------------- |
| 241 |
|
| 242 |
**_LIBCPP_ENABLE_CXX26_REMOVED_CODECVT**: |
| 243 |
This macro is used to re-enable all named declarations in ``<codecvt>``. |
| 244 |
|
| 245 |
**_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE**: |
| 246 |
This macro is used to re-enable the function |
| 247 |
``std::basic_string<...>::reserve()``. |
| 248 |
|
| 249 |
**_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS**: |
| 250 |
This macro is used to re-enable redundant member of ``allocator<T>::is_always_equal``. |
| 251 |
|
| 252 |
**_LIBCPP_ENABLE_CXX26_REMOVED_STRSTREAM**: |
| 253 |
This macro is used to re-enable all named declarations in ``<strstream>``. |
| 254 |
|
| 255 |
**_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT**: |
| 256 |
This macro is used to re-enable the ``wstring_convert`` and ``wbuffer_convert`` |
| 257 |
in ``<locale>``. |
| 258 |
|
| 259 |
Libc++ Extensions |
| 260 |
================= |
| 261 |
|
| 262 |
This section documents various extensions provided by libc++, how they're |
| 263 |
provided, and any information regarding how to use them. |
| 264 |
|
| 265 |
Extended integral type support |
| 266 |
------------------------------ |
| 267 |
|
| 268 |
Several platforms support types that are not specified in the Standard, such as |
| 269 |
the 128-bit integral types ``__int128_t`` and ``__uint128_t``. As an extension, |
| 270 |
libc++ does a best-effort attempt to support these types like other integral |
| 271 |
types, by supporting them notably in: |
| 272 |
|
| 273 |
* ``<bits>`` |
| 274 |
* ``<charconv>`` |
| 275 |
* ``<functional>`` |
| 276 |
* ``<type_traits>`` |
| 277 |
* ``<format>`` |
| 278 |
* ``<random>`` |
| 279 |
|
| 280 |
Additional types supported in random distributions |
| 281 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 282 |
|
| 283 |
The `C++ Standard <http://eel.is/c++draft/rand#req.genl-1.5>`_ mentions that instantiating several random number |
| 284 |
distributions with types other than ``short``, ``int``, ``long``, ``long long``, and their unsigned versions is |
| 285 |
undefined. As an extension, libc++ supports instantiating ``binomial_distribution``, ``discrete_distribution``, |
| 286 |
``geometric_distribution``, ``negative_binomial_distribution``, ``poisson_distribution``, and ``uniform_int_distribution`` |
| 287 |
with ``int8_t``, ``__int128_t`` and their unsigned versions. |
| 288 |
|
| 289 |
Extensions to ``<format>`` |
| 290 |
-------------------------- |
| 291 |
|
| 292 |
The exposition only type ``basic-format-string`` and its typedefs |
| 293 |
``format-string`` and ``wformat-string`` became ``basic_format_string``, |
| 294 |
``format_string``, and ``wformat_string`` in C++23. Libc++ makes these types |
| 295 |
available in C++20 as an extension. |
| 296 |
|
| 297 |
For padding Unicode strings the ``format`` library relies on the Unicode |
| 298 |
Standard. Libc++ retroactively updates the Unicode Standard in older C++ |
| 299 |
versions. This allows the library to have better estimates for newly introduced |
| 300 |
Unicode code points, without requiring the user to use the latest C++ version |
| 301 |
in their code base. |
| 302 |
|
| 303 |
In C++26 formatting pointers gained a type ``P`` and allows to use |
| 304 |
zero-padding. These options have been retroactively applied to C++20. |
| 305 |
|
| 306 |
Extensions to the C++23 modules ``std`` and ``std.compat`` |
| 307 |
---------------------------------------------------------- |
| 308 |
|
| 309 |
Like other major implementations, libc++ provides C++23 modules ``std`` and |
| 310 |
``std.compat`` in C++20 as an extension" |
| 311 |
|
| 312 |
Constant-initialized std::string |
| 313 |
-------------------------------- |
| 314 |
|
| 315 |
As an implementation-specific optimization, ``std::basic_string`` (``std::string``, |
| 316 |
``std::wstring``, etc.) may either store the string data directly in the object, or else store a |
| 317 |
pointer to heap-allocated memory, depending on the length of the string. |
| 318 |
|
| 319 |
As of C++20, the constructors are now declared ``constexpr``, which permits strings to be used |
| 320 |
during constant-evaluation time. In libc++, as in other common implementations, it is also possible |
| 321 |
to constant-initialize a string object (e.g. via declaring a variable with ``constinit`` or |
| 322 |
``constexpr``), but, only if the string is short enough to not require a heap allocation. Reliance |
| 323 |
upon this should be discouraged in portable code, as the allowed length differs based on the |
| 324 |
standard-library implementation and also based on whether the platform uses 32-bit or 64-bit |
| 325 |
pointers. |
| 326 |
|
| 327 |
.. code-block:: cpp |
| 328 |
|
| 329 |
// Non-portable: 11-char string works on 64-bit libc++, but not on 32-bit. |
| 330 |
constinit std::string x = "hello world"; |
| 331 |
|
| 332 |
// Prefer to use string_view, or remove constinit/constexpr from the variable definition: |
| 333 |
constinit std::string_view x = "hello world"; |
| 334 |
std::string_view y = "hello world"; |
| 335 |
|
| 336 |
.. _turning-off-asan: |
| 337 |
|
| 338 |
Turning off ASan annotation in containers |
| 339 |
----------------------------------------- |
| 340 |
|
| 341 |
``__asan_annotate_container_with_allocator`` is a customization point to allow users to disable |
| 342 |
`Address Sanitizer annotations for containers <https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow>`_ for specific allocators. This may be necessary for allocators that access allocated memory. |
| 343 |
This customization point exists only when ``_LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS`` Feature Test Macro is defined. |
| 344 |
|
| 345 |
For allocators not running destructors, it is also possible to `bulk-unpoison memory <https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning>`_ instead of disabling annotations altogether. |
| 346 |
|
| 347 |
The struct may be specialized for user-defined allocators. It is a `Cpp17UnaryTypeTrait <http://eel.is/c++draft/type.traits#meta.rqmts>`_ with a base characteristic of ``true_type`` if the container is allowed to use annotations and ``false_type`` otherwise. |
| 348 |
|
| 349 |
The annotations for a ``user_allocator`` can be disabled like this: |
| 350 |
|
| 351 |
.. code-block:: cpp |
| 352 |
|
| 353 |
#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS |
| 354 |
template <class T> |
| 355 |
struct std::__asan_annotate_container_with_allocator<user_allocator<T>> : std::false_type {}; |
| 356 |
#endif |
| 357 |
|
| 358 |
Why may I want to turn it off? |
| 359 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 360 |
|
| 361 |
There are a few reasons why you may want to turn off annotations for an allocator. |
| 362 |
Unpoisoning may not be an option, if (for example) you are not maintaining the allocator. |
| 363 |
|
| 364 |
* You are using allocator, which does not call destructor during deallocation. |
| 365 |
* You are aware that memory allocated with an allocator may be accessed, even when unused by container. |
| 366 |
|
| 367 |
Platform specific behavior |
| 368 |
========================== |
| 369 |
|
| 370 |
Windows |
| 371 |
------- |
| 372 |
|
| 373 |
The ``stdout``, ``stderr``, and ``stdin`` file streams can be placed in |
| 374 |
Unicode mode by a suitable call to ``_setmode()``. When in this mode, |
| 375 |
the sequence of bytes read from, or written to, these streams is interpreted |
| 376 |
as a sequence of little-endian ``wchar_t`` elements. Thus, use of |
| 377 |
``std::cout``, ``std::cerr``, or ``std::cin`` with streams in Unicode mode |
| 378 |
will not behave as they usually do since bytes read or written won't be |
| 379 |
interpreted as individual ``char`` elements. However, ``std::wcout``, |
| 380 |
``std::wcerr``, and ``std::wcin`` will behave as expected. |
| 381 |
|
| 382 |
Wide character stream such as ``std::wcin`` or ``std::wcout`` imbued with a |
| 383 |
locale behave differently than they otherwise do. By default, wide character |
| 384 |
streams don't convert wide characters but input/output them as is. If a |
| 385 |
specific locale is imbued, the IO with the underlying stream happens with |
| 386 |
regular ``char`` elements, which are converted to/from wide characters |
| 387 |
according to the locale. Note that this doesn't behave as expected if the |
| 388 |
stream has been set in Unicode mode. |