为什么 g++ 找不到 -I include-path 中的 precompiled-header?

Why doesn't g++ find the precompiled-header that's in the -I include-path?

我正在尝试构建一个预编译的 header 和一个可执行文件,如下所示:

g++ -g -Wall -std=c++17 \
    -c ./src/pch.hpp -o ./build/pch.hpp.gch

g++ -g -Wall -std=c++17 \
        -c ./src/*.cpp  \
        -I./build/ -include pch.hpp

pch.hpp.gch 文件已正确创建。但是对于每个 .cpp 文件,我都收到以下错误:

1 error generated.
<built-in>:1:10: fatal error: 'pch.hpp' file not found
#include "pch.hpp"

我认为我的编译行是正确的,基于 gcc Precompiled Headers documentation:

为什么我的编译行没有按预期工作?


我尝试了一些方法给我更好的结果,但它们对我来说不正确。

如果我修改 include 以搜索 .gch 文件,那么会找到该文件, 符合我的预期。即 -include pch.hpp.gch,而不是 -include pch.hpp .
但是,PCH 被解释为二进制文件,编译失败:

g++ -g -Wall -std=c++17 \
        -c ./src/*.cpp  \
        -I./build/ -include pch.hpp.gch
./build/pch.hpp.gch:2:22: error: source file is not valid UTF-8

我对 #include <pch.hpp.gch> 无法编译并不感到惊讶。但我提到这一点是因为它似乎表明在我的原始命令中搜索了 build 文件夹(如我所料),但是知道使用 .gch 文件而不是常规文件的机制header 未激活。奇怪。

或者,如果我将 src 文件夹添加到 header 搜索路径,它会起作用:

g++ -g -Wall -std=c++17 \
        -c ./src/*.cpp  \
        -I./src/ -I./build/ -include pch.hpp

我不明白为什么添加 另一个 ,不相关的 include-path 可以解决任何问题。奇怪。


我目前的工作解决方案是完全删除 -I include-path 指令,并 指定 build/pch.hpp 的更完整路径:

g++ -g -Wall -std=c++17 \
        -c ./src/*.cpp  \
        -include ./build/pch.hpp

这个按预期工作。不过,我不确定为什么需要它,而且它很奇特且不方便。

PCH 应该这样使用吗?为什么我原来的线路不起作用,我应该做什么?

来自documentation

A precompiled header file is searched for when #include is seen in the compilation. As it searches for the included file (see Search Path in The C Preprocessor) the compiler looks for a precompiled header in each directory just before it looks for the include file in that directory. The name searched for is the name specified in the #include with ‘.gch’ appended. If the precompiled header file cannot be used, it is ignored.

For instance, if you have #include "all.h", and you have all.h.gch in the same directory as all.h, then the precompiled header file is used if possible, and the original header is used otherwise.

意味着编译器在构建cpp时必须能够找到h-file和gch-file。所以他们应该在同一个目录或同一个包含搜索路径。