0

I have a project with the following structure

root/
  CMakeLists.txt
  mylibrary.hpp
  test/
    CMakeLists.txt  // included from root's CMakeLists
    A.cpp //includes<mylibrary.hpp>
    B.cpp // also
    C.cpp // also
    ...
    Z.cpp // also

I am conviced that the compilation (of the tests) can be speed up by precompiling (pch) mylibrary.hpp

How do I tell CMake to use precompiled header if it can?

Also, the project uses different compilers, gnu, clang, nvcc, intel, (different build configurations) so the generation of the precompiled headers may not be always available.

The CMake has all the test working otherwise. I tried using target_precompile_headers as in

  target_precompile_headers(A PUBLIC ../mylibrary.hpp)
  target_precompile_headers(B PUBLIC ../mylibrary.hpp)
  ...

But it is actaully slower because it generates a new instance of the precompiled header for each test. (PRITAVE gives the same (slow) result, using INTERFACE doesn't do anything different).

alfC
  • 14,261
  • 4
  • 67
  • 118
  • 1
    My recommendation: spend several days in reading documentation of [GCC](https://gcc.gnu.org/) and of [cmake](https://cmake.org/). By experience, in *some* cases with *some* versions of GCC, precompiled headers can be slower. – Basile Starynkevitch Apr 03 '21 at 04:41
  • 2
    Have you tried using the `REUSE_FROM` option? https://cmake.org/cmake/help/git-stage/command/target_precompile_headers.html – Alan Birtles Apr 03 '21 at 07:11

0 Answers0