Issue
I have a large test suite that is compiled as an executable, that is roughly structured as follows:
ProjectRootDir
|
---A -> .cpp/h files with a fairly common set of expensive includes
|
---B -> .cpp/.h files with a fairly common but different set of expensive includes
|
(etc.)
Using precompiled headers greatly reduces the compile time of the entire project. But because this is a test project which means the pch included files can change often, ideally I'd have one pch for the 'A' source files, another pch for the 'B' source files, etc. In order to prevent recompiling the entire project every time a subset of the precompiled headers change. For changes where I truly do want to verify that the entire project still compiles.
What is the best way to do this in CMake?
Solution
Just make sure that the source files in A
belong to a different target than those in B
. You can do this with OBJECT
libraries if it isn't the case already:
add_library(A OBJECT ...)
target_precompile_headers(A PRIVATE expensive_header_a.h)
add_library(B OBJECT ...)
target_precompile_headers(B PRIVATE expensive_header_b.h)
add_library(combined ...)
target_link_libraries(combined PRIVATE A B)
Now the sources in A
will get a PCH with expensive_header_a.h
in it and similarly for B
. See the docs: https://cmake.org/cmake/help/latest/command/target_precompile_headers.html
Answered By - Alex Reinking Answer Checked By - Senaida (WPSolving Volunteer)