ChatGPT解决这个技术问题 Extra ChatGPT

What is "stdafx.h" used for in Visual Studio?

A file named stdafx.h is automatically generated when I start a project in Visual Studio 2010. I need to make a cross-platform C++ library, so I don't/can't use this header file.

What is stdafx.h used for? Is it OK that I just remove this header file?

If I get compile error related to stdafx.h, I generally set the settings to do not create or use this file..
Article: StdAfx.h for Novices - viva64.com/en/b/0265
You can use the header file just fine on other platforms, to them is just a normal header file. It just offers no performance benefit there.

A
Alexander

All C++ compilers have one serious performance problem to deal with. Compiling C++ code is a long, slow process.

Compiling headers included on top of C++ files is a very long, slow process. Compiling the huge header structures that form part of Windows API and other large API libraries is a very, very long, slow process. To have to do it over, and over, and over for every single Cpp source file is a death knell.

This is not unique to Windows but an old problem faced by all compilers that have to compile against a large API like Windows.

The Microsoft compiler can ameliorate this problem with a simple trick called precompiled headers. The trick is pretty slick: although every CPP file can potentially and legally give a sligthly different meaning to the chain of header files included on top of each Cpp file (by things like having different macros #define'd in advance of the includes, or by including the headers in different order), that is most often not the case. Most of the time, we have dozens or hundreds of included files, but they all are intended to have the same meaning for all the Cpp files being compiled in your application.

The compiler can make huge time savings if it doesn't have to start to compile every Cpp file plus its dozens of includes literally from scratch every time.

The trick consists of designating a special header file as the starting point of all compilation chains, the so called 'precompiled header' file, which is commonly a file named stdafx.h simply for historical reasons.

Simply list all your big huge headers for your APIs in your stdafx.h file, in the appropriate order, and then start each of your CPP files at the very top with an #include "stdafx.h", before any meaningful content (just about the only thing allowed before is comments).

Under those conditions, instead of starting from scratch, the compiler starts compiling from the already saved results of compiling everything in stdafx.h.

I don't believe that this trick is unique to Microsoft compilers, nor do I think it was an original development.

For Microsoft compilers, the setting that controls the use of precompiled headers is controlled by a command line argument to the compiler: /Yu "stdafx.h". As you can imagine, the use of the stdafx.h file name is simply a convention; you can change the name if you so wish.

In Visual Studio 2010, this setting is controlled from the GUI via Right-clicking on a CPP Project, selecting 'Properties' and navigating to "Configuration Properties\C/C++\Precompiled Headers". For other versions of Visual Studio, the location in the GUI will be different.

Note that if you disable precompiled headers (or run your project through a tool that doesn't support them), it doesn't make your program illegal; it simply means that your tool will compile everything from scratch every time.

If you are creating a library with no Windows dependencies, you can easily comment out or remove #includes from the stdafx.h file. There is no need to remove the file per se, but clearly you may do so as well, by disabling the precompile header setting above.


Even if you used just for files from std namespace you get a speed benefit
omg ,very nice answer indeed.i was looking for standard compliant c compiler. it turns out that i can disable micro$oft extensions from project properties , change the compiler from "auto" to "c" and your are pretty much having "standard" compiler and IDE.
@Rishi: by 'line', do you mean #include "stdafx.h"? Sure, but that's just a standard #include. The "MS extension" part is just a compiler performance optimization; it doesn't change the semantics of having a header file that happens to be called "stdafx.h". Note that if you remove the include and your code depend on anything that was included via stdafx.h, you are going to have to include it directly.
@Youda008, not quite true. Before compiling a code file, the contents of headers are simply and literally "pasted" on the spot where you #include them in the source file (done by the same 'preprocessor' step that evaluates macros). The resulting total file is then passed to the actual compiler, which never sees a header file as a separate entity. You only put declarations on header file because that's what works well on a header file - it's a conventional rule. Try it! Create a header file with a whole program, then create a source file that only has an #include for it. It compiles fine.
Historical curiosity. The name of stdafx.h dates from around 1992, when MFC was called 'Application Framework Extensions' before its release. Visual Studio 2015 still defaults to the name ..
c
casablanca

It's a "precompiled header file" -- any headers you include in stdafx.h are pre-processed to save time during subsequent compilations. You can read more about it here on MSDN.

If you're building a cross-platform application, check "Empty project" when creating your project and Visual Studio won't put any files at all in your project.


There's nothing in this file that wouldn't work on other platforms. It might slow down the compilation there, if the compiler doesn't support precompiled headers, but it should not break it. It's just a header file that includes other header files.
@detunized: Maybe my answer made it sound otherwise, so thanks for clarifying that part.
A
Akash das

"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. You can read more about it

http://www.cplusplus.com/articles/1TUq5Di1/

https://docs.microsoft.com/en-us/cpp/ide/precompiled-header-files?view=vs-2017


R
Ranjeet R Patil

Defination: "Stdafx.h" is a precompiled header.

Precompiled means once compiled no need to compile again.

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.

For Example: If you are including the below windows header files.

Code:

#include <windows.h>
#include <string.h>

int main()
{
 //your code
return 0;
}

The compiler would always compile these header files from scratch.

But if you include #include "stdafx.h" before these headers then the compiler will find the compiled header files from stdafx.h and does not compile it from scratch.

For the very first time the compiler compiles as normal from scratch.

Code:

#include "stdafx.h"
#include <windows.h>
#include <string.h>

int main()
{
//your code
 return 0;
}

Advantages:

Reduces compile time.

Reduces Unnecessary Processing.

Source: http://www.cplusplus.com/articles/1TUq5Di1/


L
Luke

I just ran into this myself since I'm trying to create myself a bare bones framework but started out by creating a new Win32 Program option in Visual Studio 2017. "stdafx.h" is unnecessary and should be removed. Then you can remove the stupid "stdafx.h" and "stdafx.cpp" that is in your Solution Explorer as well as the files from your project. In it's place, you'll need to put

#include <Windows.h>

instead.