4

I have a very big project and for the sake of compilation speed I started to test precompiled headers.

I've setup everything now:

  • enable precompiled headers in VS (use them for the project, create it for the StdAfx.honly)
  • use multi processor compilation for all but for the StdAfx.h
  • automatically include the StdAfx.h in all my files via the force include of VS

The question that occurs now is following:

Do I need to remove all includes of all project files that I've added to the StdAfx.h file or is this unnecessary? Will the compiler skip any include automatically because he knows it's part of the StdAfx.h or should I remove them from each .h/.cpp file manually?

prom85
  • 16,896
  • 17
  • 122
  • 242
  • As long as the include guards are solid then this is not a problem. But not efficient of course, the pre-processor still plows through the file. The non-standard #pragma once can help. – Hans Passant Sep 26 '17 at 09:38
  • Do you think using `#pragma once` instead or additional to the include guards (currently I'm using the include guards in most of my files) will make a noticeable difference in a big project here? – prom85 Sep 26 '17 at 10:09

1 Answers1

2

The good practice is to make each file include all the headers directly required by this file. This way changing includes in particular file should effect only this file. If you start depending on headers included somewhere else it will make your project structure extremely fragile. And the single "common includes" file is an extreme case of such scenario. Use of precompiled header supposed to speedup compilation by prebuilding commonly included header files, but project files should never assume that something is already included there. So there is no need need to remove includes from ".h/.cpp", actually there are some tools that will populate precompiled header based on includes in project files. Compiler will skip files already included in precompiled header (or in other headers) because of header guards.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • 1
    That's what I wanted to know. I would like to keep the manual includes in my projects anyways, because I use some files in another old project which I do not want to adjust to use precompiled headers (it's compiled once a year only anyways). Just was not sure if having the includes in all files slows down the build process because it does not recognise that those are precompiled and makes unnecessary things because of this. Thanks – prom85 Sep 26 '17 at 09:30