UsingLibcxx.rst 13 KB

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