Ninja: MSVC error using PCH with forced include
- When adding forced include file to a target that is using precompiled header, MSVC compilation errors in Ninja generator.
- When using "Visual Studio 16 2019" generator instead of Ninja, MSVC compilation pass.
- When not using precompile header and Ninja, MSVC compilation pass.
Minimum testcase:
Project Structure:
- CMakeLists.txt
- empty.cxx
- h1.h
- h2.h
- main.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(pch_test)
add_library(pch_common OBJECT empty.cxx)
target_precompile_headers(
pch_common
PRIVATE
<iostream>
)
add_executable(main main.cpp)
target_compile_options(main PRIVATE "/FI" "${CMAKE_CURRENT_LIST_DIR}/h2.h")
target_precompile_headers(main REUSE_FROM pch_common)
h1.h
int h1 = 10;
h2.h
int h2 = 20;
main.cpp
#include <iostream>
#include "h1.h"
using namespace std;
int main() {
cout << "h1: " << h1 << endl;
cout << "h2: " << h2 << endl;
return 0;
}
CMake Ninja Configure & Build
$ cmake -B build_ninja -G Ninja
-- The C compiler identification is MSVC 19.29.30145.0
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.5s)
-- Generating done (0.0s)
-- Build files have been written to: <path>/pch_test/build_ninja
$ cmake --build build_ninja
[4/5] Building CXX object CMakeFiles\main.dir\main.cpp.obj
FAILED: CMakeFiles/main.dir/main.cpp.obj
C:\PROGRA~2\MIB055~1\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Zi /Ob0 /Od /RTC1 -MDd /FI <path>/pch_test/h2.h /Yu<path>/pch_test/build_ninja/CMakeFiles/pch_com
mon.dir/cmake_pch.hxx /Fp<path>/pch_test/build_ninja/CMakeFiles/pch_common.dir/./cmake_pch.cxx.pch /FI<path>/pch_test/build_ninja/CMakeFiles/pch_common.dir/cmake_pch.hxx /showIncludes /FoCMakeFiles\main.dir\main.cpp.obj /
Fdmain.dir\pch_common.pdb /FS -c <path>\pch_test\main.cpp
<path>\pch_test\main.cpp(8): error C2065: 'h2': undeclared identifier
ninja: build stopped: subcommand failed.
CMake VS 2019 Configure & Build & Run
$ cmake -B build_vs2019 -G "Visual Studio 16 2019" -A x64
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.29.30145.0
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (4.7s)
-- Generating done (0.0s)
-- Build files have been written to: <path>/pch_test/build_vs2019
$ cmake --build build_vs2019
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
Building Custom Rule <path>/pch_test/CMakeLists.txt
cmake_pch.cxx
empty.cxx
pch_common.vcxproj -> <path>\pch_test\build_vs2019\pch_common.dir\Debug\pch_common.lib
Building Custom Rule <path>/pch_test/CMakeLists.txt
main.cpp
main.vcxproj -> <path>\pch_test\build_vs2019\Debug\main.exe
Building Custom Rule <path>/pch_test/CMakeLists.txt
$ ./build_vs2019/Debug/main.exe
h1: 10
h2: 20
Edited by Abdelmaged Khalifa