C++20: Module Interface Unit and Module Implementation Unit

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.

// math.cppm

export module math;

export int add(int fir, int sec){
    return fir + sec;

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.


Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.


    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.

    Mine:   clang++ -std=c++20 - -fmodules-ts -stdlib=libc++ -c math.cppm -Xclang -emit-module-interface -o math.pcm
    Roland: clang++ -std=c++20 - -fmodules-ts -stdlib=libc++ -c math.cpp -Xclang -emit-module-interface -o math.pcm

    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.

    // client.cpp
    import math;
    int main() {
       add(2000, 20);
    clang++ -std=c++2a -stdlib=libc++ -c math.cpp -Xclang -emit-module-interface -o math.pcm  // (1)
    clang++ -std=c++2a -stdlib=libc++ -fprebuilt-module-path=. client.cpp math.pcm -o client   // (2)
    1. 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.
    2. 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:

    module;                      // global module fragment
    #include <headers for libraries not modularized so far>
    export module math;          // module declartion 
    import <importing of other modules> 
    <non-exported declarations>  // names with only visibiliy inside the module
    export namespace math {
        <exported declarations>  // exported names 

    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

    // mathInterfaceUnit.ixx
    import std.core;                            
    export module math;       
    export namespace math {
        int add(int fir, int sec);
        int getProduct(const std::vector<int>& vec);
    • 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

    // mathImplementationUnit.cpp
    module math;
    import std.core;
    int add(int fir, int sec){
        return fir + sec;
    int getProduct(const std::vector<int>& vec) {
        return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
    • The module implementation unit contains non-exporting module declarations: module math;
    • A module can have more than one module implementation unit.

    Main Program

    // client3.cpp
    import std.core;
    import math;
    int main() {
        std::cout << std::endl;   
        std::cout << "math::add(2000, 20): " << math::add(2000, 20) << std::endl;
        std::vector<int> myVec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        std::cout << "math::getProduct(myVec): " << math::getProduct(myVec) << std::endl;
        std::cout << std::endl;
    •  From the user’s perspective, only the namespace math was added

    Building the Executable

    Manually building the executable includes a few steps.

    cl.exe /std:c++latest /c /experimental:module mathInterfaceUnit.ixx /EHsc /MD      // (1)
    cl.exe /std:c++latest /c /experimental:module mathImplementationUnit.cpp /EHsc /MD // (2)
    cl.exe /std:c++latest /c /experimental:module client3.cpp /EHsc /MD                // (3)
    cl.exe client3.obj mathInterfaceUnit.obj mathImplementationUnit.obj                // (4)
    1. Creates the object file mathInterfaceUnit.obj and the module interface file math.ifc.
    2. Creates the object file mathImplementationUnit.obj.
    3. Creates the object file client3.obj.
    4. 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:


    What’s next?

    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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Philipp Lenk, Hobsbawm, Charles-Jianye Chen, and Keith Jeffery.

    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.

    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++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,



    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *