如何能够使头文件可以预编译?在不同的源文件中解释一致时,头文件可以预编译。具体来说,就是当它仅包含完全声明时。也就是说,任何一个文件中的声明都必须独自成为有效声明。不完全的类型声明,例如 struct S;,是有效声明。完全类型声明可以出现在某些其他文件中。请考虑这些头文件示例:
file a.h struct S { #include "x.h" /* not allowed */ }; file b.h struct T; // ok, complete declaration struct S { int i; [end of file, continued in another file] /* not allowed*/ |
并入预编译头文件的头文件一定不得违反以下约束。这里没有定义对违反上述约束的程序的编译结果。
头文件不得使用 __DATE__ 和 __TIME__。
头文件不得包含 #pragma hdrstop。
如果头文件还包含变量和函数定义,则也是可预编译的。