| .vscode | ||
| src | ||
| .clang-format | ||
| .gitignore | ||
| CMakeLists.txt | ||
| LICENSE | ||
| README.md | ||
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.