预编译头文件 StdAfx.h

预编译头文件:

最常见的使用场景就是 StdAfx.h 文件,在这个文件中包含常用的头文件,比如windows.h,cstdio,string,别的 .cpp 文件去包含 StdAfx.h 头文件。编译的时候 StdAfx.h 尽管被多个 .cpp 包含,但只会编译一次。加快了编译速度。
StdAfx.cpp 专门用来生成预编译文件,StdAfx.cpp 里只有一行代码 #include "StdAfx.h", 编译 StdAfx.cpp 会产生一个 .pch 文件。别的 .cpp 文件编译的时候就会使用这个 .pch 文件。
貌似还有高级使用场景,现在还没有了解。

预编译头的使用方法:
1. 创建预编译头文件 StdAfx.h, 在这个头文件中包含希望包含的头文件,注意不要包含自己的头文件,只包含系统头文件,不会发生变动。
2. 创建 StdAfx.cpp ,在这个文件中包含 StdAfx.h
3. 我们要让编译器知道, StdAfx.cpp 文件是用来创建 .pch 文件的。在 StdAfx.cpp 文件上面右键 --> 属性 --> 所有配置 --> C/C++ --> 预编译头 --> 选择 : 创建预编译头(/Yc)
4. 我们要让编译器知道,哪些 .cpp 文件需要使用 .pch 文件。在需要使用 .pch 文件的 .cpp 中添加 #include "StdAfx.h", 然后在 .cpp 文件上右键 --> 属性 --> 所有配置 --> C/C++ --> 预编译头 --> 选择 : 使用预编译头(/Yu)
5. 单独编译 StdAfx.cpp 就会产生 .pch 文件了,如果以后不小心删除了 .pch 文件,可以单独编译一下 StdAfx.cpp 文件。

注意:
1. 并不是所有的 .cpp 文件都要包含 StdAfx.h ,是否使用预编译头是可以在配置项中修改的。
2. 如果一个 .cpp 文件使用了预编译头文件,那么要在 .cpp 文件的最开头去包含 StdAfx.h 文件,否则会跳过 #include "StdAfx.h" 之前的 #include 代码。出现 warning C4627: “#include "lua_notifyicon.h"”: 在查找预编译头使用时跳过

参考:

http://38288890.blog.163.com/blog/static/19612845320081164495863/

http://www.cnblogs.com/magic-cube/archive/2011/12/06/2278568.html


原文地址:https://www.cnblogs.com/zuibunan/p/3896736.html