How to use precompiled header?

Hei Chan
Wed Jun 23 19:32:00 GMT 2010

Thanks Ian!

I think that I am getting close:

cc1plus: warning: /opt/3rdPartyLib/src/A/B.h.gch: not a PCH file

B.h.gch is generated by the 3rd party make script.  When I ran the make script, it output something like:
g++  -fPIC -O -O3 -DUSING_PCH -D_REENTRANT -I"/usr/local/include/boost" -I"/usr/local/." -I"../src" -I"/opt/xerces-c-3.1.1/src" -c -o A/B.h.gch A/B.cpp

Not sure whether it used -o instead of -x causing the problem.

Any suggestion?

Thanks in advance.


----- Original Message ----
From: Ian Lance Taylor <>
To: Hei Chan <>
Sent: Wed, June 23, 2010 7:58:51 AM
Subject: Re: How to use precompiled header?

Hei Chan <> writes:

> But I still don't fully understand how I can use a 
> precompiled header in a 3rd party library.
> For example, I have:
> - /opt/3rdPartyLib/src/A/B.h
> - /opt/3rdPartyLib/src/A/B.h.gch
> Then, I tried to compile my code with:
> g++ -I/opt/3rdPartyLib/src -H 
> -include A/B.h -o myApp.o myApp.cpp
> But I got:
> x 
> /opt/3rdPartyLib/src/A/B.h.gch
> So it seems like for some reasons, /opt/3rdPartyLib/src/A/B.h.gch isn't used.
> My user doesn't have 
> read-write permission to /opt/, but it does have read permission to 
> /opt/3rdPartyLib/src/A/B.h.gch I think that the permission is 
> fine.
> Any suggestion?

Try using the -Winvalid-pch option.

Most likely the PCH was generated by a different version of the
compiler.  In gcc a PCH is only valid for the exact compiler which
generated it.  This does make it quite difficult to use PCH's built by
third parties.  However, you should be able to build your own PCH from
the third party header file, and use -include to pick it up first.
Assuming the third party header uses the usual #ifndef/#define
construct to only get included once, that should let you use PCH
without having to change your source code.



More information about the Gcc-help mailing list