C语言 结构定义应该放在 .h 还是 .c 文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6316987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:53:43  来源:igfitidea点击:

Should struct definitions go in .h or .c file?

cheaderstruct

提问by Aaron Yodaiken

I've seen both full definitions of structs in headers and just declarations—is there any advantage to one method over the other?

struct在头文件中看到了s 的完整定义,也看到了声明——一种方法比另一种方法有什么优势吗?

If it makes a difference, I usually typedef a struct like so in the .h

如果它有所不同,我通常会在 .h

typedef struct s s_t;


Edit

编辑

To be clear, the options are declaration in the header file and definition in the class, or both declaration and definition in the header file. Both should result in the same usability, even if one is by linkage, shouldn't they?

要清楚,选项是头文件中的声明和类中的定义,或者头文件中的声明和定义。两者都应该产生相同的可用性,即使一个是通过链接,不是吗?



I see many almost duplicates, e.g. herebut no exact matches. Please correct me if I'm wrong in this regard.

我看到许多几乎重复的内容,例如在这里但没有完全匹配。如果我在这方面错了,请纠正我。

回答by τεκ

Private structures for that file should go in the .c file, with a declaration in the .h file if they are used by any functions in the .h .

该文件的私有结构应该在 .c 文件中,如果它们被 .h 中的任何函数使用,则在 .h 文件中声明。

Public structures should go in the .h file.

公共结构应该放在 .h 文件中。

回答by Matthew Slattery

Both should result in the same usability, even if one is by linkage, shouldn't they?

两者都应该产生相同的可用性,即使一个是通过链接,不是吗?

No, not when you consider other .c files including the same header. If the definition of the structure is not visible to the compiler, the details of that definition cannot be used. A declaration without a definition (e.g. just struct s;) causes the compiler to fail if anything tries to look inside struct s, while still allowing it to e.g. compile struct s *foo;(as long as foois not later dereferenced).

不,当您考虑包含相同标头的其他 .c 文件时则不会。如果结构的定义对编译器不可见,则无法使用该定义的详细信息。没有定义的声明(例如 just struct s;)会导致编译器在任何尝试查看内部struct s时失败,同时仍然允许它进行编译struct s *foo;(只要foo以后没有取消引用)。

Compare these versions of api.hand api.c:

比较这些版本的api.hapi.c

Definition in header:                 Definition in implementation:
+---------------------------------+   +---------------------------------+
| struct s {                      |   | struct s;                       |
|     int internal;               |   |                                 |
|     int other_stuff;            |   | extern void                     |
| };                              |   | api_func(struct s *foo, int x); |
|                                 |   +---------------------------------+
| extern void                     |   +---------------------------------+
| api_func(struct s *foo, int x); |   | #include "api.h"                |
+---------------------------------+   |                                 |
+---------------------------------+   | struct s {                      |
| #include "api.h"                |   |     int internal;               |
|                                 |   |     int other_stuff;            |
| void                            |   | };                              |
| api_func(struct s *foo, int x)  |   |                                 |
| {                               |   | void                            |
|     foo->internal = x;          |   | api_func(struct s *foo, int x)  |
| }                               |   | {                               |
+---------------------------------+   |     foo->internal = x;          |
                                      | }                               |
                                      +---------------------------------+

This client of the API works with either version:

该 API 客户端适用于任一版本:

#include "api.h"

void good(struct s *foo)
{
    api_func(foo, 123);
}

This one pokes around in the implementation details:

这个在实现细节中闲逛:

#include "api.h"

void bad(struct s *foo)
{
    foo->internal = 123;
}

which will work with the "definition in header" version, but not with the "definition in implementation" version, as in the latter case the compiler has no visibility of the layout of the structure:

这将适用于“头文件中的定义”版本,但不适用于“实现中的定义”版本,因为在后一种情况下,编译器无法看到结构的布局:

$ gcc -Wall -c bad.c
bad.c: In function 'bad':
bad.c:5: error: dereferencing pointer to incomplete type
$

So, the "definition in implementation" version protects against accidental or deliberate misuse of private implementation details.

因此,“实现中的定义”版本可以防止意外或故意滥用私有实现细节。

回答by nos

If the struct is to be used by other compilation units (.c files) , place it in the header file so you can include that header file wherever it is needed.

如果该结构要被其他编译单元(.c 文件)使用,请将其放在头文件中,以便您可以在需要的任何地方包含该头文件。

If the struct is only used in one compilation unit (.c file), you place it in that .c file.

如果该结构仅在一个编译单元(.c 文件)中使用,则将其放置在该 .c 文件中。

回答by Jonathan Wood

The point is, placing it in a header file allows you to use the structure (or any other definition) from multiple source files, just by including that header file.

关键是,将它放在头文件中允许您使用来自多个源文件的结构(或任何其他定义),只需包含该头文件即可。

But if you are sure it will only be used from one source file, then it really doesn't make any difference.

但是如果你确定它只会从一个源文件中使用,那么它真的没有任何区别。

回答by TofuBeer

I put them into the C file to make it more Object Oriented, see this article.

我将它们放入 C 文件中,使其更加面向对象,请参阅这篇文章

回答by Frxstrem

Generally, I don't think it makes a huge difference whether you put them in the header or source files. However, if you need to access the members of a structure from multiple source files, it is easier to put the structure in a header file and include it from any other files where the structure is needed.

一般来说,我认为将它们放在头文件或源文件中并没有太大的区别。但是,如果您需要从多个源文件访问结构的成员,将结构放在头文件中并从需要该结构的任何其他文件中包含它会更容易。