What is stdafx.cpp file that is created automatically in a new c++ project in visual studio 2012

11,483

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.

Share:
11,483

Related videos on Youtube

rage
Author by

rage

Updated on September 06, 2020

Comments

  • rage
    rage over 3 years

    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?

    • Rapptz
      Rapptz over 10 years
      You can't compile .h files, you need the .cpp file to compile it. So it's the precompiled file; stdafx.cpp includes all the headers you want precompiled and compiles them. You can read more here

Related