C++Modules/Ninja: Header units not supported
C++ 20 modules simply don't work with MSVC 2022 and CMake: cannot 'import' neither standard nor custom headers.
Nothing can be imported in module files, only included.
To reproduce:
math.ixx:
export module math;
import <iostream>;
export int add(int a, int b){
return a + b;
std::cout << a;
}
main.cpp:
import math;
int main(){
int a=1;
int b=2;
int absum = add(a, b);
return absum * 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.26)
project(CXXModules LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
add_executable(math_mod math.cpp)
target_sources(math_mod PUBLIC
FILE_SET cxx_modules
TYPE CXX_MODULES
FILES math.ixx
)
Output with Ninja:
>------ Rebuild All started: Project: module, Configuration: x64-debug ------
[1/1] Cleaning all built files...
Cleaning... 0 files.
[1/6] Scanning M:\Temp\Cpp23-examples-main\cpp20\module\math.cpp for CXX dependencies
[2/6] Scanning M:\Temp\Cpp23-examples-main\cpp20\module\math.ixx for CXX dependencies
math.ixx
[3/6] Generating CXX dyndep file CMakeFiles\math_mod.dir\CXX.dd
[4/6] Building CXX object CMakeFiles\math_mod.dir\math.ixx.obj
FAILED: CMakeFiles/math_mod.dir/math.ixx.obj CMakeFiles/math_mod.dir/math.ifc
C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -std:c++20 -MDd -Zi /showIncludes @CMakeFiles\math_mod.dir\math.ixx.obj.modmap /FoCMakeFiles\math_mod.dir\math.ixx.obj /FdCMakeFiles\math_mod.dir\ /FS -c M:\Temp\Cpp23-examples-main\cpp20\module\math.ixx
math.ixx
M:\Temp\Cpp23-examples-main\cpp20\module\math.ixx(3): error C7612: could not find header unit for 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include\iostream'
M:\Temp\Cpp23-examples-main\cpp20\module\math.ixx(7): error C2039: 'cout': is not a member of 'std'
predefined C++ types (compiler internal)(343): note: see declaration of 'std'
M:\Temp\Cpp23-examples-main\cpp20\module\math.ixx(7): error C2065: 'cout': undeclared identifier
ninja: build stopped: subcommand failed.
Rebuild All failed.
Including iostream instead of importing solves the problem, but this defeats the purpose of using C++20 modules.
MSBuild builds the above code just fine:
Rebuild started...
1>------ Rebuild All started: Project: mod, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>math.ixx
1>iostream
1>Compiling...
1>iostream
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include\atomic(325,35): warning C5260: the constant variable 'std::memory_order const (* `std::memory_order __cdecl std::_Combine_cas_memory_orders(std::memory_order,std::memory_order)'::`2'::_Combined_memory_orders)[6]' has internal linkage in an included header file context, but external linkage in imported header unit context; consider declaring it 'inline' as well if it will be shared across translation units, or 'static' to express intent to use it local to this translation unit
1>math.ixx
1>math.cpp
1>mod.vcxproj -> M:\Temp\Cpp23-examples-main\cpp20\module\mod\x64\Debug\mod.exe
1>Done building project "mod.vcxproj".
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Rebuild started at 1:31 PM and took 02.080 seconds ==========
If this helps, attached is the 'Detailed' MSBuild output.
MSBuild_log_Detailed.txt
Adding /scanDependencies- compiler flag results in another problem:
the .obj files are just not created. So build still fails:
>------ Rebuild All started: Project: module, Configuration: x64-debug ------
[1/1] Cleaning all built files...
Cleaning... 5 files.
[1/6] Scanning M:\Temp\Cpp23-examples-main\cpp20\module\math.cpp for CXX dependencies
[2/6] Scanning M:\Temp\Cpp23-examples-main\cpp20\module\math.ixx for CXX dependencies
math.ixx
[3/6] Generating CXX dyndep file CMakeFiles\math_mod.dir\CXX.dd
[4/6] Building CXX object CMakeFiles\math_mod.dir\math.ixx.obj
math.ixx
{
"version": 1,
"revision": 0,
"rules": [
{
"primary-output": "CMakeFiles\\math_mod.dir\\math.ixx.obj",
"outputs": [
"M:\\Temp\\Cpp23-examples-main\\cpp20\\module\\out\\build\\x64-debug\\CMakeFiles\\math_mod.dir\\vc140.pdb",
"-",
"CMakeFiles\\math_mod.dir\\math.ifc"
],
"provides": [
{
"logical-name": "math",
"source-path": "m:\\temp\\cpp23-examples-main\\cpp20\\module\\math.ixx",
"is-interface": true
}
],
"requires": [
{
"logical-name": "iostream",
"source-path": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.35.32215\\include\\iostream",
"lookup-method": "include-angle",
"unique-on-source-path": true
}
]
}
]
}
[5/6] Building CXX object CMakeFiles\math_mod.dir\math.cpp.obj
{
"version": 1,
"revision": 0,
"rules": [
{
"primary-output": "CMakeFiles\\math_mod.dir\\math.cpp.obj",
"outputs": [
"M:\\Temp\\Cpp23-examples-main\\cpp20\\module\\out\\build\\x64-debug\\CMakeFiles\\math_mod.dir\\vc140.pdb",
"-"
],
"requires": [
{
"logical-name": "math"
}
]
}
]
}
[6/6] Linking CXX executable math_mod.exe
FAILED: math_mod.exe
cmd.exe /C "cd . && C:\Python310\Lib\site-packages\cmake\data\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\math_mod.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\link.exe /nologo CMakeFiles\math_mod.dir\math.cpp.obj CMakeFiles\math_mod.dir\math.ixx.obj /out:math_mod.exe /implib:math_mod.lib /pdb:math_mod.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\link.exe /nologo CMakeFiles\math_mod.dir\math.cpp.obj CMakeFiles\math_mod.dir\math.ixx.obj /out:math_mod.exe /implib:math_mod.lib /pdb:math_mod.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\math_mod.dir/intermediate.manifest CMakeFiles\math_mod.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : math_mod.exe not found or not built by the last incremental link; performing full link
M:\Temp\Cpp23-examples-main\cpp20\module\out\build\x64-debug\LINK : fatal error LNK1104: cannot open file 'CMakeFiles\math_mod.dir\math.cpp.obj'
ninja: build stopped: subcommand failed.
Rebuild All failed.
Edited by Brad King