I understand that the purpose of precompiled header is about to speed up compilation process by using object file that was already compiled once and link it to actual project. But what I do not uderstand Is why should I explicitly enforce Visual Studio to use stdafx.h
(Project Properities -> C/C++ -> Precompiled Headers -> Precompiled Header)? Why isn't #include "stdafx.h"
not enough?
Why isn't include stdafx.h not enough for VisualStudio?
1.5k Views Asked by Wakan Tanka At
2
There are 2 best solutions below
12

Why isn't
#include "stdafx.h"
[...] enough?
Because that is just a standard C++ preprocessor directive.* It's no different from #include <string>
as far as the compiler is concerned. If you need to have additional semantics attached, you have to tell the compiler about it.
Since there appears to be confusion, how Visual Studio implements precompiled headers, here is a link to documentation: Creating Precompiled Header Files.
* Mind you, there's nothing special about the name "stdafx.h"
, other than it being the default header name used to generate a PCH.
So called
stdafx.h
is supposed look as an ordinary header file. There's nothing special about its name. It is not supposed to automatically enable precompiled headers.The idea is that
stdafx.h
will be seen as an ordinary header file by any other compiler and/or when you don't what to use precompiler headers in MSVC.It is you who are supposed to tell the MSVC compiler that you actually want to enable precompiled headers. That's exactly why you have that project setting. And only when you enable precompiled headers,
stdafx.h
will acquire its special role and will receive special treatment from the compiler. And it is not necessarilystdafx.h
. You can assign this special role to any other header by specifying a different name in project settings.In fact, seeing a reference to AFX in a project that has nothing to do with Microsoft-specific libraries or frameworks would be quite an oddity.