CMP0141: Policy set to NEW breaks PCH REUSE_FROM with MSVC toolchain
The following minimal project demonstrates the failure:
CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(debug_info)
add_executable(debug_info main.cpp)
target_precompile_headers(debug_info PRIVATE <iostream>)
add_executable(copy_of_debug_info main.cpp)
target_precompile_headers(copy_of_debug_info REUSE_FROM debug_info)
main.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Run CMake with CMAKE_BUILD_TYPE
set to Debug. I was using the Ninja generator with VS2019 toolchain. Then when compiling the copy_of_debug_info
target, the following error occurs:
C:\Users\craig\Projects\debug_info\main.cpp: error C2859: C:\Users\craig\Projects\debug_info\build\debug_info.pdb is not the pdb file that was used when this precompiled header was created, recreate the precompiled header.
Full build log
"C:\Program Files\CMake\bin\cmake.exe" --build C:\Users\craig\Projects\debug_info\build --target all -- --verbose
[1/5] C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -MDd -Zi /YcC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /FpC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/./cmake_pch.cxx.pch /FIC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /showIncludes /FoCMakeFiles\debug_info.dir\cmake_pch.cxx.obj /Fddebug_info.dir\debug_info.pdb /FS -c C:\Users\craig\Projects\debug_info\build\CMakeFiles\debug_info.dir\cmake_pch.cxx
[2/5] C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -MDd -Zi /YuC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /FpC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/./cmake_pch.cxx.pch /FIC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /showIncludes /FoCMakeFiles\debug_info.dir\main.cpp.obj /Fddebug_info.dir\debug_info.pdb /FS -c C:\Users\craig\Projects\debug_info\main.cpp
[3/5] cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\debug_info.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo CMakeFiles\debug_info.dir\cmake_pch.cxx.obj CMakeFiles\debug_info.dir\main.cpp.obj /out:debug_info.exe /implib:debug_info.lib /pdb:debug_info.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 ."
[4/5] C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -MDd -Zi /YuC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /FpC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/./cmake_pch.cxx.pch /FIC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /showIncludes /FoCMakeFiles\copy_of_debug_info.dir\main.cpp.obj /Fddebug_info.pdb /FS -c C:\Users\craig\Projects\debug_info\main.cpp
FAILED: CMakeFiles/copy_of_debug_info.dir/main.cpp.obj
C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -MDd -Zi /YuC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /FpC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/./cmake_pch.cxx.pch /FIC:/Users/craig/Projects/debug_info/build/CMakeFiles/debug_info.dir/cmake_pch.hxx /showIncludes /FoCMakeFiles\copy_of_debug_info.dir\main.cpp.obj /Fddebug_info.pdb /FS -c C:\Users\craig\Projects\debug_info\main.cpp
C:\Users\craig\Projects\debug_info\main.cpp: error C2859: C:\Users\craig\Projects\debug_info\build\debug_info.pdb is not the pdb file that was used when this precompiled header was created, recreate the precompiled header.
ninja: build stopped: subcommand failed.
The error does not occur if you change the first line of the CMakeLists.txt
file to version 3.24 instead, or explicitly set CMP0141
to OLD
before the call to project()
. You can also avoid the error by setting CMAKE_MSVC_DEBUG_INFORMATION_FORMAT
to Embedded
before the first call to add_executable()
, but that disables PDB files altogether, so that's not surprising. Setting that variable to ProgramDatabase
or EditAndContinue
fails with the same error as above.