Clang CMAKE 预编译头文件

Bra*_*nds 3 c++ cmake clang precompiled-headers

我找到了很多关于如何为 MSVC 使用预编译头的示例,但我似乎找不到任何使用 clang 的示例。从this SO post我可以看到clang命令,但我想知道它们如何转换为cmake:

使用预编译头文件减少 clang 编译时间

创建预编译的头文件包括所有你没有更改的头文件 > > 到 Query.h 并使用:

clang -cc1 Query.h -emit-pch -o Query.h.pch 使用预编译头类型:

clang -cc1 -include-pch Query.h.pch Query.cpp -shared -o libquery.so; Query.cpp 需要包含 Query.h

编辑:使用 clang 6 和 cmake 3.11.2

Bra*_*nds 5

找到了一个对我有用的解决方案:

# 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)
Run Code Online (Sandbox Code Playgroud)