Where are C++ Modules?

This article aims to summarize the current status as regards Modules and the import keyword with modern compilers, in particular accessing the Standard Library without the need for #include <iostream> and similar.

At the time of writing, all of the most popular C++ compilers (MSVC, Clang and g++) offer some (“experimental”) support for modules, as detailed below:

  1. MSVC needs to be installed using the Visual Studio Installer from the “Desktop development with C++” workload with the “C++ Modules for v143 build tools (x64/x86 – experimental)” option selected in order to be able to use import. The options /experimental:module, /std:c++latest and /MD all need to be specified to the cl compiler when using Visual Studio Code or the command line. More details here; the syntax to use in your code is: import std.core;
  2. Recent versions of Clang/LLVM support C++ Modules, including replacing the Standard Library headers, when invoked with the options -std=c++20, -stdlib=libc++ and -fmodules. Compiling from source is one way to access this functionality; the build flag LLVM_ENABLE_MODULES is required and the libcxx project needs to be built too. A rolling/testing distribution should provide these, but ensure you have installed the correct libc++ package. A list of issues is described on this page; the syntax to use in your code (slightly different from MSVC) is: import std;
  3. The GCC devel/c++-modules branch has now been folded into trunk; the new C++ modules page is here. The main compile option available to use is -fmodules-ts, however the C++ Standard Library is not available as an import at the time of writing, so only writing your own modules is supported.

It is clearly going to take a while for C++ modules to become universal, so the code on this site will likely use #include for the foreseeable future. The example code archive for the tutorial on this site includes both a “headers” and “modules” directory so you can choose which to experiment with (the “modules” versions are all able to be built by either MSVC or Clang/LLVM).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s