Setting up Precompiled Headers with GCC is problematic in the following situation:
- somefile.cpp and precompiledheader.h are in the same directory
- multiple build configurations are needed
Multiple build configurations require multiple generated .h.gch files, so they cannot be built side-by-side into the source dir. The -o option allows us to output them to different locations:
-o "out/$BuildConfig$/precompiledheader.h.gch"
The problem then is massaging the include searching behaviour for somefile.cpp so GCC will find and use the .h.gch. We can add the generated .h.gch location to the include search path:
-Iout/$BuildConfig$
but this directory is checked after the directory of the object being compiled. Since the precompiled header and the somefile.cpp are in the same directory, the precompiledheader.h will therefore always be found first in the source dir and the .h.gch will never be used.
I managed to make it work by injecting the .h.gch directory in the right spot by using -I- to suppress and re-add the somefile.cpp directory:
-I- -Iout/$BuildConfig$ -I.
This works, but -I- is marked as deprecated and generates a note:
cc1: note: obsolete option -I- used, please use -iquote instead
-iquote doesn't allow you to suppress the current directory and therefore can't work as a replacement.
Is there another way to do this without using -I-? Or do I just have to live with the "obsolete option" warning?
PS - Clang and MSVC allow you to explicitly specify the location of the pch/gch (via -include-pch and /Yu respectively) which avoids all this hassle. A similar option for gcc is sorely needed.