Impossible to reuse precompiles headers (PCH) of an imported library
I'm trying to create one CMake library project that creates a PCH if CMAKE_DISABLE_PRECOMPILE_HEADERS
is set to ON
(I only enable this for developer builds, not production builds). The PCH of this project should be reused in multiple other projects, i.e. if the option is ON
all projects use the same PCH (generated once). If the option is OFF
no PCH are used at all.
I have one CMake project foo_pch
, which creates an object library foo::pch-obj
and static library foo::pch
, both declared in a CMake package config file. The foo::pch-obj
IMPORTED
target in the package config file has INTERFACE_PRECOMPILE_HEADERS
properly set.
In the project foo_client
the PCH from foo_pch
should be reused as follows:
if(NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
target_precompile_headers(foo_client_obj REUSE_FROM foo::pch)
endif()
But this does not work. Instead CMake v3.18.2 creates the following error message:
PRECOMPILE_HEADERS_REUSE_FROM set with non existing target
But the target exists, e.g. the branch if(TARGET foo::pch)
is taken. Is there any workaround available? I don't want to create a separate PCH per project.