Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang CMAKE Precompiled headers

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

like image 474
Braden Edmunds Avatar asked Dec 03 '25 15:12

Braden Edmunds


1 Answers

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)
like image 121
Braden Edmunds Avatar answered Dec 05 '25 07:12

Braden Edmunds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!