Question

I believe I understand the basics concerning precompiled headers and I can't think of any reason why a precompiled header couldn't be used inside a of library, but I've been told that it's either not possible or shouldn't be done (can't remember which).

I have a project where a shared library will be statically linked by multiple others and I would like to use precompiled headers inside the shared library for my external STL/Boost dependencies but I'm not sure if it's possible. I'm targeting OS X and Windows systems and common searches for details seem to point more towards Windows solutions involving stdafx.h which leads me to believe that using precompiled headers is customarily a Windows practice.

My question is simply this:

Can/should I use a precompiled header inside of a library that I'll be linking statically to other projects? If not, is using a convenience header for, say, all of my STL/Boost dependencies a good thing to pursue? I'm referring to something like this:

// common.h
#pragma once

#include <boost/this>
#include <boost/that>
#include <string>
#include <vector>
#include <other_stl_header>

// Foo.h
#include "common.h"

class Foo { ... }

// Foo.cpp

#include "Foo.h"
...

EDIT: I should specify, I have no expectation that the precompiled header be shared with anything other than the library I'm compiling. My question is related to whether or not it's possible to create a precompiled header that is used for the compilation of that library.

Was it helpful?

Solution

Once you have finished the compilation, there is no more usage for precompiled header and they have no effect on the linkage stage (that includes the shared library and any other linkable components). You can use it to speed up the compilation of your shared library but that's it, the static library is an outcome of the compilation, with or without precomiled headers usage.

OTHER TIPS

A precompiled header is not an executable or any kind of linkable format. The compiler itself can define any kind of format for a precompiled haeder. Maybe it is simply tokenized, maybe some other includes and the definitions of other includes are already evaluated. The only intention for precompiled headers is a speed up for the compiler.

gcc also uses precompiled headers on demand but it is not a common practice to use this feature. gcc generates .gch files for the output of precompile stage.

The simple answer to your question: No

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top