预编译头是递归的吗?

use*_*395 2 c c++ visual-studio visual-c++

让我们假设我的用于创建预编译头的 .h 文件如下所示:

stdafx.h

#include "A.h" //an external library
Run Code Online (Sandbox Code Playgroud)

.. 和“Ah”包含一堆其他标头:

#include "B.h" //other headers from an external library included by "A.h"
#include "C.h"
//...
Run Code Online (Sandbox Code Playgroud)

是否还会为“Bh”和“Ch”生成预编译头(以及这两个文件包含的头文件,等等...),或者我是否必须在“stdafx”中包含“Bh”和“Ch” .h”也?

Ric*_*dle 5

是的,预编译状态将包括 B 和 C。

预编译的工作原理是运行编译器直到 stdafx.h 的末尾并将其状态转储到磁盘,然后在编译每个源文件的正文之前恢复该状态。编译器在编译 stdafx.h 时看到的所有内容都会进入该状态。

(换句话说:无论您是生成预编译状态还是只是“正常”编译源文件,编译 stdafx.h 的过程都是完全相同的 - 只是在预编译情况下编译器在最后停止文件的。即使它愿意,它也不能以任何不同的方式对待 B 和 C。)