Skip to content
Public template

About

A minimal C++23 modules template with Clang, libc++, CMake, Ninja and clangd, with working import std and VS Code IntelliSense on Ubuntu/WSL.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

C++23 Modules + import std with Clang, CMake and VS Code

Tested: Ubuntu 24.04 / WSL · Clang 21 · CMake 4.4+

The included clang21 preset targets Ubuntu/WSL. Other distributions may require adjusting compiler and libc++ module metadata paths.

A minimal C++23 project configured to use modern C++ modules with:

  • Clang 21
  • libc++ 21
  • CMake
  • Ninja
  • import std;
  • clangd IntelliSense in VS Code

The setup also works as a base for user-defined C++ modules such as:

export module helloworld;

import std;

export void hello() 
{
    std::cout << "Hello, World!\n";
}

and:

import helloworld;

CMake supports dependency scanning for C++ modules and automatically determines the order in which module interfaces and their users must be compiled.

The additional libc++.modules.json configuration in this repository is specifically required for the standard library module (import std).

Project Structure

. ├── helloworld.cppm # user-defined C++ module ├── my_program.cpp # imports std and helloworld ├── CMakeLists.txt ├── CMakePresets.json └── .vscode/

Why?

Modern C++ allows replacing many traditional includes:

#include <iostream>
#include <vector>
#include <print>

with:

import std;

Modules can improve dependency management and compilation architecture and avoid many problems associated with textual header inclusion.

Unfortunately, support across compilers, build systems and IDEs is still maturing. This repository provides a working Clang + libc++ + CMake + clangd configuration, tested on Ubuntu 24.04 / WSL.

Requirements

This repository is tested with:

Tool Version
Clang 21
clangd 21
libc++ 21
CMake 4.4+
Ninja 1.11+

Check your installation:

clang++-21 --version
clangd-21 --version
cmake --version
ninja --version

CMake officially supports module dependency scanning with Clang 16+, while import std is supported with Clang 18.1.2+ and libc++/libstdc++. Ninja 1.11+ is required by CMake's Ninja module support.

Ubuntu

Install LLVM 21 using the official LLVM installer:

wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 21

Then install the remaining tools:

sudo apt install \
    clang-tools-21 \
    clangd-21 \
    libc++-21-dev \
    libc++abi-21-dev \
    ninja-build

You also need CMake 4.4 or newer. Check the currently installed version with:

cmake --version

See the official CMake downloads if your distribution provides an older version.

Ubuntu libc++ workaround

On Ubuntu the libc++ module metadata may resolve from:

/lib/x86_64-linux-gnu/libc++.modules.json

while std.cppm actually lives under:

/usr/lib/llvm-21/share/libc++/v1/std.cppm

This can cause CMake to incorrectly search for:

/lib/share/libc++/v1/std.cppm

The included CMake preset works around this by explicitly setting:

CMAKE_CXX_STDLIB_MODULES_JSON=/usr/lib/llvm-21/lib/libc++.modules.json

Building

The complete toolchain configuration is stored in CMakePresets.json, so no long CMake command is necessary.

Configure:

cmake --preset clang21

Build:

cmake --build --preset clang21

Run:

./build-clang/MyProgram

Expected output:

Hello, World!
Hello, World!

VS Code

Install the official LLVM clangd extension.

The repository configures clangd to use:

build-clang/compile_commands.json

generated by CMake.

Microsoft C/C++ IntelliSense is disabled because its current support for import std may produce false diagnostics.

This gives working compilation, completion, diagnostics and navigation for C++23 code using import std.

About

A minimal C++23 modules template with Clang, libc++, CMake, Ninja and clangd, with working import std and VS Code IntelliSense on Ubuntu/WSL.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages