Can't include standard libs in cmake_pch.h

Viewed 542

I am trying to generate a precompiled header in cmake that contains my regularly used standard libs. When I run cmake there is no errors but when I build it says it can't find the headers in cmake_pch.h.

Here is the snippet of my cmake script that adds the precompiled header:

target_precompile_headers(fae-core PRIVATE
    <algorithm>
    <cstddef>
    <fstream>
    <string>
    <sstream>
    <memory>
    <chrono>
)

Here is the full output from running the makefile that cmake generates:

Scanning dependencies of target fae-core
[  6%] Building CXX object core/CMakeFiles/fae-core.dir/cmake_pch.hxx.gch
[ 12%] Building C object core/CMakeFiles/fae-core.dir/cmake_pch.h.gch
In file included from <command-line>:32:
/home/finn/dev/fae/build/core/CMakeFiles/fae-core.dir/cmake_pch.h:4:10: fatal error: algorithm: No such file or directory
    4 | #include <algorithm>
      |          ^~~~~~~~~~~
compilation terminated.
make[2]: *** [core/CMakeFiles/fae-core.dir/build.make:78: core/CMakeFiles/fae-core.dir/cmake_pch.h.gch] Error 1
make[1]: *** [CMakeFiles/Makefile2:136: core/CMakeFiles/fae-core.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I've only included the section of my cmake code relating to the pch as I was previously just including the libs directly in my src files and everything was working fine so I assume it's purely related to the pch. I'm happy to edit and add the rest of my scripts if it's helpful.

1 Answers

You have added C++ headers to your target_precompile_headers command but also added a C source to your target. This cannot work as the C compiler does not understand how to include C++ standard headers. See the error message:

[ 12%] Building C object core/CMakeFiles/fae-core.dir/cmake_pch.h.gch

Either remove the C++ headers from your target or define separate targets for C and C++ sources.

Related