0

I would always typically include dependencies in my header files so that when adding that header to a source file, I don't need to dig around for the other required headers to make it compile.

However, after reviewing some other coding standards, it appears that this is often banned, with the requirement that a header file will not contain any #include statements.

I can't really find any discussion on this - so what would be the reason for banning such a practice, or is it purely down to preference?

--

E.g. typedef.h contains a typedef for U8. my_header.h declares void display_message(U8 arg);

Should the reference to typedef.h go into my_source_file.c or into my_header.h ??

4

2 回答 2

5

我认为没有充分的理由不允许标头包含其先决条件。

#include考虑从源文件中删除一个。例如,假设代码已被修改为不再使用foo.h,那么#includefor 将被删除。但是源文件有十几个#include语句。您应该删除哪些其他的,因为它们不再需要?希望foo.h记录其先决条件,以便您可以确定要删除的候选对象。但是,如果您删除他们的#include语句,您可能会删除其他头文件所需的先决条件。所以你必须检查每个头文件的先决条件。

相反,如果标题包含它们的先决条件,那么您可以简单地删除#include <foo.h>并完成它。

于 2013-08-09T11:02:46.350 回答
-1

它应该放在源文件中。标头应仅在源代码文件(通常是标准文件)中声明函数和变量。

于 2013-08-07T16:48:56.490 回答