0

I am investigating using precompiled headers to reduce our compile times.

I have read the documentaiton on the subject here: https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html, where I read the following:

Only one precompiled header can be used in a particular compilation.

On the project whose build time I would like to improve, there are often very Long lists of includes. The above leads me to Think that to get the most performance improvements, I would have to make a collection of common includes, put them into a single Header file, compile and include that Header file.

On the other hand, I prefer to list my dependancies in particular file explicitly, so I would be inclined to include first the precompiled Header, followed by the Manual list of actual Header files.

I have two questions related to this:

  1. Is my analysis and approach correct? Have I interpreted the statement correctly?

  2. Doing this, I will use this file (say stdafx.h) in many places, thereby including files I don't need. I would like to explicitly list my dependencies however, for code documentation purposes. Where I to do something like the following:

    #ifdef USE_PRECOMPILED_HEADERS 
    #include "stdafx.h"
    #else
    #include "dep1.h"
    #include "dep2.h"
    #endif
    

    I could periodically run a build without pre-compiled headers to check if all my dependencis are listed. This is a bit clunky however. Does anyone have a better solution?

If anyone has Information to help us obtain better results in our Investigation, I am happy to hear them.

Leedehai
  • 3,660
  • 3
  • 21
  • 44
Spacemoose
  • 3,856
  • 1
  • 27
  • 48
  • possible duplicate of [GCC and Precompiled Headers](http://stackoverflow.com/questions/12437955/gcc-and-precompiled-headers); see [this answer](http://stackoverflow.com/a/12438040/841108) still relevant for GCC 4.9 – Basile Starynkevitch Oct 22 '14 at 10:02

2 Answers2

1
  1. Yes, your observation is absolutely fine! You "would have to make a collection of common includes, put them into a single Header file, compile and include that Header file". This common header file is generally named as stdafx.h (although you can name it anything you want!)

  2. I am afraid I don't really understand this part of the question.

EDIT :

Do you also want the standard headers (like iostream, map, vector, etc.) to be included as dependencies in the code documentation?

Generally this must be a NO. Hence, you must include only those header files in stdafx.h which are not under your control (i.e., [1] standard language includes [2] includes from dependent modules (mostly exposed interface headers)). Rest all includes (whose source is in the current project/module) must be explicitly included in each header file wherever required, and not put in the pre-compiled stdafx.h.

CinCout
  • 9,486
  • 12
  • 49
  • 67
0

The above leads me to Think that to get the most Performance improvements, I would have to make a collection of common includes, put them into a single Header file, compile and include that Header file.

Yes, this observation is correct: You put most (all?) includes in one single header file, which is then precompiled.

Which, in turn, means that...

  • any compilation without the aid of that header being precompiled will take ages;
  • you are relying on naming conventions or other means (documentation?) to make the information link between things referenced in your individual translation unit and their declaration.

I don't much like precompiled headers for those reasons...

DevSolar
  • 67,862
  • 21
  • 134
  • 209