Minimal example of using C++20 modules with cmake
Find a file
2024-06-23 22:56:12 +02:00
.vscode Initial Import 2024-02-28 20:48:41 +01:00
src GCC 14.1 supported now 2024-06-23 22:56:12 +02:00
.clang-format Initial Import 2024-02-28 20:48:41 +01:00
.gitignore Initial Import 2024-02-28 20:48:41 +01:00
CMakeLists.txt GCC 14.1 supported now 2024-06-23 22:56:12 +02:00
LICENSE Initial Import 2024-02-28 20:48:41 +01:00
README.md GCC 14.1 supported now 2024-06-23 22:56:12 +02:00

Minimal C++ Modules Example with CMake

This repo contains a very simple example of using C++ modules (first introduced in C++20) with CMake and the clang compiler.

It contains of an application target (ExampleExecutable) that makes use of modules from the (statically linked) library target ExampleLibrary.

Obligatory this works on my machine. No idea if it'll work on yours.

Building

To build this project, use the standard cmake commands in the repo directory:

cmake -G Ninja -S . -B build
cmake --build build

Note that I specify Ninja as the generator, since it's the only one that supports modules on Linux systems at the time of writing.
If you want to build this on Windows, you probably want to use Visual Studio 17 2022 instead.

If all goes well you should have a binary that you can run:

$ ./build/bin/ExampleExecutable
Moin julian

Toolchain requirements

You need to have

  • CMake version 3.28 or newer
  • A suitable generator:
    • ninja version 1.11 or newer
    • Visual Studio 2022 or newer
  • A suitable compiler:
    • clang version 16.0 or newer
    • GCC version 14.1 or newer
    • MSVC toolsets 14.34 (Visual Studio 17.4) or newer

Limitations

When I experimented with this, I could not get clang to compile when trying to import, rather than include, standard library components.
#include <iostream> should be the same as import <iostream>;, as written on clang's official documentation.

This is a limitation of cmake, header units or import std; is not supported... yet.

Once this changes I'll update this repo. If I forget feel free to yell at me.

Also the clang language server I used with the clang visual studio code extension did not work smoothly for me. Manually restarting it did help a lot.

License

good luck with that shit