From what i understand stdafx.h is a precompiled header file and is used to make compile time faster in visual studio. When i create a c++ project in visual studio 2012 there is also a stdafx.cpp. Can someone explain the relationship between stdafx.h and stdafx.cpp?
"Stdafx. h" is a precompiled header.It include file for standard system include files and for project-specific include files that are used frequently but are changed infrequently. which reduces compile time and Unnecessary Processing.
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.
Add a new source file to the project, as follows. In Solution Explorer, right-click the Source Files folder, point to Add, and then click New Item. In the Code node, click C++ File (. cpp), type a name for the file, and then click Add.
Pre-compiled headers can greatly speed the up the compilation of the .cpp files in your project. By convention, it is stdafx.h that #includes all of the .h files that you want to be precompiled. You can name it anything you want but the project template just picks stdafx.h
But before that can work, there must be one .cpp file that #includes stdafx.h and gets compiled first. Before all the other .cpp files in your project. Once that's done, the compiler can use the .pch file that was created and quickly compile all the other .cpp files, no longer having to actually #include the headers anymore.
Stdafx.cpp is that one .cpp file. It has a setting that's different from all the other .cpp files, it gets built with /Yc. The "Create precompiled header file" option.
UPDATE: after 28 years, Microsoft broken their "there used to be an std framework but nobody ever saw it" practices at VS2019. Now named "pch" instead of "stdafx", surely for the more obvious acronym. Just a name change, the mechanics are still the same.
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