I am trying to create a precompiled header file for my shared library using GCC. After making the necessary configuration and trying to build, I got these warnings:
cc1plus: warning: ./PrecompiledHeaders.h.gch/.c++: created and used with different settings of -fpic [enabled by default]
After some time searching, I found this piece of code which seems to suggest that PCH doesn't work with shared code:
/* -fpic and -fpie also usually make a PCH invalid. */
if (data[0] != flag_pic)
return _("created and used with different settings of -fpic");
if (data[1] != flag_pie)
return _("created and used with different settings of -fpie");
data += 2;
So I changed my library to a static library and the error disappeared and compilation time was reduced! So is it indeed not possible to have a PCH with shared library? If yes, is there any work around this?
The message and the code you posted doesn't suggest that PCH can't be used with shared code. It tells you that you can't use a PCH file compiled with -fpic when compiling code without -fpic, and vice-versa. Same for -fpie.
Depending on the architecture, -shared might imply -fpic or other such options.
You need to pre-compile your headers with the same options as you will compile the rest of your code. If you want to be able to use PCH for both static and dynamic builds, you'll need (at least) two different sets of pre-compiled headers.
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