Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do Visual Studio's precompiled headers contain?

The question concerns the contents of the .pch binary created by the Visual Studio compiler. What does it contain? Is it only the parsed tree of the header files, or object code as well?

Consider this example:

// myheader.h
#include <vector>
class A {
public:
  void add(int i) { v.push_back(i); }
private:
  std::vector<int> v;
};

Would including this header in the set to be precompiled result in a complete template instantiation of vector<int> being compiled and added to the .pch?

To give some more context; if only the parse tree is precompiled, this means that object code for instantiated templates will still be created once per compile unit with resulting increases in compile and link time. So "unity builds"/reducing compile units would still be a relevant factor in decreasing build time even with precompiled headers enabled.

like image 435
Viktor Avatar asked Nov 14 '22 04:11

Viktor


1 Answers

Though I cannot say definite thing about the internal of PCH, as for unity builds, even if vector<int> is fully instantiated in PCH, I think unity builds still has some advantage.

As you may know, C++ has to keep one-definition-rule(ODR). Linker has to unite template's definitions scattered in each object file into one.
That is, roughly speaking, C++ has to do the following:

  1. Instantiate templates
  2. Write object file
  3. Reread object file for link
  4. Resolve ODR

Unity builds make it possible to save the above load in some degree.
However, I cannot say the above saving will surpass non-unity compilation system with PCH in your environment.

like image 121
Ise Wisteria Avatar answered Dec 28 '22 06:12

Ise Wisteria