头文件中包含头文件 [英] Header files included in header files
问题描述
大家好
小组怎么看待将
中的一个头文件包含在另一个内的做法?
我已经完成了一些遗留代码,并且它创建了一个
依赖于一个模块(文件集合),这是不需要的,除了
对于一个头文件的内容。
我会说''不,头文件应该包含在C源代码中,而不是在另一个内容中
header'',但我总是遇到后者的强烈争论。
你怎么想,什么是公认的做法?
谢谢
JS
文章< cd ********** @ newstree.wise.edt.ericsson.se>,
" John Smith" <所以***** @ microsoft.com>写道:
大家好
小组怎么想在
中包含一个头文件的做法?
<我已经完成了一些遗留代码,并且它创建了对模块(文件集合)的依赖,这对于一个头文件的内容除了
之外是不需要的。
我会说''不,头文件应该包含在C源代码中,而不是在另一个
头文件中',但我总是遇到强有力的争论后者。
你怎么想,什么是公认的做法?
如果你写一个头文件header.h,然后它必须以单行源文件的方式写成
方式
#include" header.h"
>
编译没有错误。这意味着header.h需要包含完成这项工作所需的所有内容
,但不能更多。
注意一个函数可以有参数或返回值类型struct
xxx *没有struct xxx的声明;这个功能
的C经常使得不必在头文件中包含头文件
文件。
" Christian Bau" < CH *********** @ cbau.freeserve.co.uk>在消息中写道
news:ch ********************************* @ slb- newsm1.svr.pol.co.uk ...在文章< cd ********** @ newstree.wise.edt.ericsson.se>中,
约翰史密斯 <所以***** @ microsoft.com>写道:
大家好
小组怎么想到从另一个里面包含一个头文件
的做法?
<我已经完成了一些遗留代码,并且它创建了对不需要的模块(文件集合)的依赖,
除了一个头文件的内容。
我会说''不,头文件应该包含在C源代码中,而不是
另一个头文件'',但我总是遇到强有力的争论后者。
你怎么想,什么是公认的做法?
如果你写一个头文件header.h,那么它必须是用这样的方式写的单行源文件
编译没有错误。这意味着header.h需要包含使这项工作成为必要的所有内容,而不是更多内容。
请注意,函数可以包含参数或返回类型为struct的值/> xxx *"没有struct xxx的声明; C的这个特性经常使得不必在头文件中包含头文件。
谢谢Christian
你有一个关于它必须编译的好点。这个特殊的标题
问题在第一个标题中有类似
typedef struct
{int array [THIS_IS_DEFINED_IN_HEADER2]; } astruct;
当然在另一个标题中
#define THIS_IS_DEFINED_IN_HEADER2 5
结果是是唯一依赖的东西!
正常练习,还是编码不好?
谢谢
JS
嗨John,
你对它必须编译有一个好处。这个特殊的标题问题在第一个标题中有类似
typedef struct
{int array [THIS_IS_DEFINED_IN_HEADER2]; } astruct;
当然在另一个标题中
#define THIS_IS_DEFINED_IN_HEADER2 5
这原来是唯一依赖的东西!
正常练习或者编码不好?
这取决于。我正在使用一个巨大的代码,你有数百个b $ b的头文件。许多THIS_IS_DEFINED_IN_HEADER2类型
符号常量是有意义的,因为您可以假设
所有数组(在您的示例中)服务于某个目的
具有相同的尺寸 - 我认为优点很明显。
另一方面,如果这个符号常数没有
a"更深"意思是你的数组的目的,但每次只有
你有数值5的值,
那么它编码很差。
如果您确定其含义,您还可以使用
构造,例如
#ifndef DONTWANNAINCLUDEIT
#include" header2.h"
#endif
#ifdef THIS_IS_DEFINED_IN_HEADER2
#define MY_ARRAY_SIZE THIS_IS_DEFINED_IN_HEADER2
#else
#define MY_ARRAY_SIZE 5
#endif
typedef struct {
int array [MY_ARRAY_SIZE];
} astruct;
干杯,
Michael
Hi all
What does the group think of the practise of including one header file from
inside another?
I have some legacy code where this has been done, and it creates a
dependency on a module (collection of files) which are not required, except
for one header file''s contents.
I''d say ''No, header files should be included in the C source, not in another
header'', but I''ve always come across strong arguments for the latter.
What do you think, and what is accepted practise?
Thanks
JS
In article <cd**********@newstree.wise.edt.ericsson.se>,
"John Smith" <so*****@microsoft.com> wrote:
Hi all
What does the group think of the practise of including one header file from
inside another?
I have some legacy code where this has been done, and it creates a
dependency on a module (collection of files) which are not required, except
for one header file''s contents.
I''d say ''No, header files should be included in the C source, not in another
header'', but I''ve always come across strong arguments for the latter.
What do you think, and what is accepted practise?
If you write a header file "header.h", then it must be written in such a
way that the single line source file
#include "header.h"
compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.
Note that a function can have arguments or return values of type "struct
xxx *" without having the declaration of struct xxx around; this feature
of C often makes it unnecessary to include header files within header
files.
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...In article <cd**********@newstree.wise.edt.ericsson.se>,
"John Smith" <so*****@microsoft.com> wrote:Hi all
What does the group think of the practise of including one header file from inside another?
I have some legacy code where this has been done, and it creates a
dependency on a module (collection of files) which are not required, except for one header file''s contents.
I''d say ''No, header files should be included in the C source, not in another header'', but I''ve always come across strong arguments for the latter.
What do you think, and what is accepted practise?
If you write a header file "header.h", then it must be written in such a
way that the single line source file
#include "header.h"
compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.
Note that a function can have arguments or return values of type "struct
xxx *" without having the declaration of struct xxx around; this feature
of C often makes it unnecessary to include header files within header
files.
Thanks Christian
You have a good point about it having to compile. This particular header
problem has, in the first header, something like
typedef struct
{ int array[THIS_IS_DEFINED_IN_HEADER2]; } astruct;
and of course in the other header
#define THIS_IS_DEFINED_IN_HEADER2 5
This turns out to be the only dependant thing!
Normal practise, or poor coding?
Thanks
JS
Hi John,
You have a good point about it having to compile. This particular header
problem has, in the first header, something like
typedef struct
{ int array[THIS_IS_DEFINED_IN_HEADER2]; } astruct;
and of course in the other header
#define THIS_IS_DEFINED_IN_HEADER2 5
This turns out to be the only dependant thing!
Normal practise, or poor coding?
It depends. I am working with a huge code where you have hundreds
of header files. Many of the THIS_IS_DEFINED_IN_HEADER2 type
symbolic constants make sense insofar as you then can assume
that all arrays (in your example) serving a certain purpose
are of the same size -- the advantages are clear, I think.
On the other hand, if this symbolic constant does not have
a "deeper" meaning for the purpose of your array but only has
per chance the value 5 which you also have as the array size,
then it is poor coding.
If you are sure about the implications, you can also use a
construct like
#ifndef DONTWANNAINCLUDEIT
#include "header2.h"
#endif
#ifdef THIS_IS_DEFINED_IN_HEADER2
#define MY_ARRAY_SIZE THIS_IS_DEFINED_IN_HEADER2
#else
#define MY_ARRAY_SIZE 5
#endif
typedef struct {
int array[MY_ARRAY_SIZE];
} astruct;
Cheers,
Michael
这篇关于头文件中包含头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!