Include directories for target_precompile_headers

I have this CMakeLists.txt:

cmake_minimum_required(VERSION 3.18)
project(ocv VERSION 0.1.0)

find_package(fmt REQUIRED)
find_package(OpenCV REQUIRED)

add_executable(ocv main.cpp grid.cpp)


target_precompile_headers(ocv PRIVATE cppCommon.hpp subtCommon.hpp)

which produces this error on build:

[build] /home/paul/st/ocv/build/CMakeFiles/ocv.dir/cmake_pch.hxx:5:10: fatal error: /home/paul/st/ocv/cppCommon.hpp: No such file or directory

What is the way to define include directories for precompiled headers, if they are located in a different directory than CMakeLists.txt? I thought that include_directories will do that, and cppCommon.hpp is going to be found in /home/paul/include. It doesn’t in this case.


Header file names specified with angle brackets (e.g. <unordered_map> ) or explicit double quotes (escaped for the cmake-language(7), e.g. [["other_header.h"]] ) will be treated as is, and include directories must be available for the compiler to find them. Other header file names (e.g. project_header.h ) are interpreted as being relative to the current source directory (e.g. CMAKE_CURRENT_SOURCE_DIR) and will be included by absolute path.

So you need to write:

target_precompile_headers(ocv PRIVATE <cppCommon.hpp> <subtCommon.hpp>)


target_precompile_headers(ocv PRIVATE [["cppCommon.hpp"]] [["subtCommon.hpp"]])


target_precompile_headers(ocv PRIVATE
1 Like

Thank you. I should have parsed that documentation page more carefully.