# Share pre-compiled header object

**URL:** https://discourse.cmake.org/t/share-pre-compiled-header-object/1428
**Category:** Code
**Created:** [June 22, 2020, 12:20pm UTC](https://discourse.cmake.org/t/share-pre-compiled-header-object/1428 "2020-06-22T12:20:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![MathiasMagnus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathiasmagnus/32/431_2.png) [@MathiasMagnus](https://discourse.cmake.org/u/MathiasMagnus)
#### Post date: [June 22, 2020, 12:20pm UTC](https://discourse.cmake.org/t/share-pre-compiled-header-object/1428/1 "2020-06-22T12:20:31Z")

</div>

I have a whole bunch of unit tests that share the headers of Google Test and a bunch of common includes. Many targets, same PCH. I noticed that inheriting an `INTERFACE` library [as per the docs](https://cmake.org/cmake/help/latest/command/target_precompile_headers.html?#main-form) will create multiple PCH files, one for each target with identical content. The `REUSE_FROM` method promotes an arbitrary target as something other targets depend on. It’s problematic from an both logical perspective and it precludes writing macros to declare unit tests, because the target to reuse from has not been declared (fed to the macro) yet.

As a test I resorted to a similar method depicted [here](https://discourse.cmake.org/t/help-with-target-precompile-headers/1065/2) by creating an object library that I link to. The following sample has two source files sharing the same pre-compiled header consisting of `Common.hpp`

```cmake
cmake_minimum_required(VERSION 3.16)

project(InheritPCH LANGUAGES CXX)

file(GENERATE
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/PCH.cpp
  CONTENT ""
)

add_library(PCH OBJECT
  ${CMAKE_CURRENT_BINARY_DIR}/PCH.cpp
)

target_precompile_headers(PCH
  PRIVATE
    Common.hpp
)

target_include_directories(PCH
  PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}
)

foreach(Target IN ITEMS
  Main1
  Main2
)
  add_executable(${Target}
    ${Target}.cpp
  )

  target_precompile_headers(${Target}
    REUSE_FROM
      PCH
  )
endforeach()

```

The problem with this approach is that it requires a dummy empty source file, because `OBJECT` libraries cannot be empty. Building this project with Ninja looks like:

```auto
[main] Building folder: CPPTest 
[build] Starting build
[proc] Executing command: C:\Kellekek\Kitware\CMake\3.17.2\bin\cmake.exe --build c:/Users/mnagy/Source/CPPTest/.vscode/build --config Debug --target all -- -j 18
[build] [1/6 16% :: 0.635] Building CXX object CMakeFiles\PCH.dir\cmake_pch.cxx.obj
[build] [2/6 33% :: 0.691] Building CXX object CMakeFiles\PCH.dir\PCH.cpp.obj
[build] [4/6 50% :: 0.748] Building CXX object CMakeFiles\Main1.dir\Main1.cpp.obj
[build] [5/6 66% :: 0.755] Building CXX object CMakeFiles\Main2.dir\Main2.cpp.obj
[build] [6/6 83% :: 1.453] Linking CXX executable Main1.exe
[build] [6/6 100% :: 1.453] Linking CXX executable Main2.exe
[build] Build finished with exit code 0

```

But `PCH.cpp.obj` is totally unnecessary.

Would it be possible to create a target with no extra object files, _just_ `cmake_pch.cxx.obj` to share with correct `/Yu` and `/Yc` respectively?

---

<div class="post-metadata">

### Author: ![cristianadam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cristianadam/32/124_2.png) [@cristianadam](https://discourse.cmake.org/u/cristianadam)
#### Post date: [June 23, 2020, 12:11pm UTC](https://discourse.cmake.org/t/share-pre-compiled-header-object/1428/2 "2020-06-23T12:11:07Z")

</div>

The target needs to have a source file in order to know which type of language the target supports.

I have tried fixing this issue in the past and gave up due to the solution being more complicated than having an empty source file added to the build.

You can use `file(GENERATE)` to generate the file so that you won’t get rebuilds after running cmake.
