# Pre compile header issue

**URL:** https://discourse.cmake.org/t/pre-compile-header-issue/5650
**Category:** Code
**Created:** [May 11, 2022, 9:24am UTC](https://discourse.cmake.org/t/pre-compile-header-issue/5650 "2022-05-11T09:24:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Iamniki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/58f4c7/32.png) [@Iamniki](https://discourse.cmake.org/u/Iamniki)
#### Post date: [May 11, 2022, 9:24am UTC](https://discourse.cmake.org/t/pre-compile-header-issue/5650/1 "2022-05-11T09:24:12Z")

</div>

Hi everyone,

I have a very big project which compilation takes 40-60 mins with 4 threads. So I am trying to optimize it. I have pch.hpp and its ProjectRoot/deps\_header/cmakelists.txt is here

```cmake
option(BUILD_USING_PCH "Build using pre-compile header support" ON)
if (BUILD_USING_PCH)
    message(STATUS "Compiling using pre-compiled header support")
    target_precompile_headers(${TARGET_NAME} PUBLIC deps_header/pch.hpp) # With PUBLIC they will be used by targets using this target
    set(CMAKE_PCH_INSTANTIATE_TEMPLATES ON)
else()
    message(STATUS "Disabled pre-compiled header support")
endif()

```

I added the above cmake in root cmakelists.txt as this

```cmake
set(TARGET_NAME foo)
if (STATIC_BUILD)
    add_library(${TARGET_NAME} STATIC "")
else()
    add_library(${TARGET_NAME} SHARED "")
endif()

# Add top level source directory
add_subdirectory(src)

# Add top level benchmark source directory
if(NOT EXCLUDE_BENCHMARK_TESTS)
    add_subdirectory(benchmark)
endif()

include(deps_header/CMakeLists.txt)

```

Note Target name is foo and this foo is used in all tests as this,

```cmake

list(APPEND DEPS
    nrlmsise00
    foo
    gtest
    gtest_main
    fmt_lib
    spdlog_lib
    testutils_lib)

```

in src/foo-1/test/cmakelists.txt.  
When ever I build my project, pch start building for every test folder(since the foo is appended). How can I avoid this ? This pch is build once and created cmake\_pch.hxx.gch so whenever foo is appended it should not build again rather it should use the same one.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 11, 2022, 10:41am UTC](https://discourse.cmake.org/t/pre-compile-header-issue/5650/2 "2022-05-11T10:41:52Z")

</div>

> [@Iamniki](#):
>
> compilation takes 40-60 mins with 4 threads

What generator is this with?

> [@Iamniki](#):
>
> So I am trying to optimize it

The first thing I’d try is to use the `Ninja` generator. If you still have issues there, the [`ninjatracing`](https://github.com/nico/ninjatracing) tool can be used to see where bottlenecks are that you can start working on within the build graph itself.

That said, I’m not sure how PCH itself works in CMake, so I don’t have much to say there specifically. Does making it `PRIVATE` help out?

---

<div class="post-metadata">

### Author: ![Iamniki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/58f4c7/32.png) [@Iamniki](https://discourse.cmake.org/u/Iamniki)
#### Post date: [May 11, 2022, 10:54am UTC](https://discourse.cmake.org/t/pre-compile-header-issue/5650/3 "2022-05-11T10:54:56Z")

</div>

I am using UNIX Makefiles as generator. Ninja is also in the list as available generator. Unix Makefiles is as default. I will try with ninja now.
