|
29 | 29 | \frametitlecpp[20]{Writing modules} |
30 | 30 | \begin{exampleblock}{ratio.cpp} |
31 | 31 | \begin{cppcode*}{} |
32 | | - export module math; |
| 32 | + export module math; // module declaration |
33 | 33 | import <string>; |
34 | 34 | namespace utils { // ns. and module names orthogonal |
35 | | - export struct Ratio { float num, denom; }; |
| 35 | + export struct Ratio { int num, denom; }; |
36 | 36 | export std::string toString(const Ratio& r) { ... } |
37 | 37 | void normalize(Ratio& r) { ... } // not exported |
38 | 38 | } |
|
112 | 112 | \begin{exampleblock}{ratio.cpp} |
113 | 113 | \begin{cppcode*}{} |
114 | 114 | export module math; |
115 | | - struct Ratio { float num, denom; }; |
| 115 | + struct Ratio { int num, denom; }; |
116 | 116 | export Ratio golden() { ... } // golden is visible |
117 | 117 | \end{cppcode*} |
118 | 118 | \end{exampleblock} |
119 | | - \begin{exampleblock}{main.cpp} |
| 119 | + \begin{exampleblockGB}{main.cpp}{https://godbolt.org/z/n95rdxPdos}{Modules example} |
120 | 120 | \begin{cppcode*}{} |
121 | 121 | import math; |
122 | 122 | import <iostream>; |
|
129 | 129 | R r2{1.f, 3.f}; // OK |
130 | 130 | } |
131 | 131 | \end{cppcode*} |
132 | | - \end{exampleblock} |
| 132 | + \end{exampleblockGB} |
133 | 133 | \end{frame} |
134 | 134 |
|
135 | 135 | \begin{frame}[fragile] |
|
191 | 191 | \end{block} |
192 | 192 | \begin{exampleblock}{Interface} |
193 | 193 | \begin{cppcode*}{} |
194 | | - export module math; |
| 194 | + export module math; // Module interface unit declaration |
195 | 195 |
|
196 | 196 | export struct S { ... }; |
197 | 197 | export S f(); |
|
201 | 201 | \begin{cppcode*}{} |
202 | 202 | module; |
203 | 203 | #include <cassert> |
204 | | - module math; |
| 204 | + module math; // Module implementation unit |
205 | 205 | import <string>; |
206 | 206 |
|
207 | 207 | S f() { ... } |
|
348 | 348 | % see: Minimal module support for the standard library http://wg21.link/P2412 |
349 | 349 | \begin{block}{Standard library modules} |
350 | 350 | \begin{itemize} |
351 | | - \item Use \cppinline|import std;| for the entire C++ standard library. Basically everything in namespace \cppinline|std|. |
| 351 | + \item Use \cppinline|import std;| for the entire C++ standard library. |
352 | 352 | \begin{itemize} |
353 | 353 | \item E.g.\ \cppinline{memcpy(a, b, n)}, \cppinline{sqrt(val)}, \cppinline{uint32_t} |
354 | 354 | \item Note that you need to prefix with \cppinline{std::} |
| 355 | + \item Currently, support is not great (see later) |
355 | 356 | \end{itemize} |
356 | | - \item Use \cppinline|import std.compat;| for the entire C and C++ standard library |
| 357 | + \item \cppinline|import std.compat;| for the C and C++ |
357 | 358 | \item No macros are exported. For these, you still need to include the corresponding headers. |
358 | 359 | \begin{cppcode*}{} |
359 | 360 | import std; // everything C++ |
360 | 361 | #include <cassert> // for assert() |
361 | 362 | #include <climits> // for e.g. CHAR_BIT |
362 | 363 | \end{cppcode*} |
363 | | - \item Definition of further, smaller modules of the standard library is in progress (e.g.\ fundamentals, containers, networking, etc.) |
| 364 | + \item Definition of further, smaller modules of the standard library is in progress |
364 | 365 | \end{itemize} |
365 | 366 | \end{block} |
366 | 367 | \end{frame} |
|
405 | 406 | \frametitlecpp[20]{Automatic include to import translation} |
406 | 407 | \begin{block}{Importable headers} |
407 | 408 | \begin{itemize} |
408 | | - \item A header file which can successfully be either included or imported is called ``importable'' |
| 409 | + \item A header file which can either be included or imported is called ``importable'' |
409 | 410 | \begin{itemize} |
410 | 411 | \item I.e.\ the header does not require setup of preprocessor state before inclusion |
411 | 412 | \end{itemize} |
|
607 | 608 | \frametitlecpp[20]{Build system} |
608 | 609 | \begin{block}{Status} |
609 | 610 | \begin{itemize} |
610 | | - \item Dependency scanner since clang 16: \texttt{clang-scan-deps} |
611 | | - \item Experimental module support in cmake since 3.26, uses \texttt{clang-scan-deps}, many fixes in later versions % see: https://discourse.cmake.org/t/c-20-modules-update/7330/24 and https://youtu.be/c563KgO-uf4?si=HUpnSivtDxvBoTr3&t=3592 |
612 | | - \item MSBuild (Windows) fully handles dependency scanning |
613 | | - \item \cppinline{g++ a.cpp b.cpp ...} ``just works'', must be one line though |
614 | | - \item \href{https://wg21.link/p1602}{Experimental extensions to GNU make} exist to grow dependency graph on the fly while modules are discovered |
615 | | - \item Header units unsupported in all build systems (May 2023) % See talk: https://youtu.be/_LGR0U5Opdg?si=yQoCYD2yGFhi_vs6&t=3768 |
616 | | - \item C++23: \cppinline{import std;} supported in MSVC, partially in clang |
| 611 | + \item Build systems: |
617 | 612 | \begin{itemize} |
618 | | - \item GCC/clang/MSVC also plan support in C++20 % see: https://github.com/microsoft/STL/issues/3945 |
| 613 | + \item Dependency scanner since clang 16: \texttt{clang-scan-deps} |
| 614 | + \item Cmake support in v3.28 (\href{https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html}{documentation}) |
| 615 | + \item MSBuild (Windows) fully handles dependency scanning |
| 616 | + \item \href{https://wg21.link/p1602}{Experimental extensions to GNU make} |
619 | 617 | \end{itemize} |
620 | | - \end{itemize} |
621 | | - \end{block} |
622 | | -\end{frame} |
623 | | - |
624 | | -\begin{frame}[fragile] |
625 | | - \frametitlecpp[20]{Building modules (g++)} |
626 | | - \begin{block}{Case study: g++} |
627 | | - \begin{itemize} |
628 | | - \item BMI is called CMI (compiled module interfaces) |
629 | | - \item By default, g++ caches CMIs in subdirectory \texttt{./gcm.cache} |
630 | | - \item Uses a \href{https://wg21.link/P1184}{module mapper} to resolve imports, which specifies a protocol and uses a central server for module maintenance |
631 | | - \item Each module needs to be built before it can be imported |
632 | | - \begin{itemize} |
633 | | - \item {\footnotesize \cppinline{g++ -std=c++20 -fmodules-ts -c ratio.cpp -o ratio.o}} |
634 | | - \item Generates \texttt{ratio.o} and \texttt{./gcm.cache/ratio.gcm} (CMI) |
635 | | - \end{itemize} |
636 | | - \item Each header unit needs to be built before it can be imported |
| 618 | + \item \cppinline{g++ a.cpp b.cpp ...} ``just works'', must be one line though |
| 619 | + \item C++23: \cppinline{import std;} supported in MSVC, partially in clang and gcc |
| 620 | + \begin{itemize} |
| 621 | + \item The standard library module currently has to be compiled manually (\href{https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=7db55c0ba1baaf0e323ef7f9ef8c9cda077d40e9}{see gcc commit in 2024}, |
| 622 | + \href{https://0xstubs.org/using-the-c23-std-module-with-gcc-15/}{gcc-15 recipe}, \href{https://0xstubs.org/using-the-c23-std-module-with-clang-18/}{clang-18 recipe}) |
| 623 | + \item GCC/clang/MSVC also plan support in C++20 % see: https://github.com/microsoft/STL/issues/3945 |
| 624 | + \end{itemize} |
| 625 | + \item Header units unsupported (May 2023) % See talk: https://youtu.be/_LGR0U5Opdg?si=yQoCYD2yGFhi_vs6&t=3768 |
637 | 626 | \begin{itemize} |
638 | | - \item {\footnotesize \cppinline{g++ -std=c++20 -fmodules-ts -x c++-system-header vector}} |
639 | | - \item Generates e.g.\ \texttt{./gcm.cache/usr/include/c++/11/vector.gcm} (CMI) |
640 | | - \item {\footnotesize \cppinline{g++ -std=c++20 -fmodules-ts -x c++-header ratio.h}} |
641 | | - \item Generates e.g.\ \texttt{./gcm.cache/,/ratio.h.gcm} (CMI) |
| 627 | + \item Standard library header units possible in gcc11 and MSVC (2025) |
642 | 628 | \end{itemize} |
643 | 629 | \end{itemize} |
644 | 630 | \end{block} |
|
658 | 644 | \begin{exampleblock}{Guidance for tomorrow} |
659 | 645 | \begin{itemize} |
660 | 646 | \item Start writing modules when your users have \cpp20 |
661 | | - \item Maybe import standard headers when \cpp20 is available |
| 647 | + \item Maybe import standard headers when compilers fully support \cpp20 |
662 | 648 | \item Start using \cppinline{import std;} when available |
663 | 649 | \item Future of header units unclear (implementation difficulties) |
664 | 650 | \end{itemize} |
|
0 commit comments