# Help with target\_precompile\_headers

**URL:** <https://discourse.cmake.org/t/help-with-target-precompile-headers/1065>\
**Category:** Code\
**Created:** [April 20, 2020, 5:12pm UTC](https://discourse.cmake.org/t/help-with-target-precompile-headers/1065 "2020-04-20T17:12:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![polar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/polar/32/542_2.png) [@polar](https://discourse.cmake.org/u/polar)\
**Post date:** [April 20, 2020, 5:12pm UTC](https://discourse.cmake.org/t/help-with-target-precompile-headers/1065/1 "2020-04-20T17:12:09Z")

</div>

Problem, Windows10, vs2019: using

```auto
target_precompile_headers(some_target PRIVATE _pch.h)

```

when building a subcomponent of a static library, `some.lib`. The problem is that when I try to link to `some.lib`, I get a `LNK2011: precompiled header not linked in`, naming a seemingly random file from the directory of `some_target`. (Gotta love Windows).  
I have a file in the target called `_pch.cpp` that includes `_pch.h`, whiich was the way I used it with Cotire.  
However, I don’t think `_pch.cpp.obj` is the precompiled header object, as it is only 3kb. It seems to be `some_target/CMakeFiles/some_target.dir/CMakeFiles/some_target.dir/cmake_pch.cxx.obj`

So, if I put a

```auto
target_sources(some_target CMakeFiles/some_target.dir/cmake_pch.cxx)

```

this problem is rectified.

However, this approach sounds counter intuitive to me. It seems like this inclusion should happen automatically, at least on Windows with Ninja.  
NOTE: On Linux there is no problem.

---

<div class="post-metadata">

**Author:** ![polar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/polar/32/542_2.png) [@polar](https://discourse.cmake.org/u/polar)\
**Post date:** [April 20, 2020, 6:19pm UTC](https://discourse.cmake.org/t/help-with-target-precompile-headers/1065/2 "2020-04-20T18:19:55Z")

</div>

Actually, this approach does not work, since `${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/some_target.dir/cmake_pch.cxx` will not exist at the time of CMake configuration.

---

<div class="post-metadata">

**Author:** ![polar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/polar/32/542_2.png) [@polar](https://discourse.cmake.org/u/polar)\
**Post date:** [April 20, 2020, 6:31pm UTC](https://discourse.cmake.org/t/help-with-target-precompile-headers/1065/3 "2020-04-20T18:31:07Z")

</div>

The issue is that most of my static library subsections are OBJECT libraries.  
In directory lib1:

```auto
add_library(lib1 OBJECT ...)
target_precompile_headers(lib1 PRIVATE _pch.h)

```

In directory lib2:

```auto
add_library(lib2 OBJECT ...)
target_precompile_headers(lib2 PRIVATE _pch.h)

```

In superior directory:

```auto
add_subdirectory(lib1)
add_subdirectory(lib2)
add_library(some_lib STATIC ...)
target_link_libraries(some_lib PRIVATE lib1 lib2)

```

This generated `some_lib.lib` and installed it in a suitable place to where it could be linked.  
In another separate project:

```auto
add_library(thing SHARED ...)
target_link_libraries(thing PRIVATE some_lib)

```

I would get

```auto
LNK2011: (.../somelib1file.cpp) precompiled header not linked in, image may not run
LNK2011: (.../somelib2file.cpp) precompiled header not linked in, image may not run

```

However, I discovered that if I just made ONE of those subdirectory precompiled headers PUBLIC the shared library would link to it.

So, my solution was to generate a bogus/empty precompiled header for the STATIC library, and that seemed to rectify the problem.

```auto
add_subdirectory(lib1)
add_subdirectory(lib2)
add_library(some_lib STATIC ...)
target_precompiled_header(some_lib PRIVATE bogus.h)
target_link_libraries(some_lib PRIVATE lib1 lib2)

```

Something about this discovery just doesn’t seem right.  
I have not yet gotten to the point of testing this approach, just yet.
