Modules are one of the four prominent features of C++20. They overcome the restrictions of header files and promise a lot: faster build-times, fewer violations of the One-Definition-Rule, less usage of the preprocessor. Today, I want to create a simple math module.
The Long History of Modules in C++
Modules may be older than you think. My short historic detour should give only an idea of how long it takes to get something such valuable into the C++ standard.
In 2004, Daveed Vandevoorde wrote the proposal N1736.pdf, describing the first-time module idea. It took until 2012 to get a dedicated Study Group (SG2, Modules) for modules. In 2017, Clang 5.0 and MSVC 19.1 provided the first implementation. One year later, the Modules TS (technical specification) was finalized. Around the same time, Google proposed the so-called ATOM (Another Take On Modules) proposal (P0947) for modules. In 2019, the Modules TS and the ATOM proposal were merged into the C++20 committee draft (N4842), the syntax I present in my posts to modules.
The C++ standardization process is democratic. Section Standardization gives you more information about the standard and the standardization process. The image to the right shows the various study groups.
Explaining modules from the user’s perspective is quite easy, but this will not hold for the implementer’s perspective. My plan for this post is to start with simple modules math and add more features to it as we go.
The math Modul
First, here is my first module:
The expression export module math is the module declaration. By putting export before the function adds, add is exported and can, therefore, be used by a consumer of my module.
import math imports the module math and makes the exported names in the module visible to the client.cpp. Before I build the module, let me say a few words about module declaration files.
Module Declaration Files
Did you notice the strange name of the module: math.ixx.
- cl.exe (Microsoft) uses the required extension ixx. The ixx stands for a module interface source.
- Clang uses the extensioncppm. cppm stands presumably for a cpp module declaration. Wrong!!! The documentation to Clang is misleading. Please stop using the cppm extension until you read my next post. Use the extension cpp. I assume you don’t want to make the identical Odyssey such as me.
- I don’t know of a GCC extension.
Compile the Module math
To compile the module, you must use a very current Clang, GCC, or cl.exe compiler. I go into this post with cl.exe on Windows. The Microsoft blog provides two excellent introductions to modules: Overview of modules in C++ and C++ Modules conformance improvements with MSVC in Visual Studio 2019 16.5. In contrast, the lack of introductions to the Clang and GCC compilers makes it quite challenging to use modules.
Here are more details about my used Microsoft compiler:
These are the steps to compile and use the module with the Microsoft compiler. I only show the minimal command line. With an older Microsoft compiler, you must use at least /std:cpplatest.
- Creates an obj file math.obj and an IFC file math.ifc. The IFC file contains the metadata description of the module interface. The binary format of the IFC is modeled after the Internal Program Representation by Gabriel Dos Reis and Bjarne Stroustrup (2004/2005).
- Creates the executable client.exe. Without the implicitly used math.ifc file from the first step, the linker can not find the module.
For obvious reasons, I will not show you the output of the program execution. Let me change this.
Global Module Fragment
The global module fragment is meant to compose module interfaces. It’s a place to use preprocessor directives such as #include so the module interface can compile. The module interface does not export the code in the global module fragment.
The second version of the module math supports the two functions add and getProduct.
I included the necessary headers between the global module fragment (line 1) and the module declaration (line 2).
The client imports the module math and uses its functionality:
Maybe, you don’t like to use a Standard Library Header anymore. Microsoft supports modules for all STL headers. Here is what I have found in the post “Using C++ Modules in Visual Studio 2017” from the Microsoft C++ team blog.
- C++ modules in Visual Studio 2017
std.regexprovides the content of the header
std.filesystemprovides the content of the header
std.memoryprovides the content of the header
std.threadingprovides the contents of headers
std.coreprovides everything else in the C++ Standard Library
Here are the modified versions of the interface file math2.ixx and the source file client2.cpp.
Both files use in line (1) the module std.core.
My first module math.ixx, math1.ixx, and math2.ixx defined its functionality in one file. In the next post, I will separate the module definition into a module interface unit and a module implementation unit.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,