Quick answer: CMake build with MSVC failing on C++20 import statements? Module support needs CMake 3.28+ and an explicit FILE_SET declaration for module unit files.

Migrating an engine to C++20 modules; CMake builds error with “cannot find module unit” despite the files being on disk.

CMake Version

C++20 module support landed in CMake 3.28. Older versions ignore module unit files. Bump your project’s required CMake version.

FILE_SET CXX_MODULES

target_sources(MyEngine
  PUBLIC FILE_SET CXX_MODULES
  FILES src/Math.cppm src/Engine.cppm)

Declares which files are module units. CMake invokes the right MSVC flags for each.

MSVC Flag /experimental:module

On older toolchains, manual /experimental:module is needed. With modern MSVC and CMake 3.28+, the right flags propagate automatically.

Compile Database Order

Modules require dependency order at compile. CMake 3.28+ handles the topological sort. If you wrote custom build rules, switch to CMake-driven.

Verifying

Build succeeds. Module imports resolve; partial builds rebuild only affected modules; full builds complete cleanly.

“Modules need CMake 3.28+ and FILE_SET CXX_MODULES. Older toolchains need experimental flags.”

Modules are a productivity win once set up — the precompiled module units cut build times in modular codebases significantly.