Skip to content

Visual Studio Intellisense not working with C++20 Modules when created with CMake

Description
I’ve used CMake to create the following Visual Studio 2022 solution to test modules:

  • PrintLib library which exports Print module.
  • MathLib library which exports Math module and uses Print module.
  • main executable which uses Math module.

The Intellisense does not work properly when using modules, highlighting the modules as errors.

The CMakeLists.txt has the following properties:

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)

For Visual Studio 2022 IntelliSense to work with modules correctly the property “C/C++ > General > scan sources for module dependencies” needs to be enabled manually in all projects after the solution has been generated. But CMAKE_CXX_SCAN_FOR_MODULES only enables the property on each source file and not in the project.

IssueIntellisenseModules

Issue reported in Visual Studio too: https://developercommunity.visualstudio.com/t/Intellisense-not-working-with-C20-Modu/10650325

Requirements

  • Visual Studio 2022. I’m using Version 17.9.6.
  • CMake 3.29.

Steps to reproduce

  • Clone https://github.com/AaronRuizMoraUK/cxx-reminder
  • Enter cxx-reminder folder and create a folder called build
  • In a command line go to cxx-reminder/build and enter cmake ..
  • Open VS solution generated cxx-reminder.sln
  • Inside main project, open Cxx20.cpp file and go to the end of the file.

Expected Behaviour
Math module is correctly picked up by Intellisense.

Actual Behaviour
Math module is highlighted as error by Intellisense.

Workaround
Enable manually “scan sources for module dependencies” in all projects after the solution has been generated by CMake. But this is a tedious process and it’d be great if this is handled automatically, either by:

  • CMake CMAKE_CXX_SCAN_FOR_MODULES does enable the property at project level, and not at source file level.
  • Or Visual Studio 2022 Intellisense works properly with “scan sources for module dependencies” at source file level.
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information