Purpose of Stdafx.H

Purpose of stdafx.h

stdafx.h is a file, generated by
Microsoft Visual Studio IDE wizards,
that describes both standard system
and project specific include files
that are used frequently but hardly
ever change
.
Compatible compilers (for example,
Visual C++ 6.0 and newer) will
pre-compile this file to reduce
overall compile times
.

Visual C++ will
not compile anything before the
#include "stdafx.h" in the source file
, unless the compile option
/Yu'stdafx.h' is unchecked (by
default); it assumes all code in the
source up to and including that line
is already compiled.

http://en.wikipedia.org/wiki/Precompiled_header

What is stdafx.h used for in Visual Studio?

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.

Why do we use `#include stdafx.h` instead of `#include <stdafx.h>`?

A stdafx.h, stdafx.cpp pair is generated by VS from a template. It resides in the same directory the rest of the files end up. You will probably end up altering it specifically for your project. So we use "" instead of <> for exactly the reason that it's in the same directory as your first quote describes.

The purpose of stdafx.h (And: Why doesn't this work?)

stdafx.h is mainly used in the VS generated projects as the 'container' of headers to be precompiled.

When you added a new #include to stdafx.h it didn't get included because your project is probably configured to use precompiled headers, and when you add something to stdafx.h you need to regenerate the .pch file that contains the precompiled information.

One way to do that is to have a .cpp file in your project that does nothing but #include "stdafx.h". Maybe call it `precompile.cpp". Then go to the project settings for that one .cpp file and change the following setting (for all configurations):

"C/C++ | Precompiled Headers | Precompiled Header" setting 

and select "Create /Yc".

That will set up the build so that when precompile.cpp needs to be built (because the stdafx.h header it includes has changed), it'll rebuild the .pch file that everything else uses.

stdafx.h: When do I need it?

If you don't want to use precompiled headers, then there is no point to using a standard include file - this will slow down the build for every file that includes it and cause them to include extra stuff that they do not need. Get rid of it and just include the headers they need.

Visual Studio - why does stdafx.h gets embedded

for the use of stdafx.h please see the link Here

If you want to add a header file and then the respective cpp file the you just need to go to the project in solution explorer at right side of your IDE. Right click and go for add. There you will get the option for header file and class both.
You can add from there and start writing your code for the test cases.

Why mandatory to include stdafx.h at the start?

The issue that you're seeing is the fact that MS Visual C++ uses a feature called precompiled headers by default (other compilers on other platforms have a similar feature, I recall GCC for example having this feature). This ensures that all the code referenced up to the point of the precompiled header is pre-compiled, thus the compiler has less work to do at compilation time.

What happened when you switched that around was that it assumed that the contents of "abc.h" were already pre-compiled when they actually weren't. One easy solution would be to put #include "abc.h" inside stdafx.h. See here for a more details explanation of how the stdafx.h header works.

The precompiled headers option can be easily turned off in the compiler options. There should be a "precompiled headers/precompilation" category in the options, was in earlier Visual C++ IDE's I've encountered.

What is the purpose of including the stdafx.h file in a C++ program using opencv

It's part of the compilers Precompiled Header to speed up builds.



Related Topics



Leave a reply



Submit