Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 626
rage Avatar asked Jul 28 '13 00:07

rage


People also ask

What is Stdafx C++?

"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.

Why we use Stdafx h in C++?

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.

How do I add a CPP file to Visual Studio?

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.


1 Answers

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.

like image 200
Hans Passant Avatar answered Oct 10 '22 10:10

Hans Passant