11

I have a Visual Studio C++ project, and after updating Visual Studio 2022 recently from version 17.5 to version 17.6, the compilation stops in the very beginning with the error:

1>------ Rebuild All started: Project: MRPch, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>std.compat.ixx
1>std.ixx
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\modules\std.compat.ixx : fatal  error C1083: Cannot open include file: 'MRPch.h': No such file or directory
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\modules\std.ixx : fatal  error C1083: Cannot open include file: 'MRPch.h': No such file or directory
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(486,5): warning MSB8074: Cannot read Module Dependencies file C:\Work\MeshInspector\source\TempOutput\MRPch\x64\Debug\std.ixx.module.json: Expecting element 'root' from namespace ''.. Encountered 'None'  with name '', namespace ''.  The build order might be incorrect.
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(486,5): warning MSB8074: Cannot read Module Dependencies file C:\Work\MeshInspector\source\TempOutput\MRPch\x64\Debug\std.compat.ixx.module.json: Expecting element 'root' from namespace ''.. Encountered 'None'  with name '', namespace ''.  The build order might be incorrect.

As far as I can see, it is somehow related to the precompiled header MRPch.h, which is forcedly included in all source files via the compiler option /FI"MRPch.h".

If I remove forced include, then the error changes to

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\modules\std.ixx(147,1): fatal  error C1010: unexpected end of file while looking for precompiled header.

There are neither std.compat.ixx nor std.ixx in my project, but I found that the error is somehow related to /std:c++latest command-line option, which enables the latest language features.

I looked at Visual Studio 2022 version 17.6 Release Notes, but did not see anything related to precompiled headers there. Are they no longer compatible with the latest C++ standard?

Fedor
  • 17,146
  • 13
  • 40
  • 131

2 Answers2

14

To fix this problem, change this setting: Project properties > C/C++ > Language > Build ISO C++23 Standard Library Modules > No.

head scratcher
  • 156
  • 1
  • 2
  • 2
    Still...what would you need to do if you *do* want to use the std lib modules? – Erik Bongers May 28 '23 at 21:01
  • 2
    Update: apparently in msvc, the std lib modules **replace** the precompiled headers! Which makes sense. However, if your code needs to work with other compilers, it might be best not to replace these PCHs too quickly. – Erik Bongers May 28 '23 at 21:11
  • 3
    You can also fix it by setting 'C++ Language Standard' to 'ISO C++20 Standard (/std:c++20)' or something else besides latest. – manylegged May 30 '23 at 15:15
1

I know there was no mention of CMake in the original question, but it was one of the first results when I searched for this problem, so I'll leave the solution for CMake projects here, maybe it will be useful for others.

Create a Property Sheet (*.props file) with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemDefinitionGroup>
        <ClCompile>
        <BuildStlModules>false</BuildStlModules>
        </ClCompile>
    </ItemDefinitionGroup>
</Project>

Add the property sheet to your targets using PCHs, e.g.:

SET_PROPERTY(TARGET target_name PROPERTY VS_USER_PROPS path_to_props_file.props)
Donpedro
  • 828
  • 7
  • 22
  • This alone didn't fix it for me . It works if I either revert from C++23 to `set(CMAKE_CXX_STANDARD 20)` and keep precomp's, or if I keep C++23 and disable `target_precompile_headers(MyTarget PRIVATE source/precomp.h)`. – Dwayne Robinson Jul 07 '23 at 22:44