0

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.

  • @hvd - That does fix the problem - thanks for the link! I'm actually pretty happy with that as a solution (since it seems to be still be ok with msvc and clang also). Should this question be closed as a duplicate, left open for the larger question of "is there a better/different way", or should we add an answer as it applies to this specific use-case? – Franta Fulin Dec 29 '15 at 20:03
  • Glad it helped. :) The question itself (how to get the behaviour of `-I-` without `-I-`) is an exact duplicate, the answer (you can't) is also exactly the same, and if the additional information of how to work around the problem is exactly the same yet again, I think it's okay to close this question. Done. Questions closed as duplicates are not normally deleted, so people searching for the same thing may find your question and follow the link to that other question. –  Dec 29 '15 at 20:30

0 Answers0