Pre compile header issue

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

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

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,


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.

What generator is this with?

The first thing I’d try is to use the Ninja generator. If you still have issues there, the 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?

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.