Skip to content

mantkiew/hello-cxx-modules

Repository files navigation

Example of using C++ modules with Clang and CMake on Linux

An experiment with Ubuntu 24.04, CMake 3.30, and Clang 18 showing:

  1. import std;
  2. export module and import module for hello_lib

According to cmake-cxxmodules(7), import std Support, using Clang 18.1.2 or later is necessary (g++-14 does not support import std;).

Conventions

execute as sudo execute as a user
# command
output
$ command
output

Installing Ninja

Ninja 1.11 or newer is required according to cmake-cxxmodules(7), Generator Support. GNU make is not supported.

# apt-get install ninja-build

Installing CMake 3.30.0

# snap install cmake
$ cmake --version
cmake version 3.30.0
...

Installing Clang and libc++

# apt-get install clang libc++-dev
# clang++ --version
Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Configuring toolchain

It is important to set -stdlib=libc++ for both the compiler and the linker so that GNU libstdc++ is not used. Otherwise the following error occurs:

The "CXX_MODULE_STD" property on the target "hello_lib" requires that the
   "__CMAKE::CXX23" target exist, but it was not provided by the toolchain.
   Reason:
 
     Only `libc++` is supported

The compiler and linker flags can be set in a toolchain file clang-toolchain.cmake.

Building from command line

Configure

$ cd hello-cxx-modules
$ cmake --toolchain clang-toolchain.cmake \
        -G Ninja \
        -DCMAKE_BUILD_TYPE=MinSizeRel \
        -S . \
        -B build
-- The CXX compiler identification is Clang 18.1.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
CMake Warning (dev) at /snap/cmake/1403/share/cmake-3.30/Modules/Compiler/CMakeCommonCompilerMacros.cmake:248 (cmake_language):
  CMake's support for `import std;` in C++23 and newer is experimental.  It
  is meant only for experimentation and feedback to CMake developers.
Call Stack (most recent call first):
  /snap/cmake/1403/share/cmake-3.30/Modules/CMakeDetermineCompilerSupport.cmake:113 (cmake_create_cxx_import_std)
  /snap/cmake/1403/share/cmake-3.30/Modules/CMakeTestCXXCompiler.cmake:83 (CMAKE_DETERMINE_COMPILER_SUPPORT)
  CMakeLists.txt:13 (project)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Detecting CXX compile features - done
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /.../hello-cxx-modules/build

Build

$ cmake --build build --target all
[4/14   7% :: 0.058] Scanning /.../hello-cxx-modules/hello.cxx for CXX dependencies
[4/14  14% :: 0.060] Scanning /.../hello-cxx-modules/hello_lib.cxx for CXX dependencies
[4/14  21% :: 0.089] Scanning /usr/lib/llvm-18/share/libc++/v1/std.compat.cppm for CXX dependencies
[4/14  28% :: 0.188] Scanning /usr/lib/llvm-18/share/libc++/v1/std.cppm for CXX dependencies
[5/14  35% :: 0.195] Generating CXX dyndep file CMakeFiles/__cmake_cxx23.dir/CXX.dd
[7/14  42% :: 0.202] Generating CXX dyndep file CMakeFiles/hello_lib.dir/CXX.dd
[8/14  50% :: 0.208] Generating CXX dyndep file CMakeFiles/hello.dir/CXX.dd
[8/14  57% :: 3.679] Building CXX object CMakeFiles/__cmake_cxx23.dir/usr/lib/llvm-18/share/libc++/v1/std.cppm.o
[10/14  64% :: 3.913] Building CXX object CMakeFiles/hello_lib.dir/hello_lib.cxx.o
[11/14  71% :: 3.980] Building CXX object CMakeFiles/hello.dir/hello.cxx.o
[11/14  78% :: 4.075] Building CXX object CMakeFiles/__cmake_cxx23.dir/usr/lib/llvm-18/share/libc++/v1/std.compat.cppm.o
[12/14  85% :: 4.136] Linking CXX static library lib__cmake_cxx23.a
[13/14  92% :: 4.196] Linking CXX static library libhello_lib.a
[14/14 100% :: 4.268] Linking CXX executable hello

Execute

$ build/hello 
Hello World! My name is Michał

Check dependencies

$ ldd build/hello
	linux-vdso.so.1 (0x00007fff96cdb000)
	libc++.so.1 => /lib/x86_64-linux-gnu/libc++.so.1 (0x00007f51ea2b4000)
	libc++abi.so.1 => /lib/x86_64-linux-gnu/libc++abi.so.1 (0x00007f51ea278000)
	libunwind.so.1 => /lib/x86_64-linux-gnu/libunwind.so.1 (0x00007f51ea26a000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f51ea181000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f51ea154000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f51e9f40000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f51ea3cb000)

Building from VSCode

Install CMake and CMake Tools extensions

VSCode does not work with snap version of CMake out-of-the box. Set CMake Path to /snap/cmake/current/bin/cmake in "CMake Tools Extension Settings" or in settings.json:

"cmake.cmakePath": "/snap/cmake/current/bin/cmake"

Configure the Clang kit to use the toolchain file

See .vscode/cmake-kits.json.

Configure and build

Ensure the kit Clang 18.1.3 x86_64-pc-linux-gnu is selected in Configure

CMake tool

Build as usual.

References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published