PCH: Using TARGET_OBJECTS:target might add the pch to the linker
Continuing my quest to replace cotire with upstream PCH files I ran into a funky issue where it seems like a PCH on a OBJECT library doesn't work correctly. Note that I haven't been able to reproduce this outside our "big" CMake project and I am looking for some help to narrow this down.
In our big project we have a ServerLibrary OBJECT library that has it's own PCH. This is then used by the Server and ServerTests targets which also have their own PCH's.
What happens when linking the Server target is that the pch is passed to linker and strange things happens:
clang++ -fPIC -DPIC -O0 -g -D_GLIBCXX_DEBUG=1 -fno-finite-math-only --Wno-delete-non-virtual-dtor -fvisibility-inlines-hidden -Wno-expansion-to-defined -Wsign-compare -Werror=sign-compare -pipe -Wno-unused-variable -g -static-libgcc -z noexecstack -Wl,-rpath='XORIGIN:XORIGIN/../lib' -Wl,--build-id=sha1 -fuse-ld=lld -Wl,-rpath,'$ORIGIN/lib' CMake/Targets/Server/CMakeFiles/Server.dir/__/__/__/main.cpp.o <more server target objects> -o "build/Server" -Wl,-rpath,'$ORIGIN/lib' CMake/Targets/ServerLibrary/CMakeFiles/ServerLibrary.dir/CMakeFiles/ServerLibrary.dir/cmake_pch.hxx.pch CMake/Targets/ServerLibrary/CMakeFiles/ServerLibrary.dir/CMakeFiles/ServerLibrary.dir/cmake_pch.h.pch CMake/Targets/ServerLibrary/CMakeFiles/ServerLibrary.dir/__/__/__/Activity/Activity.cpp.o ... <more ServerLibrary objects here>
As you see the pch files are passed to the linker which then leads to a linker error. I can't really share the full cmakefiles because they are big and contains stuff I don't want on the Internet. If I switch the OBJECT lib to STATIC it doesn't show the same problem.
I tried to replicate this with a small simple CMakeLists that replicates much more direct what we do above:
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(Hello CXX)
add_library(objectlib OBJECT object.cpp)
target_precompile_headers(objectlib PRIVATE stdafx.h)
add_executable(hello hello.cpp $<TARGET_OBJECTS:objectlib>)
target_precompile_headers(hello PRIVATE stdafx.h)
Unfortunately this doesn't show the same problem. So there must be something more to this issue, can you guys think of something that can help me provide more info here?
Btw we are using clang and the ninja generator for this.