PCH: target_precompile_headers REUSE_FROM can cause link failures in other projects using target_precompile_headers
Here is the test case that is currently causing me trouble:
cmake_minimum_required(VERSION 3.16)
project(CMakePCH VERSION 1.0.0 LANGUAGES CXX)
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/Empty.cpp "\n")
add_library(BasePCH STATIC Empty.cpp)
target_compile_options(BasePCH PRIVATE /permissive-)
target_precompile_headers(BasePCH PRIVATE <windows.h> <vector>)
add_library(MyObjectLib OBJECT Empty.cpp)
target_compile_options(MyObjectLib PRIVATE /permissive-)
target_precompile_headers(MyObjectLib REUSE_FROM BasePCH)
add_library(IntermdiateStatic STATIC Empty.cpp)
target_compile_options(IntermdiateStatic PRIVATE /permissive-)
target_link_libraries(IntermdiateStatic PRIVATE MyObjectLib)
add_library(MySharedLib SHARED Empty.cpp)
target_link_libraries(MySharedLib PRIVATE IntermdiateStatic)
#using /MTd (staticically linking the std library) causes the BasePCH to be incompatible
target_compile_options(MySharedLib PRIVATE /MTd)
#when using target_precompile_headers it results in BasePCH.dir\Debug\cmake_pch.obj getting linked into MySharedLib when it really shouldn't
#commenting target_precompile_headers below fixes the issue
target_precompile_headers(MySharedLib PRIVATE <iostream>)
I'm running into this problem that using target_precompile_headers( ... REUSE_FROM ...)
causes problems when using target_precompile_headers
in other projects. Uncommenting the last line fixes the issue. Basically what happens is that when target_precompile_headers(MySharedLib PRIVATE <iostream>)
is used this causes the .obj (BasePCH.dir\Debug\cmake_pch.obj
) to be included in the linked to objects of MySharedLib (although this two things should not relate to each other).