VS2012预编译头:我的.h文件如何知道包含在stdafx.h
VS2012 precompiled headers: how does my .h file know about includes in stdafx.h?
我有一个非常基本的项目在VS2012使用预编译头。我知道我应该将所有"公共"包含添加到stdafx.h中,并且我需要将其包含在每个.cpp文件中。因此,基本设置如下所示:
foo:
#ifndef FOO_H
#define FOO_H
class Foo {
public:
Foo();
~Foo();
void do(string str);
};
#endif
Foo.c:
#include "stdafx.h"
#include "Foo.h"
void Foo::do(string str) {}
在stdafx.h: #include <string>
using namespace std;
如果没有预编译的头文件,我将#include <string>
放入foo.h中,因为它必须知道string
的声明。但我是怎么知道string
的呢?(注意stdafx.h只包含在.cpp文件中)。
注意:我有一个使用预编译头文件的工作示例;问题是这是如何工作的
这是因为编译器按照头文件在主编译单元中出现的顺序处理头文件。
因为.cpp
文件包含<string>
(间接通过"stdafx.h"
), <string>
的内容可以被编译器知道,并且可以被后面的代码使用,甚至可以从头文件中提取代码。
这是脆弱的,因为包括你的头文件没有首先包含<string>
将导致错误。
您可以将预编译的头文件看作是头文件的一种缓存。编译器在第一次遇到一组头文件时(通常是在编译stdafx.cpp时)对其进行分析,编译它们,然后为任何需要它们的模块准备好结果。
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 我怎么知道数组包含多少个元素C++?
- main.cpp 如何知道包含标头的源文件?
- 为什么我可以在不链接任何额外库的情况下包含 sys/*.h
- 我如何知道 boost::any 是否包含文字字符串
- C++:我需要在这里包含这个标题,我不知道为什么
- 如何在C 中编写一个程序,以说明不知道它正在阅读的数据文本文件中包含多少行
- 不知道为什么我收到有关 std::size_t 不包含参数包的编译错误
- 如何知道unordered_map<T*>包含值?
- STL映射如何知道,该映射包含一个给定的元素
- 我需要制作一个包含基类和派生类的对象的向量,并知道哪个元素是哪个元素
- 如果向量包含重复项,则此函数应返回 true。知道为什么它不起作用吗?
- 是否可以知道是否包含标头
- 编译器不知道我的包含文件
- 如何在没有文件、objdump或gdb的情况下知道二进制文件是否包含调试符号
- 我想知道指针在调试时包含特定的地址
- VS2012预编译头:我的.h文件如何知道包含在stdafx.h
- Char *(包含空值)到std::string知道长度
- 如何创建一个包含类型尚不知道的值的类?
- 如果我只知道C++中的基类,如何包含派生类