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


Thanks to the module interface unit and the module implementation unit, you can separate the interface from the implementation when defining a module. Let me show, 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 me. 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 totally 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, 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  literally had the same Clang compiler and the same module definition. Character by character, I compared his command-line with mine, 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 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 structure of a module and also an idea, 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 such 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 previously 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.

For the Microsoft compiler, you have to specify the exception handling model (/EHsc) and the multithreading library (/MD).  Additionally, use the flag /std:c++latest.

Finally, here is the output of the program:


What's next?

In the next post, I extend my module math with new features. First, I import modules and export them in one unit; second, I use names that are only visible inside the module.

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, and Peter Ware.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, and Embarcadero Marketing.



I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)


Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.


Contact Me

Modernes C++,


Tags: modules, C++20


0 #1 Paul Topping 2020-09-17 17:22
In "Building the Executable", Step 1 compiles mathInterface.ixx but your description of it says it "Creates the object file mathInterfaceUnit.obj". Is this a typo? If not, where does "Unit" in the object file name come from?
0 #2 LP 2020-09-22 19:12
I've been using clang-10 since last fall, building modules with the .cppm suffix. Note that you need to omit the -fmodules-ts flag, as that causes clang to emulate an older modules implementation. You also need to use libc++, not libstdc++, anduse -std=c++2a (c++20 currently seems to ignore -fimplicit-modules). Oh, and the builtin module stuff means you need to use `import std;` rather than `import std.core` or similar.

Anyway, my complete clang command is

clang++ -stdlib=libc++ --std=c++2a -fbuiltin-module-map -fmodule-map-file=/usr/include/c++/v1/module.modul emap -fimplicit-modules -fPIC -fprebuilt-module-path=. --precompile -o math.pcm math.cppm

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 2892

Yesterday 40511

Week 60456

Month 227290

All 5524394

Currently are 248 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments