包含在.h文件和.c文件之间的区别

时间:2021-02-24 03:14:09

what is the difference between doing an include in .h file and .c file. For example, I have the file a.h and a.c which have the class A. I use class A in class B (b.h, b.c). What is the difference between doing an include:

在.h文件和.c文件中执行include有什么区别?例如,我有文件a。h和。c有A类,我用B类的A类。h,公元前)。做“包括”的区别是什么?

#include "a.h"

in b.h vs b.c.

在b。h公元前vs。

6 个解决方案

#1


3  

Usually, the class definition is typically in your .h file, and the implementation is in your .c(pp) file.

通常,类定义通常在.h文件中,实现在.c(pp)文件中。

One advantage of doing #include "a.h" in your b.c file, rather than in your b.h file, is that whenever a.h changes, not only b.c but also any other file that includes b.h will have be recompiled.

做#的一个好处包括“a”。在你的b h”。c文件,而不是你的b。h文件,当a。h变化,不只是b。也可以是任何包含b的文件。h将被重新编译。

Also, you're kind of unnecessary exposing implementation details of B to anyone using b.h. The idea is that b.h should not contain additional info that is of no interest to someone using class B (not because it's secret, but because people don't care and you don't want to bother them with it).

而且,你不需要向任何使用b.h的人公开B的实现细节。h不应该包含对使用B类的人不感兴趣的附加信息(不是因为它是秘密,而是因为人们不在乎,你也不想用它来打扰他们)。

There's no need to force anyone including b.h, to indirectly include a dozen of other .h files as well (because they're #included in b.h), just because you happen to use that stuff in b.c. Avoid clutter.

没有必要强迫任何人包括b。h,间接地包含了12个其他的。h文件(因为它们是在b.h中包含的#),只是因为你碰巧在b.c中使用了这些东西。避免混乱。

So if possible, it's best to #include "a.h" in b.c !

所以如果可能的话,最好将#包含“a”。在b h”。c !

But this is not always possible. If you're just using A inside b.c, and there are no references to A in b.h, then it's OK. Also, if b.h only contains pointers to A (i.e. as members, function arguments or return values) but no 'type dependent' references, you could also put just this in b.h:

但这并不总是可能的。如果你用的是A里面的b。c, b中没有提到A。h,然后没关系。同样,如果b。h只包含指向A的指针(如成员、函数参数或返回值),但没有“类型依赖”引用,您也可以在b.h中放入这个。

class A;

And still keep #include "a.h" in your b.c. But if there are more references or dependencies on a.h, that anyone including b.h really couldn't do without, then #include "a.h" should go in b.h

并保持#include“a”。在你的b.c.,但如果有更多的引用或依赖a。h,包括b在内的任何人。h真的离不开,然后#include“a”。h应该是b.h

#2


2  

There is No difference in including a header file in .h or .c file.
The contents of the included file are just copy pasted in to the file in which you include it.

在.h或.c文件中包含头文件没有区别。所包含文件的内容只是复制粘贴到包含该文件的文件中。

#3


1  

If you put the include directive in your header file, other files that include that header file will also get the included header.

如果将include指令放在头文件中,其他包含该头文件的文件也将获得包含的头文件。

foo.h:

foo。:

#include "dependency.h"

bar.h:

bar.h:

#include "foo.h"

In this case, bar.h has both foo.h and dependency.h.

在这种情况下,酒吧。h foo。h和dependency.h。

#4


0  

#include "a.h" expands to the contents of a.h.

# include "。h扩展到a.h的内容。

If #include "a.h" is placed in b.h, then a.h will be copied into b.h during compilation.

如果# include "。h在b中。h,然后。h会被复制到b中。在编译过程中h。

If #include "a.h" is placed in b.c, then a.h will be copied into b.c during compilation instead.

如果# include "。h在b中。c,那么。h会被复制到b中。c在编译。

#5


0  

.h files put at top of .c or .h files before compile

.h文件在编译前放在.c或.h文件的顶部

but .c files compile separately then link to getter to make executable file

但是。c文件单独编译,然后链接到getter以生成可执行文件

#6


0  

You can include .c files - but by convention you do not.

您可以包含.c文件——但是按照惯例您不能包含。

.h files are for declarations - i.e. the .c file is the definition and the this .h file is going to do this. It is like the .h file is the contents of the cook book, and the .c file is the actual recipes.

.h文件是用于声明的,即.c文件是定义,而这个.h文件将执行此操作。它就像。h文件是烹饪书的内容,而。c文件是实际的食谱。

#1


3  

Usually, the class definition is typically in your .h file, and the implementation is in your .c(pp) file.

通常,类定义通常在.h文件中,实现在.c(pp)文件中。

One advantage of doing #include "a.h" in your b.c file, rather than in your b.h file, is that whenever a.h changes, not only b.c but also any other file that includes b.h will have be recompiled.

做#的一个好处包括“a”。在你的b h”。c文件,而不是你的b。h文件,当a。h变化,不只是b。也可以是任何包含b的文件。h将被重新编译。

Also, you're kind of unnecessary exposing implementation details of B to anyone using b.h. The idea is that b.h should not contain additional info that is of no interest to someone using class B (not because it's secret, but because people don't care and you don't want to bother them with it).

