Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use stdafx.h properly?

What is the proper way to use stdafx.h, in terms of separating dependencies?

If I put everything in there, then my compiles are blazing fast, and all I need to say in any file is

#include "stdafx.h"

with the caveats that:

  1. I no longer know which files depend on which headers.
  2. It reduces modularity, making my code less reusable.
  3. I can no longer separate C #includes from C++ ones (e.g. cstring versus string.h) without a great deal of pain. (Speaking of which, does this even matter?)

If I put nothing in there, I can organize everything well -- but then my compiles slow down dramatically.

If I put everything in there and also include every header in my individual files, then I get the best of both worlds, with the caveat that now I have to keep track of synchronization issues.

Is there a well-known/well-accepted solution to this problem?

like image 693
user541686 Avatar asked Aug 26 '11 08:08

user541686


People also ask

How does Stdafx H work?

Precompiled Header stdafx. h is basically used in Microsoft Visual Studio to let the compiler know the files that are once compiled and no need to compile it from scratch. The compiler would always compile this header files from scratch.

Why do I need Stdafx H?

The file stdafx. h is usually used as a precompiled header file. Precompiled headers help to speed up compilation when a group of files in a project do not change.

What does Stdafx stand for?

h (named stdafx. h before Visual Studio 2017) is a file generated by the Microsoft Visual Studio IDE wizard, that describes both standard system and project specific include files that are used frequently but hardly ever change. The afx in stdafx. h stands for application framework extensions.


2 Answers

You shouldn't put your own header files in stdafx.h since your code is likely to change and will cause your whole program to recompile.

I usually put standard headers and other libraries in there, such as all boost includes.

like image 90
Marlon Avatar answered Sep 19 '22 13:09

Marlon


When using precompiled headers, you should make sure your project compiles cleanly even without PCH present (so treat it as optimisation, not a replacement for includes in every TU). Aside from that, don't put absolutely everything in there (and more importantly, don't ever put headers from your own project — PCH should contain only those that don't change or change very rarely — system libraries, external dependencies), but rather try to keep most used/biggest things precompiled.

like image 27
Cat Plus Plus Avatar answered Sep 20 '22 13:09

Cat Plus Plus