为什么我们使用 `#include "stdafx.h"` 而不是 `#include <stdafx.h>`?

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

来自here,据说:

For #include "filename" the preprocessor searches in the same directory as the file containing the directive. This method is normally used to include programmer-defined header files.

For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

虽然this wiki link暗示stdafx.hvisual studio IDE预先设计的头文件

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

The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft Foundation Classes (MFC). While the name stdafx.h is used by default, projects may specify an alternative name.

然后

为什么我们使用#include "stdafx.h"而不是#include <stdafx.h>

stdafx.hstdafx.cpp 对由 VS 从模板生成。它位于其余文件所在的同一目录中。您可能最终会专门为您的项目更改它。所以我们使用 "" 而不是 <> 正是因为它与你的第一句话描述的目录相同。

因为stdafx.h每个项目都不一样。正如你所引用的,#include "" 搜索当前项目的路径,这就是 stdafx.h 所在的位置。

使用 #include <stdafx.h> 将是一个 巨大的 错误,因为它必须在库路径中(所有标准库头位于)。这意味着你不应该修改它,或者它总是保持不变,但对于不同的项目来说它永远不会相同。

所以即使它是由 Visual Studio 生成的,它也是特定于项目的,而不是所有项目。

通常,对于给定的包含文件 name.h,语法 #include <name> 保留用于 standard libraries,而 #include "name.h" 用于 user-defined文件。在那里,'user' 可能意味着任何开发人员实现 non-standard 功能,比如针对特定的编译器包。

因此,预处理器会按照适当的系统设置指定的合适路径搜索包含文件。不仅要注意括号和引号的区别,还要注意 *.h 扩展名的不同用法。