而且,你不需要向任何使用b.h的人公开B的实现细节。h不应该包含对使用B类的人不感兴趣的附加信息(不是因为它是秘密,而是因为人们不在乎,你也不想用它来打扰他们)。

There's no need to force anyone including b.h, to indirectly include a dozen of other .h files as well (because they're #included in b.h), just because you happen to use that stuff in b.c. Avoid clutter.

没有必要强迫任何人包括b。h,间接地包含了12个其他的。h文件(因为它们是在b.h中包含的#),只是因为你碰巧在b.c中使用了这些东西。避免混乱。

So if possible, it's best to #include "a.h" in b.c !

所以如果可能的话,最好将#包含“a”。在b h”。c !

But this is not always possible. If you're just using A inside b.c, and there are no references to A in b.h, then it's OK. Also, if b.h only contains pointers to A (i.e. as members, function arguments or return values) but no 'type dependent' references, you could also put just this in b.h:

但这并不总是可能的。如果你用的是A里面的b。c, b中没有提到A。h,然后没关系。同样,如果b。h只包含指向A的指针(如成员、函数参数或返回值),但没有“类型依赖”引用,您也可以在b.h中放入这个。

class A;

And still keep #include "a.h" in your b.c. But if there are more references or dependencies on a.h, that anyone including b.h really couldn't do without, then #include "a.h" should go in b.h

并保持#include“a”。在你的b.c.,但如果有更多的引用或依赖a。h,包括b在内的任何人。h真的离不开,然后#include“a”。h应该是b.h

#2


2  

There is No difference in including a header file in .h or .c file.
The contents of the included file are just copy pasted in to the file in which you include it.

在.h或.c文件中包含头文件没有区别。所包含文件的内容只是复制粘贴到包含该文件的文件中。

#3


1  

If you put the include directive in your header file, other files that include that header file will also get the included header.

如果将include指令放在头文件中,其他包含该头文件的文件也将获得包含的头文件。

foo.h:

foo。:

#include "dependency.h"

bar.h:

bar.h:

#include "foo.h"

In this case, bar.h has both foo.h and dependency.h.

在这种情况下,酒吧。h foo。h和dependency.h。

#4


0  

#include "a.h" expands to the contents of a.h.

# include "。h扩展到a.h的内容。

If #include "a.h" is placed in b.h, then a.h will be copied into b.h during compilation.

如果# include "。h在b中。h,然后。h会被复制到b中。在编译过程中h。

If #include "a.h" is placed in b.c, then a.h will be copied into b.c during compilation instead.

如果# include "。h在b中。c,那么。h会被复制到b中。c在编译。

#5


0  

.h files put at top of .c or .h files before compile

.h文件在编译前放在.c或.h文件的顶部

but .c files compile separately then link to getter to make executable file

但是。c文件单独编译,然后链接到getter以生成可执行文件

#6


0  

You can include .c files - but by convention you do not.

您可以包含.c文件——但是按照惯例您不能包含。

.h files are for declarations - i.e. the .c file is the definition and the this .h file is going to do this. It is like the .h file is the contents of the cook book, and the .c file is the actual recipes.

.h文件是用于声明的,即.c文件是定义,而这个.h文件将执行此操作。它就像。h文件是烹饪书的内容,而。c文件是实际的食谱。