1.. _BuildingLibcxx: 2 3=============== 4Building libc++ 5=============== 6 7.. contents:: 8 :local: 9 10.. _build instructions: 11 12The instructions on this page are aimed at vendors who ship libc++ as part of an 13operating system distribution, a toolchain or similar shipping vehicules. If you 14are a user merely trying to use libc++ in your program, you most likely want to 15refer to your vendor's documentation, or to the general documentation for using 16libc++ :ref:`here <using-libcxx>`. 17 18.. warning:: 19 If your operating system already provides libc++, it is important to be careful 20 not to replace it. Replacing your system's libc++ installation could render it 21 non-functional. Use the CMake option ``CMAKE_INSTALL_PREFIX`` to select a safe 22 place to install libc++. 23 24 25The default build 26================= 27 28By default, libc++ and libc++abi are built as sub-projects of the LLVM project. 29This can be achieved with the usual CMake invocation: 30 31.. code-block:: bash 32 33 $ git clone https://github.com/llvm/llvm-project.git 34 $ cd llvm-project 35 $ mkdir build 36 $ cmake -G Ninja -S llvm -B build -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" # Configure 37 $ ninja -C build cxx cxxabi # Build 38 $ ninja -C build check-cxx check-cxxabi # Test 39 $ ninja -C build install-cxx install-cxxabi # Install 40 41.. note:: 42 See :ref:`CMake Options` below for more configuration options. 43 44After building the ``install-cxx`` and ``install-cxxabi`` targets, shared libraries 45for libc++ and libc++abi should now be present in ``<CMAKE_INSTALL_PREFIX>/lib``, and 46headers in ``<CMAKE_INSTALL_PREFIX>/include/c++/v1``. See :ref:`using an alternate 47libc++ installation <alternate libcxx>` for information on how to use this libc++ over 48the default one. 49 50In the default configuration, libc++ and libc++abi will be built using the compiler available 51by default on your system. It is also possible to bootstrap Clang and build libc++ with it. 52 53 54Bootstrapping build 55=================== 56 57It is also possible to build Clang and then build libc++ and libc++abi using that 58just-built compiler. This is the correct way to build libc++ when putting together 59a toolchain, or when the system compiler is not adequate to build libc++ (too old, 60unsupported, etc.). This type of build is also commonly called a "Runtimes build": 61 62.. code-block:: bash 63 64 $ mkdir build 65 $ cmake -G Ninja -S llvm -B build -DLLVM_ENABLE_PROJECTS="clang" \ # Configure 66 -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ 67 -DLLVM_RUNTIME_TARGETS="<target-triple>" 68 $ ninja -C build runtimes # Build 69 $ ninja -C build check-runtimes # Test 70 $ ninja -C build install-runtimes # Install 71 72 73Support for Windows 74=================== 75 76libcxx supports being built with clang-cl, but not with MSVC's cl.exe, as 77cl doesn't support the ``#include_next`` extension. Furthermore, VS 2017 or 78newer (19.14) is required. 79 80libcxx also supports being built with clang targeting MinGW environments. 81 82CMake + Visual Studio 83--------------------- 84 85Building with Visual Studio currently does not permit running tests. However, 86it is the simplest way to build. 87 88.. code-block:: batch 89 90 > cmake -G "Visual Studio 16 2019" -S libcxx -B build ^ 91 -T "ClangCL" ^ 92 -DLIBCXX_ENABLE_SHARED=YES ^ 93 -DLIBCXX_ENABLE_STATIC=NO ^ 94 -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO 95 > cmake --build build 96 97CMake + ninja (MSVC) 98-------------------- 99 100Building with ninja is required for development to enable tests. 101A couple of tests require Bash to be available, and a couple dozens 102of tests require other posix tools (cp, grep and similar - LLVM's tests 103require the same). Without those tools the vast majority of tests 104can still be ran successfully. 105 106If Git for Windows is available, that can be used to provide the bash 107shell by adding the right bin directory to the path, e.g. 108``set PATH=%PATH%;C:\Program Files\Git\usr\bin``. 109 110Alternatively, one can also choose to run the whole build in a MSYS2 111shell. That can be set up e.g. by starting a Visual Studio Tools Command 112Prompt (for getting the environment variables pointing to the headers and 113import libraries), and making sure that clang-cl is available in the 114path. From there, launch an MSYS2 shell via e.g. 115``C:\msys64\msys2_shell.cmd -full-path -mingw64`` (preserving the earlier 116environment, allowing the MSVC headers/libraries and clang-cl to be found). 117 118In either case, then run: 119 120.. code-block:: batch 121 122 > cmake -G Ninja -S libcxx -B build ^ 123 -DCMAKE_C_COMPILER=clang-cl ^ 124 -DCMAKE_CXX_COMPILER=clang-cl ^ 125 -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO 126 > ninja -C build cxx 127 > ninja -C build check-cxx 128 129If you are running in an MSYS2 shell and you have installed the 130MSYS2-provided clang package (which defaults to a non-MSVC target), you 131should add e.g. ``-DLIBCXX_TARGET_TRIPLE=x86_64-windows-msvc`` (replacing 132``x86_64`` with the architecture you're targeting) to the ``cmake`` command 133line above. This will instruct ``check-cxx`` to use the right target triple 134when invoking ``clang++``. 135 136Also note that if not building in Release mode, a failed assert in the tests 137pops up a blocking dialog box, making it hard to run a larger number of tests. 138 139CMake + ninja (MinGW) 140--------------------- 141 142libcxx can also be built in MinGW environments, e.g. with the MinGW 143compilers in MSYS2. This requires clang to be available (installed with 144e.g. the ``mingw-w64-x86_64-clang`` package), together with CMake and ninja. 145 146.. code-block:: bash 147 148 > cmake -G Ninja -S libcxx -B build \ 149 -DCMAKE_C_COMPILER=clang \ 150 -DCMAKE_CXX_COMPILER=clang++ \ 151 -DLIBCXX_HAS_WIN32_THREAD_API=ON \ 152 -DLIBCXX_CXX_ABI=libstdc++ \ 153 -DLIBCXX_TARGET_INFO="libcxx.test.target_info.MingwLocalTI" 154 > ninja -C build cxx 155 > cp /mingw64/bin/{libstdc++-6,libgcc_s_seh-1,libwinpthread-1}.dll lib 156 > ninja -C build check-cxx 157 158As this build configuration ends up depending on a couple other DLLs that 159aren't available in path while running tests, copy them into the same 160directory as the tested libc++ DLL. 161 162(Building a libc++ that depends on libstdc++ isn't necessarily a config one 163would want to deploy, but it simplifies the config for testing purposes.) 164 165.. _`libc++abi`: http://libcxxabi.llvm.org/ 166 167 168.. _CMake Options: 169 170CMake Options 171============= 172 173Here are some of the CMake variables that are used often, along with a 174brief explanation and LLVM-specific notes. For full documentation, check the 175CMake docs or execute ``cmake --help-variable VARIABLE_NAME``. 176 177**CMAKE_BUILD_TYPE**:STRING 178 Sets the build type for ``make`` based generators. Possible values are 179 Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio 180 the user sets the build type with the IDE settings. 181 182**CMAKE_INSTALL_PREFIX**:PATH 183 Path where LLVM will be installed if "make install" is invoked or the 184 "INSTALL" target is built. 185 186**CMAKE_CXX_COMPILER**:STRING 187 The C++ compiler to use when building and testing libc++. 188 189 190.. _libcxx-specific options: 191 192libc++ specific options 193----------------------- 194 195.. option:: LIBCXX_INSTALL_LIBRARY:BOOL 196 197 **Default**: ``ON`` 198 199 Toggle the installation of the library portion of libc++. 200 201.. option:: LIBCXX_INSTALL_HEADERS:BOOL 202 203 **Default**: ``ON`` 204 205 Toggle the installation of the libc++ headers. 206 207.. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL 208 209 **Default**: ``OFF`` 210 211 Build libc++ with assertions enabled. 212 213.. option:: LIBCXX_BUILD_32_BITS:BOOL 214 215 **Default**: ``OFF`` 216 217 Build libc++ as a 32 bit library. Also see `LLVM_BUILD_32_BITS`. 218 219.. option:: LIBCXX_ENABLE_SHARED:BOOL 220 221 **Default**: ``ON`` 222 223 Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or 224 `LIBCXX_ENABLE_STATIC` has to be enabled. 225 226.. option:: LIBCXX_ENABLE_STATIC:BOOL 227 228 **Default**: ``ON`` 229 230 Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or 231 `LIBCXX_ENABLE_STATIC` has to be enabled. 232 233.. option:: LIBCXX_LIBDIR_SUFFIX:STRING 234 235 Extra suffix to append to the directory where libraries are to be installed. 236 This option overrides `LLVM_LIBDIR_SUFFIX`. 237 238.. option:: LIBCXX_HERMETIC_STATIC_LIBRARY:BOOL 239 240 **Default**: ``OFF`` 241 242 Do not export any symbols from the static libc++ library. 243 This is useful when the static libc++ library is being linked into shared 244 libraries that may be used in with other shared libraries that use different 245 C++ library. We want to avoid exporting any libc++ symbols in that case. 246 247.. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL 248 249 **Default**: ``ON`` except on Windows when using MSVC. 250 251 This option can be used to enable or disable the filesystem components on 252 platforms that may not support them. For example on Windows when using MSVC. 253 254.. option:: LIBCXX_ENABLE_INCOMPLETE_FEATURES:BOOL 255 256 **Default**: ``ON`` 257 258 Whether to enable support for incomplete library features. Incomplete features 259 are new library features under development. These features don't guarantee 260 ABI stability nor the quality of completed library features. Vendors 261 shipping the library may want to disable this option. 262 263.. option:: LIBCXX_INSTALL_LIBRARY_DIR:PATH 264 265 **Default**: ``lib${LIBCXX_LIBDIR_SUFFIX}`` 266 267 Path where built libc++ libraries should be installed. If a relative path, 268 relative to ``CMAKE_INSTALL_PREFIX``. 269 270.. option:: LIBCXX_INSTALL_INCLUDE_DIR:PATH 271 272 **Default**: ``include/c++/v1`` 273 274 Path where target-agnostic libc++ headers should be installed. If a relative 275 path, relative to ``CMAKE_INSTALL_PREFIX``. 276 277.. option:: LIBCXX_INSTALL_INCLUDE_TARGET_DIR:PATH 278 279 **Default**: ``include/c++/v1`` or 280 ``include/${LLVM_DEFAULT_TARGET_TRIPLE}/c++/v1`` 281 282 Path where target-specific libc++ headers should be installed. If a relative 283 path, relative to ``CMAKE_INSTALL_PREFIX``. 284 285.. _libc++experimental options: 286 287libc++experimental Specific Options 288------------------------------------ 289 290.. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL 291 292 **Default**: ``ON`` 293 294 Build and test libc++experimental.a. 295 296.. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL 297 298 **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY`` 299 300 Install libc++experimental.a alongside libc++. 301 302 303.. _ABI Library Specific Options: 304 305ABI Library Specific Options 306---------------------------- 307 308.. option:: LIBCXX_CXX_ABI:STRING 309 310 **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``. 311 312 Select the ABI library to build libc++ against. 313 314.. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS 315 316 Provide additional search paths for the ABI library headers. 317 318.. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH 319 320 Provide the path to the ABI library that libc++ should link against. 321 322.. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL 323 324 **Default**: ``OFF`` 325 326 If this option is enabled, libc++ will try and link the selected ABI library 327 statically. 328 329.. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL 330 331 **Default**: ``ON`` by default on UNIX platforms other than Apple unless 332 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``. 333 334 This option generate and installs a linker script as ``libc++.so`` which 335 links the correct ABI library. 336 337.. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL 338 339 **Default**: ``OFF`` 340 341 Build and use the LLVM unwinder. Note: This option can only be used when 342 libc++abi is the C++ ABI library used. 343 344 345libc++ Feature Options 346---------------------- 347 348.. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL 349 350 **Default**: ``ON`` 351 352 Build libc++ with exception support. 353 354.. option:: LIBCXX_ENABLE_RTTI:BOOL 355 356 **Default**: ``ON`` 357 358 Build libc++ with run time type information. 359 360.. option:: LIBCXX_INCLUDE_TESTS:BOOL 361 362 **Default**: ``ON`` (or value of ``LLVM_INCLUDE_TESTS``) 363 364 Build the libc++ tests. 365 366.. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL 367 368 **Default**: ``ON`` 369 370 Build the libc++ benchmark tests and the Google Benchmark library needed 371 to support them. 372 373.. option:: LIBCXX_BENCHMARK_TEST_ARGS:STRING 374 375 **Default**: ``--benchmark_min_time=0.01`` 376 377 A semicolon list of arguments to pass when running the libc++ benchmarks using the 378 ``check-cxx-benchmarks`` rule. By default we run the benchmarks for a very short amount of time, 379 since the primary use of ``check-cxx-benchmarks`` is to get test and sanitizer coverage, not to 380 get accurate measurements. 381 382.. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING 383 384 **Default**:: ``""`` 385 386 **Values**:: ``libc++``, ``libstdc++`` 387 388 Build the libc++ benchmark tests and Google Benchmark library against the 389 specified standard library on the platform. On Linux this can be used to 390 compare libc++ to libstdc++ by building the benchmark tests against both 391 standard libraries. 392 393.. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING 394 395 Use the specified GCC toolchain and standard library when building the native 396 stdlib benchmark tests. 397 398.. option:: LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT:BOOL 399 400 **Default**: ``OFF`` 401 402 Pick the default for whether to constrain ABI-unstable symbols to 403 each individual translation unit. This setting controls whether 404 `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined by default -- 405 see the documentation of that macro for details. 406 407 408libc++ ABI Feature Options 409-------------------------- 410 411The following options allow building libc++ for a different ABI version. 412 413.. option:: LIBCXX_ABI_VERSION:STRING 414 415 **Default**: ``1`` 416 417 Defines the target ABI version of libc++. 418 419.. option:: LIBCXX_ABI_UNSTABLE:BOOL 420 421 **Default**: ``OFF`` 422 423 Build the "unstable" ABI version of libc++. Includes all ABI changing features 424 on top of the current stable version. 425 426.. option:: LIBCXX_ABI_NAMESPACE:STRING 427 428 **Default**: ``__n`` where ``n`` is the current ABI version. 429 430 This option defines the name of the inline ABI versioning namespace. It can be used for building 431 custom versions of libc++ with unique symbol names in order to prevent conflicts or ODR issues 432 with other libc++ versions. 433 434 .. warning:: 435 When providing a custom namespace, it's the users responsibility to ensure the name won't cause 436 conflicts with other names defined by libc++, both now and in the future. In particular, inline 437 namespaces of the form ``__[0-9]+`` are strictly reserved by libc++ and may not be used by users. 438 Doing otherwise could cause conflicts and hinder libc++ ABI evolution. 439 440.. option:: LIBCXX_ABI_DEFINES:STRING 441 442 **Default**: ``""`` 443 444 A semicolon-separated list of ABI macros to persist in the site config header. 445 See ``include/__config`` for the list of ABI macros. 446 447 448.. _LLVM-specific variables: 449 450LLVM-specific options 451--------------------- 452 453.. option:: LLVM_LIBDIR_SUFFIX:STRING 454 455 Extra suffix to append to the directory where libraries are to be 456 installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64`` 457 to install libraries to ``/usr/lib64``. 458 459.. option:: LLVM_BUILD_32_BITS:BOOL 460 461 Build 32-bits executables and libraries on 64-bits systems. This option is 462 available only on some 64-bits Unix systems. Defaults to OFF. 463 464.. option:: LLVM_LIT_ARGS:STRING 465 466 Arguments given to lit. ``make check`` and ``make clang-test`` are affected. 467 By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on 468 others. 469 470 471Using Alternate ABI libraries 472============================= 473 474In order to implement various features like exceptions, RTTI, ``dynamic_cast`` and 475more, libc++ requires what we refer to as an ABI library. Typically, that library 476implements the `Itanium C++ ABI <https://itanium-cxx-abi.github.io/cxx-abi/abi.html>`_. 477 478By default, libc++ uses libc++abi as an ABI library. However, it is possible to use 479other ABI libraries too. 480 481Using libsupc++ on Linux 482------------------------ 483 484You will need libstdc++ in order to provide libsupc++. 485 486Figure out where the libsupc++ headers are on your system. On Ubuntu this 487is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>`` 488 489You can also figure this out by running 490 491.. code-block:: bash 492 493 $ echo | g++ -Wp,-v -x c++ - -fsyntax-only 494 ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" 495 ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include" 496 #include "..." search starts here: 497 #include <...> search starts here: 498 /usr/include/c++/4.7 499 /usr/include/c++/4.7/x86_64-linux-gnu 500 /usr/include/c++/4.7/backward 501 /usr/lib/gcc/x86_64-linux-gnu/4.7/include 502 /usr/local/include 503 /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed 504 /usr/include/x86_64-linux-gnu 505 /usr/include 506 End of search list. 507 508Note that the first two entries happen to be what we are looking for. This 509may not be correct on all platforms. 510 511We can now run CMake: 512 513.. code-block:: bash 514 515 $ cmake -G Ninja -S llvm -B build \ 516 -DLLVM_ENABLE_PROJECTS="libcxx" \ 517 -DLIBCXX_CXX_ABI=libstdc++ \ 518 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" 519 $ ninja -C build install-cxx 520 521 522You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++`` 523above, which will cause the library to be linked to libsupc++ instead 524of libstdc++, but this is only recommended if you know that you will 525never need to link against libstdc++ in the same executable as libc++. 526GCC ships libsupc++ separately but only as a static library. If a 527program also needs to link against libstdc++, it will provide its 528own copy of libsupc++ and this can lead to subtle problems. 529 530Using libcxxrt on Linux 531------------------------ 532 533You will need to keep the source tree of `libcxxrt`_ available 534on your build machine and your copy of the libcxxrt shared library must 535be placed where your linker will find it. 536 537We can now run CMake like: 538 539.. code-block:: bash 540 541 $ cmake -G Ninja -S llvm -B build \ 542 -DLLVM_ENABLE_PROJECTS="libcxx" \ 543 -DLIBCXX_CXX_ABI=libcxxrt \ 544 -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src 545 $ ninja -C build install-cxx 546 547Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as 548clang is set up to link for libc++ linked to libsupc++. To get around this 549you'll have to set up your linker yourself (or patch clang). For example, 550 551.. code-block:: bash 552 553 $ clang++ -stdlib=libc++ helloworld.cpp \ 554 -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc 555 556Alternately, you could just add libcxxrt to your libraries list, which in most 557situations will give the same result: 558 559.. code-block:: bash 560 561 $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt 562 563.. _`libcxxrt`: https://github.com/libcxxrt/libcxxrt 564