stdafx.h: When do I need it?

67,708

Solution 1

If you don't want to use precompiled headers, then there is no point to using a standard include file - this will slow down the build for every file that includes it and cause them to include extra stuff that they do not need. Get rid of it and just include the headers they need.

Solution 2

stdafx.h is just another header file. If you feel that you don't need it then feel free to not include it and remove it from the project.

However it's quite typical to have a file like stdafx.h exactly for precompiled headers to work and to not include all the stuff manually in each source file.

Solution 3

Even without pre-compiled headers stdafx.h could be handy as it groups header includes and definitions common for all files.

You can of course choose to repeat all these definitions in every file. stdafx.h is not strictly necessary.

Solution 4

As others have mentioned: if you don't need precompiled headers, you don't really need stdafx.h . And using it just to group common includes is pretty bad practice, actually.

In fact, even when using precompiled headers, it's good practice to include the headers your process actually needs after stdafx.h (or precompiled.h or whatever you want to call it) - along with #ifdef magic in your precompiled header to turn off the usage of PCH.

Why? In order to check your module dependencies. Being able to disable your PCH lets you catch whether you're including necessary modules or not, and you can then write a tool to check your module interdependencies by parsing your .cpp and .h files (excluding the PCH header, of course).

Share:
67,708
Markus Joschko
Author by

Markus Joschko

I work with GPUs on deep learning and computer vision.

Updated on July 13, 2022

Comments

  • Markus Joschko
    Markus Joschko almost 2 years

    I see so much code including stdafx.h. Say, I do not want pre-compiled headers. And I will include all the required system headers myself manually. In that case is there any other good reason I should be aware of where I require stdafx.h?