7 .. warning:: Modules are an experimental feature. It has additional build
8 requirements and not all libc++ configurations are supported yet.
10 The work is still in an early development state and not
11 considered stable nor complete
13 This page contains information regarding C++23 module support in libc++.
14 There are two kinds of modules available in Clang
16 * `Clang specific modules <https://clang.llvm.org/docs/Modules.html>`_
17 * `C++ modules <https://clang.llvm.org/docs/StandardCPlusPlusModules.html>`_
19 This page mainly discusses the C++ modules. In C++20 there are also header units,
20 these are not part of this document.
25 The module sources are stored in ``.cppm`` files. Modules need to be available
26 as BMIs, which are ``.pcm`` files for Clang. BMIs are not portable, they depend
27 on the compiler used and its compilation flags. Therefore there needs to be a
28 way to distribute the ``.cppm`` files to the user and offer a way for them to
29 build and use the ``.pcm`` files. It is expected this will be done by build
30 systems in the future. To aid early adaptor and build system vendors libc++
31 currently ships a CMake project to aid building modules.
33 .. note:: This CMake file is intended to be a temporary solution and will
34 be removed in the future. The timeline for the removal depends
35 on the availability of build systems with proper module support.
41 * Running tests using the ``std`` and ``std.compat`` module
42 * Using the ``std`` and ``std.compat`` module in external projects
43 * The following "parts disabled" configuration options are supported
45 * ``LIBCXX_ENABLE_LOCALIZATION``
46 * ``LIBCXX_ENABLE_WIDE_CHARACTERS``
47 * ``LIBCXX_ENABLE_THREADS``
48 * ``LIBCXX_ENABLE_FILESYSTEM``
49 * ``LIBCXX_ENABLE_RANDOM_DEVICE``
50 * ``LIBCXX_ENABLE_UNICODE``
51 * ``LIBCXX_ENABLE_EXCEPTIONS`` [#note-no-windows]_
53 * A C++20 based extension
57 .. [#note-no-windows] This configuration will probably not work on Windows
58 due to hard-coded compilation flags.
60 Some of the current limitations
61 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 * There is no official build system support, libc++ has experimental CMake support
64 * Requires CMake 3.26 for C++20 support
65 * Requires CMake 3.26 for C++23 support
66 * Requires CMake 3.27 for C++26 support
69 * The path to the compiler may not be a symlink, ``clang-scan-deps`` does
70 not handle that case properly
71 * Libc++ is not tested with modules instead of headers
73 * Including headers after importing the ``std`` module may fail. This is
74 hard to solve and there is a work-around by first including all headers
75 `bug report <https://github.com/llvm/llvm-project/issues/61465>`__.
82 * Currently the tests only test with modules enabled, but do not import
83 modules instead of headers. When converting tests to using modules there
84 are still failures. These are under investigation.
86 * It has not been determined how to fully test libc++ with modules instead
91 * Some concepts do not work properly
92 `bug report <https://github.com/llvm/llvm-project/issues/62943>`__.
95 Using in external projects
96 ==========================
98 Users need to be able to build their own BMI files.
100 .. note:: The requirements for users to build their own BMI files will remain
101 true for the foreseeable future. For now this needs to be done manually.
102 Once libc++'s implementation is more mature we will reach out to build
103 system vendors, with the goal that building the BMI files is done by
106 Currently there are two ways to build modules
108 * Use a local build of modules from the build directory. This requires
109 Clang 17 or later and CMake 3.26 or later.
111 * Use the installed modules. This requires Clang 18.1.2 or later and
112 a recent build of CMake. The CMake changes will be part of CMake 3.30. This
113 method requires you or your distribution to enable module installation.
115 Using the local build
116 ~~~~~~~~~~~~~~~~~~~~~
120 $ git clone https://github.com/llvm/llvm-project.git
123 $ cmake -G Ninja -S runtimes -B build -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
126 The above ``build`` directory will be referred to as ``<build>`` in the
127 rest of these instructions.
129 This is a small sample program that uses the module ``std``. It consists of a
130 ``CMakeLists.txt`` and a ``main.cpp`` file.
134 import std; // When importing std.compat it's not needed to import std.
138 std::cout << "Hello modular world\n";
139 ::printf("Hello compat modular world\n");
142 .. code-block:: cmake
144 cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
150 # Set language version used
153 set(CMAKE_CXX_STANDARD 23)
154 set(CMAKE_CXX_STANDARD_REQUIRED YES)
155 set(CMAKE_CXX_EXTENSIONS OFF)
158 # Enable modules in CMake
161 # This is required to write your own modules in your project.
162 if(CMAKE_VERSION VERSION_LESS "3.28.0")
163 if(CMAKE_VERSION VERSION_LESS "3.27.0")
164 set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
166 set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "aa1f7df0-828a-4fcd-9afc-2dc80491aca7")
168 set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
170 cmake_policy(VERSION 3.28)
174 # Import the modules from libc++
177 include(FetchContent)
178 FetchContent_Declare(
180 URL "file://${LIBCXX_BUILD}/modules/c++/v1/"
181 DOWNLOAD_EXTRACT_TIMESTAMP TRUE
184 FetchContent_MakeAvailable(std)
191 add_dependencies(main std.compat)
192 target_link_libraries(main std.compat)
198 Building this project is done with the following steps, assuming the files
199 ``main.cpp`` and ``CMakeLists.txt`` are copied in the current directory.
204 $ cmake -G Ninja -S . -B build -DCMAKE_CXX_COMPILER=<path-to-compiler> -DLIBCXX_BUILD=<build>
208 .. warning:: ``<path-to-compiler>`` should point point to the real binary and
211 .. warning:: When using these examples in your own projects make sure the
212 compilation flags are the same for the ``std`` module and your
213 project. Some flags will affect the generated code, when these
214 are different the module cannot be used. For example using
215 ``-pthread`` in your project and not in the module will give
218 ``error: POSIX thread support was disabled in PCH file but is currently enabled``
220 ``error: module file _deps/std-build/CMakeFiles/std.dir/std.pcm cannot be loaded due to a configuration mismatch with the current compilation [-Wmodule-file-config-mismatch]``
223 Using the installed modules
224 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
226 CMake has added experimental support for importing the Standard modules. This
227 is available in the current nightly builds and will be part of the 3.30
228 release. Currently CMake only supports importing the Standard modules in C++23
229 and later. Enabling this for C++20 is on the TODO list of the CMake
232 The example uses the same ``main.cpp`` as above. It uses the following
235 .. code-block:: cmake
237 # This requires a recent nightly build.
238 # This will be part of CMake 3.30.0.
239 cmake_minimum_required(VERSION 3.29.0 FATAL_ERROR)
241 # Enables the Standard module support. This needs to be done
242 # before selecting the languages.
243 set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
244 set(CMAKE_CXX_MODULE_STD ON)
251 # Set language version used
254 set(CMAKE_CXX_STANDARD 23)
255 set(CMAKE_CXX_STANDARD_REQUIRED YES)
256 # Currently CMake requires extensions enabled when using import std.
257 # https://gitlab.kitware.com/cmake/cmake/-/issues/25916
258 # https://gitlab.kitware.com/cmake/cmake/-/issues/25539
259 set(CMAKE_CXX_EXTENSIONS ON)
267 Building this project is done with the following steps, assuming the files
268 ``main.cpp`` and ``CMakeLists.txt`` are copied in the current directory.
273 $ cmake -G Ninja -S . -B build -DCMAKE_CXX_COMPILER=<path-to-compiler> -DCMAKE_CXX_FLAGS=-stdlib=libc++
277 .. warning:: ``<path-to-compiler>`` should point point to the real binary and
280 If you have questions about modules feel free to ask them in the ``#libcxx``
281 channel on `LLVM's Discord server <https://discord.gg/jzUbyP26tQ>`__.
283 If you think you've found a bug please it using the `LLVM bug tracker
284 <https://github.com/llvm/llvm-project/issues>`_. Please make sure the issue
285 you found is not one of the known bugs or limitations on this page.