3

Is it possible to use Precompiled Headers like a library is used? For example can I create a header containing preprocessor like so:

#include <iostream>
#include <string>
#include "boost_headers.hpp"

Compile it into a PCH and then distribute the PCH without having to distribute the headers files used to create it?

Soapy
  • 557
  • 14
  • 29
  • I don't think so. The compiler checks if the pch needs to be updated. For that it needs the header files. Besides that, the pch file format is proprietary, and is not even compatible between different versions of the same compiler. – wimh Feb 24 '15 at 10:54
  • Why do not you want to distribute normal headers? – Piotr Siupa Feb 24 '15 at 10:56
  • @NO_NAME Just to make its easier/smaller to distribute library source code – Soapy Feb 24 '15 at 10:57
  • 1
    If you just want to distribute as a single file and not require compilation I guess you could cat all your headers together. – Praxeolitic Feb 24 '15 at 10:58
  • 1
    In C++ you traditionally provide the header files required by the library. Whether a PCH is used, is a matter for the client and not provider. – Neil Kirk Feb 24 '15 at 10:58
  • @Wimmel But lets say I distribute a MSVC pch and a GCC pch, could they not be used on their respective platforms? – Soapy Feb 24 '15 at 10:59
  • @Soapy I don't think it is sufficient argument. This will prevent prompting syntax. – Piotr Siupa Feb 24 '15 at 11:00
  • @NO_NAME well shared/static/dynamic libraries we're made to distribute functionality in one file (.lib / .dll / .so), is this not the same for a PCH? For example boost is quite big and even though parts can be extracted using _bcp_ it still would be nice to have it all contained in one PCH to distribute. – Soapy Feb 24 '15 at 11:03
  • 1
    You are confusing precompiled headers, which are a LOCAL optimization to compiler speed, and a single giant header for "convenience" purposes only. They are not the same thing. – Neil Kirk Feb 24 '15 at 11:14
  • @NeilKirk Yea I get that now thanks to Sebastian Redl – Soapy Feb 24 '15 at 11:15
  • @Soapy I think that .dll are packet to single file as a convenience to the end user. Programmer can handle a whole tree of headers. They have many advantages. First of all you can always look in the them and see something you do not remember. Also your development environment can be analyzed and displayed tips when writing code. I'm sure these things are more important for the programmer than the number of files. – Piotr Siupa Feb 24 '15 at 11:19

2 Answers2

5

No. Precompiled headers have multiple aspects that make them unsuitable as a distribution format.

  • They're unstable. Any change to the compiler or the build settings invalidates precompiled headers.
  • They're not modular. A PCH must be the first thing included from a source file (or even via the command line). As a corollary, you cannot include more than one PCH. In other words, if you distribute your library as a PCH, you're basically saying that your library is the only thing the user will ever need.

The problem is that PCHs are typically (in MSVC and GCC, Clang is slightly different) implemented as a simple dump of the internal compiler state. Loading a PCH means replacing the compiler state with the state in the PCH. There is no middle ground - compilers cannot merge the state from the PCH into their current state.

Clang's PCHs are implemented differently, but still have to be the first thing, because if anything came before the PCH, the C++ compilation model would still mean that the PCH is potentially invalid. Clang's module support basically describes a changed compilation model that allows merging PCHs. (There's also a lot of work involved in doing the merging correctly.)

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • "implemented as a simple dump of the internal compiler state" so even minor variations in the same compiler, e.g MSVC version 1.0 and MSVC version 1.1 would require would require a different pch? – Soapy Feb 24 '15 at 11:06
  • 5
    Absolutely. PCHs are completely unportable and sensitive to the slightest changes of the environment. – Sebastian Redl Feb 24 '15 at 11:07
3

No. Precompiled headers have can only be used by compiler that created them. For GCC it even meands the same binary.

Adam Sosnowski
  • 1,126
  • 9
  • 7
  • So If I made a pch in gcc, could three separate gcc projects not use the same pch? – Soapy Feb 24 '15 at 10:58
  • PCHs could be shared among the projects, because you would probably use the same compiler version for them But there are a lot of conditions that make successful sharing unlikely. For example, a lot of compilation options have to be specified in the same way for these various projects. In general, you can think of pch as means for speeding up consecutive compilation of a single project. – Adam Sosnowski Feb 24 '15 at 11:05