Ursescu Ionut
Ursescu Ionut

Reputation: 97

C++ precompiled-headers huge in size

I have 2 questions about c++ precompiled-headers feature.

1.What actually is happening when you make a .gch file (using GCC), what it contains ?

2.Why those files are so huge in size , but the final executable is so small.

Upvotes: 5

Views: 1951

Answers (1)

Quentin
Quentin

Reputation: 63164

When you precompile a header, it all begins like an usual compilation:

  • The preprocessor is run, including any dependent headers and performing macro substitution
  • The resulting source code is hander over to the compiler, which parses it and validates the syntax
  • Then the compiler produces the AST, the data structure which encodes the semantics of the code

Usually, this is done on .cpp files, and goes on afterwards to actually compile the AST and generate executable code. However, a header precompilation stops there, and the compiler dumps the AST inside the .gch file.

On further uses of this precompiled header, the compiler can then directly load the AST back from file and pick it up from there, skipping the costly processing listed above.

The .gch file is huge, because it contains a lot of information that was implicit in the original header. But it has no relation to the size of the final executable -- compiling with and without precompiled headers should produce the exact same result.

Upvotes: 6

Related Questions