[cfe-users] How to have clang ignore gch directories?

Richard Smith via cfe-users cfe-users at lists.llvm.org
Thu Apr 25 14:38:33 PDT 2019

On Tue, 23 Apr 2019 at 12:06, Paul Smith via cfe-users
<cfe-users at lists.llvm.org> wrote:
> How can I get clang to stop caring about gch directories for
> precompiled headers that were created by GCC?
> Currently my main build uses GCC and it generates .gch directories:
>   $ ls -1d foo_pch*
>   foo_pch.h
>   foo_pch.h.gch/
> When I compile my code using GCC, it works fine:
>   $ g++ ... -Winvalid-pch --include=foo_pch.h ...
> But then if I try to use tools based on clang with exactly the same
> arguments, it throws an error (with or without -Winvalid-pch):
>   $ clang++ ... --include=foo_pch.h ...
>   error: no suitable precompiled header file found in directory
>       './foo_pch.h.gch'
>   1 error generated.
> Note I'm really not using clang++, I'm using a tool linked with libLLVM
> which parses the code, but I get the same behavior there.
> Using strace I can see clang first look for .pch then .pth (neither of
> which exist, which is fine), and finally .gch.  Apparently clang cannot
> parse the GCC-generated precompiled header file.  I don't care if clang
> can't read those files, I just want it to include the .h file.
> How can I tell clang to just ignore the GCC-specific precompiled
> headers, or equivalently ignore precompiled headers altogether?  I
> can't delete the gch directories because I need them for my build. I've
> read through the clang docs and can't find any likely-looking options.
> It's really frustrating when clang tries to appropriate GCC-specific
> options and configurations when it's not actually compatible with them.
> It makes modern multi-compiler development so unnecessarily difficult.

Sorry about that. Clang is trying to be a transparent drop-in
replacement for build systems that are designed for GCC, and that
means that -include needs to automatically pick up a corresponding
.pch / .gch file if a prior build step put one there. Unfortunately we
don't do any validation to check whether that's Clang's file or one
generated by GCC at that layer :(

You can work around this by using "-Xclang -include -Xclang foo_pch.h"
instead of "-include foo_pch.h"; that will bypass the driver logic
that tries to convert -include to -include-pch.

Also, -include=foo_pch.h won't do what you think: in GCC (and in
Clang, because we usually follow GCC conventions even when they're
weird), that will include a file named "=foo_pch.h". (The space
between -include and the filename is optional, and there is no
optional equals, so if you put one in it ends up as part of the
argument value. This is the same behavior as for -I etc.)

More information about the cfe-users mailing list