cmake_pch.h 中不能包含标准库

Can't include standard libs in cmake_pch.h

我正在尝试在 cmake 中生成预编译的 header,其中包含我经常使用的标准库。当我 运行 cmake 时没有错误,但是当我构建它时它说它无法在 cmake_pch.h.

中找到 headers

这是我的 cmake 脚本片段,它添加了预编译 header:

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

这里是 运行ning cmake 生成的 makefile 的完整输出:

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

我只包含了我的 cmake 代码中与 pch 相关的部分,因为我之前只是将 libs 直接包含在我的 src 文件中并且一切正常,所以我认为它纯粹与 pch 相关。如果有帮助,我很乐意编辑和添加其余脚本。

您已将 C++ headers 添加到您的 target_precompile_headers 命令中,而且还向您的目标添加了 C 源代码。这行不通,因为 C 编译器不理解如何包含 C++ 标准 headers。查看错误信息:

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

从目标中删除 C++ headers 或为 C 和 C++ 源定义单独的目标。