I have a header file called stdafx.h and this one is precompiled of course. I've read that I should include these files into my .cpp files, but some of these statements are already needed in the header file coming with that.
Should I add the stdafx into my header or into my cpp? I thought it was good practise to put it into the header, but I seem to be obliged to put it into the header instead.
Example:
stdafx contains freeglut.
my class header file has an attribute of GLenum.
Should I include the stdafx into the .h of the class?
stdafx. h is basically used in Microsoft Visual Studio to let the compiler know that the files are once compiled and no need to compile it from scratch.
h should be included into each and every one of your source files, before you include anything else. It shouldn't be included in any other header files, but it doesn't matter if it is. stdint. h and stdbool.
#include "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.
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.
stdafx.h should be the first include in EVERY cpp file in your project.
Therefore if the stdafx is the first include in the cpp file, then the compiler will have everything the header file needs, when it hits the header-file in the Cpp file.
e.g.
You have A.cpp & A.h.
A.h needs std:string.
you have B.cpp & B.h
B.h needs A.h therefore B.h needs std::string too.
Because it's good practice, you put #include <string>
in stdafx.h.
Your build fails because nothing can see std::string
Now put stafx.h as the first include in A.cpp and B.cpp.
When the compiler hits A.cpp, it picks up the include for <string>
, then picks up A.h, and everything is happy because we know what std::string is.
The compiler now hits B.cpp, again it includes stdafx first, which brings <string>
, then hits B.h, which brings A.h which is again happy because std::string has already been included.
Hope this helps.
Ask yourself these two questions before including something in stdafx.h
If the answer is "No" to either of those then don't include it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With