C++ Modules Usage & Examples
Introduction
Xmake uses .mpp as the default module extension, but also supports .ixx, .cppm, .mxx, etc. It fully supports C++20 Modules with gcc11/clang/msvc, and can automatically analyze module dependencies for maximum parallel compilation.
Basic Example
add_rules("mode.release", "mode.debug")
set_languages("c++20")
target("hello")
set_kind("binary")
add_files("src/*.cpp", "src/*.mpp")
Class Example
Exporting classes in modules:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("class_test")
set_kind("binary")
add_files("src/*.cpp")
add_files("src/*.mpp")
Module Partitions
Using module partitions:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("partition_test")
set_kind("binary")
add_files("src/*.cpp")
add_files("src/*.mpp")
Shared Library Modules
Creating a shared library with modules:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("foo")
set_kind("shared")
add_files("src/foo.mpp")
target("test")
set_kind("binary")
add_deps("foo")
add_files("src/main.cpp")
Cross-Target Dependency
Modules dependency between targets:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("bar")
set_kind("static")
add_files("src/bar.mpp")
target("app")
set_kind("binary")
add_deps("bar")
add_files("src/main.cpp")
Private Module Fragment
Using private module fragment to hide implementation details:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("box")
set_kind("binary")
add_files("src/*.cpp")
add_files("src/*.mpp")
Module Implementation Unit
Separating module interface and implementation:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("app")
set_kind("binary")
add_files("src/*.cpp")
add_files("src/*.mpp")
Module Aggregation
Using export import to aggregate submodules:
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("app")
set_kind("binary")
add_files("src/*.cpp")
add_files("src/*.mpp")
Cpp-only Project with Modules
From v2.7.1, Headerunits are supported. Normally, at least one .mpp file is needed to enable modules, but you can also force it:
add_rules("mode.debug", "mode.release")
set_languages("c++latest")
target("stdmodules_cpp_only")
set_kind("binary")
add_files("src/*.cpp")
set_policy("build.c++.modules", true)
Headerunits Example
See headerunits example for how to use STL or custom headers as headerunits.
add_rules("mode.release", "mode.debug")
set_languages("c++20")
target("stl_headerunit")
set_kind("binary")
add_files("src/*.cpp", "src/*.mpp")
C++23 Standard Library Modules
Support for C++23 stdmodules:
add_rules("mode.debug", "mode.release")
set_languages("c++latest")
target("mod")
set_kind("static")
add_files("src/*.cpp")
add_files("src/*.mpp", {public = true})
target("stdmodules")
set_kind("binary")
add_files("test/*.cpp")
add_deps("mod")
Module Package Distribution
Define and distribute a C++ Modules package:
add_rules("mode.release", "mode.debug")
set_languages("c++20")
target("foo")
set_kind("static")
add_files("*.cpp")
add_files("*.mpp", {
defines = "FOO_EXPORT",
public = true
})
Module Package Integration
Quickly integrate with add_requires("foo"):
add_rules("mode.release", "mode.debug")
set_languages("c++20")
add_repositories("my-repo repo")
add_requires("foo")
target("app")
set_kind("binary")
add_packages("foo")
add_files("src/*.cpp")
set_policy("build.c++.modules", true)
More official examples: C++ Modules Examples