I am using GCC precompiled headers in my project with multi-architecture build, but things break down when I try to place it in a directory different from current source's directory.
The file is included with double quotes, and it works if I change it to angle brackets, but the problem is that I have a lot of other projects that use the same precompiled header name, so changing all of them to angle brackets is not desirable as it may create ambiguity about which header to include in Visual Studio build of the same files.
GCC searches current directory for double-quote includes before its search path. I can work around it using -I-
option (e.g. -Ipch_dir.i686 -I-
), so that precompiled headers directory is searched before the current directory, but this option is deprecated. GCC suggests I use -iquote
, but it does not have the same effect as -I-
.
So the question is how do I make it work without changing all precompiled headers include directives to angle brackets and using a deprecated GCC switch?
GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.
gcc -I adds include directory of header files.
The angle brackets (<>) cause the preprocessor to search for the header file in the standard place for header files on your system, usually the /usr/include directory.
GCC needs to install corrected versions of some system header files. This is because most target systems have some header files that won't work with GCC unless they are changed. Some have bugs, some are incompatible with ISO C, and some depend on special features of other compilers.
I have found a workaround.
Build a precompiled header under a different name. For example is the header is a.h
, original precompiled header is pchdir.i686/a.h.gch
, build it as
gcc a.h -o pchdir.i686/a-precompiled.h.gch
Use GCC's -include
switch to make sure the renamed header is included before anything else (even before the original a.h
), e.g.
gcc -Ipchdir.i686 -include a-precompiled.h <other arguments> <source>
Final inclusion order in the source file will be: a-precompiled.h.gch
, a.h
, which I've checked with -H
. Original header is included, but is not actually processed because precompiled header has identical include guards (verified as well by inserting an #error
in the original header after building the precompiled one).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With