14

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?

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • What's wrong with just having one set of precompiled headers in a single location that's used across your projects? Or renaming your precompiled header if it needs to be unique for your project? – Owen S. Jul 02 '10 at 00:32
  • @Owen S. I'd like to avoid making substantial changes to global project structure/build system. – Alex B Jul 02 '10 at 00:42

1 Answers1

11

I have found a workaround.

  1. 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
    
  2. 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>
    
  3. 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).

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • 1
    +1 @alex nice, I'm running into a similar problem where I want gcc to look for the pch file in a different directory from the location of the header. I'll give your workaround a try and hope it works. Something that's really been irritating me about gcc is why don't they make the pch feature easier to use? I find the programmer/user way too often has to fiddle around with the tool settings that really we shouldn't have to worry about. I mean it should just simply 'work'. – greatwolf Dec 17 '10 at 10:18
  • 1
    +1 @alex. Thanks for sharing the solution, it helped me too. Just want to note that you can simplify it significantly: no need to rename the precompiled output and add its folder to the includes. After generating the precompiled output in your configuration dependent folder use -include with its unique path, not just the name. In your case: `gcc -include pchdir.i686/a.h ... ` is all you need. (works on os x clang) – zzz Jun 04 '15 at 03:39