Using Pre-Compiled Headers with Cmake

How to add precompiled headers in CMake for visual studio generator

The command target_precompile_headers has been added in CMake 3.16.

You'll either have to upgrade your CMake distribution (The one shipped inside VS2019 is quite up to date) or install the latest from the installer.

Otherwise, you'll have to use cotire for precompiled header, but my recommendation would be to upgrade since it's usually quite easy to do, and you probably already have an up to date version on your computer shipped with visual studio.


Since you updated your question.

The command target_precompile_headers is not doing what you think. You don't provide your PCH there, you provide what header should be part of the generated PCH. If the file ugpch.h contains includes for headera.h, headerb.h and headerc.h, then your precompiled header command should look like this:

target_precompile_headers(UGame PRIVATE headera.h headerb.h headerc.h)

The actual precompiled header will be generated by CMake.

Of course, you can still use ugpch.h as your precompiled header. CMake will simply use it to generate its own. As you observed.

After that, remove manual inclusion of the header. It will be included automatically.

From the CMake documentation:

The list of header files is used to generate a header file named cmake_pch.h|xx which is used to generate the precompiled header file (.pch, .gch, .pchi) artifact. The cmake_pch.h|xx header file will be force included (-include for GCC, /FI for MSVC) to all source files, so sources do not need to have #include "pch.h".

Why does it work like that you ask? CMake support PUBLIC precompiled header. If you link to multiple target that each have public precompiled headers, then your target inherit all of them. Since compilers usually accept one precompiled header, CMake has to generate agglomerated headers for that to work.

CMake uses C-style precompiled headers instead of C++ version of the precompiled header

Thanks to @drescherjm, adding target_precompile_headers(${PROJECT_NAME} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_SOURCE_DIR}/MyProject/pch.h>" got rid of the errors.

Force CMake to rebuild precompiled headers

In CMake, and assuming use of the target_precompile_headers() command, you can disable precompiled headers for a specific CMake target by setting the DISABLE_PRECOMPILE_HEADERS property:

set_target_properties(MyTarget PROPERTIES 
DISABLE_PRECOMPILE_HEADERS ON
)

or you can disable precompiled headers for the entire project by setting this in the top-level CMake file:

set(CMAKE_DISABLE_PRECOMPILE_HEADERS ON)

To get CMake to rebuild all of the precompiled headers, you could simply delete those that have been generated, so they are re-generated.

Can .gch files generated from CMake's target_precompile_headers be shared across targets?

You need to use target_precompile_headers per each target you want PCHs for. With one producer you use like you did and then the consumers should look like this:

target_precompile_headers(target1 REUSE_FROM pch)

And you do not need to link against your pch "library".

Docs actually have the section for it.



Related Topics



Leave a reply



Submit