Thanks to the module interface unit and implementation unit, you can separate the interface from the implementation when defining a module. Let me show you how.
As promised in my last post, C++20: A Simple math Modul, I want to make a short detour on my Clang Odyssee. My detour is a compact refresher to all I wrote in the referred post.
My Clang Odyssey
First, I don’t want to blame anyone but myself. Based on talks from Boris Kolpackov “Building C++ Modules” at the CppCon 2017 or Corentin Jabot “Modules are not a tooling opportunity” I had the impression that the vendors suggested the following extensions for their module definition:
- Windows: ixx
- Clang: cppm
- GCC: no suggestion
In the case of the Clang compiler, I was wrong. This is my simple math module, which I tried to compile with the Clang compiler.
I tried to compile the module with Clang 9 and Clang 10 on Microsoft and Linux. I tried to compile it with the brand-new Clang 11 compiler, built from the sources. Here is one of my many tries.
This command line should create the module math.pcm. I specified in the command line -std=c++20 -fmodules-ts and the error message said: module interface compilation requires ‘-std=c++20′ or ‘-fmodules-ts’. I made all variations of the two flags, added the global module fragment to the module definition, and invoked the Clang compiler with additional flags, but the result was always the same.
Then I asked Arthur O’Dwyer and Roland Bock for their help. For Arthur, modules worked fine with Clang: “Hello World with C++2a modules“. Roland rebuilt its Clang 11, and it worked with my module definition.
Roland and I had the same Clang compiler and the exact module definition. I compared his command-line with mine, character by character, and I noticed something.
Roland gave his module math.cpp cpp, and so did Arthur. Don’t give your module definition the suffix cppm.
Now, compiling and using the module was straightforward.
To end this Odyssey, here is the client.cpp file and a few words to the necessary flags for the Clang command line.
- Creates the module math.pcm. The suffix pcm stands for the precompiled module. The flag combination -Xclang -emit-module-interface is necessary for creating the precompiled module.
- Creates the executable client, which uses the module math.pcm. You have to specify the path to the module with the flag -fprebuilt-module-path.
The module math was straightforward. Let’s be a bit more sophisticated.
Guideline for a Module Structure
Here is the first guideline for a module structure:
This guideline serves two purposes. It gives you a simplified module structure and an idea of what I’m going to write about. So, what’s new in this module structure?
- You can import modules. The imported modules have module linkage and are not visible outside the module. This observation also applies to the non-exported declarations.
- I put the exported names in namespace math, which has the same name as the module.
- The module has only declared names. Let’s write about the separation of the interface and the implementation of a module.
Module Interface Unit and Module Implementation Unit
According to the mentioned guideline, I want to refactor the final version of module math from the previous post C++20: A Simple math Modul.
Module Interface Unit
- The module interface unit contains the exporting module declaration: export module math.
- The names add, and getProduct are exported.
- A module can have only one module interface unit.
Module Implementation Unit
- The module implementation unit contains non-exporting module declarations: module math;
- A module can have more than one module implementation unit.
- From the user’s perspective, only the namespace math was added.
Building the Executable
Manually building the executable includes a few steps.
- Creates the object file mathInterfaceUnit.obj and the module interface file math.ifc.
- Creates the object file mathImplementationUnit.obj.
- Creates the object file client3.obj.
- Creates the executable client3.exe.
Specify the exception handling model (/EHsc) and the multithreading library (/MD) for the Microsoft compiler. Additionally, use the flag /std:c++latest.
Finally, here is the output of the program:
I will extend my module math in the next post with new features. First, I import and export modules in one unit; second, I use names only visible inside the module.
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, and Bhavith C Achar.
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|
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,