Issue
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?
Solution
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. The project templates now name these files pch.h and pch.cpp instead of stdafx.h and stdafx.cpp. Surely for the more obvious acronym. Just a name change, the mechanics are still the same.
Answered By - Hans Passant Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.