3

I've found a lot of examples on how to use precompiled headers for MSVC, but I can't seem to find any examples using clang. From this SO post I can see the clang commands but I'm wondering how they translate into cmake:

Decrease clang compile time with precompiled headers

to create pre-compiled header include all the headers you don't change > > into Query.h and use:

clang -cc1 Query.h -emit-pch -o Query.h.pch to use the pre-compiled header type:

clang -cc1 -include-pch Query.h.pch Query.cpp -shared -o libquery.so; Query.cpp needs to include Query.h

Edit: Using clang 6 and cmake 3.11.2

Community
  • 1
  • 1
Braden Edmunds
  • 177
  • 1
  • 7
  • I'd guess you'd probably get a greater benefit by using a compiler cache like [ccache](https://ccache.samba.org) than by using precompiled headers and you'd avoid all the problems with PCHs at the same time. – Jesper Juhl Jan 15 '19 at 17:21
  • I'm already using ccache, but the project is pretty large. If any of the main header files change it takes ~20 minutes to compile again. I do have one stdinc.hpp file that never changes and I think if I can make it a precompiled header the compilation time will improve (just a theory). – Braden Edmunds Jan 15 '19 at 17:23
  • 2
    The answer suggested as a duplicate shows a lot of MSVC examples, as the OP complained about, so I doubt it answers his question. – Michael Surette Jan 15 '19 at 17:41
  • Agreed @MichaelSurette not sure why it was marked duplicate. – Braden Edmunds Jan 15 '19 at 17:53
  • 1
    Oops, sorry for wrong duplicate. Found "MSVC" word, but didn't notice that this is a no-word for given question. @BradenEdmunds: When refer to the other question in your question post, it is better to cite key phrase(s) from that question/answers. Two additional lines of code wouldn't harm for your question post, but with them one needn't to open the referenced question for understand what do you want. – Tsyvarev Jan 15 '19 at 19:42

1 Answers1

5

Found a solution that worked for me:

# add the pch custom target as a dependency
add_dependencies(corelib pch)

# add the flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include-pch ${CMAKE_CURRENT_BINARY_DIR}/stdinc.hpp.pch")

# target
add_custom_target(pch COMMAND clang -x c++-header ${CMAKE_CURRENT_SOURCE_DIR}/src/stdinc.hpp -o ${CMAKE_CURRENT_BINARY_DIR}/stdinc.hpp.pch)
Braden Edmunds
  • 177
  • 1
  • 7