C语言 C 中的源文件和头文件之间有什么根本区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3482948/
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 06:11:03  来源:igfitidea点击:

Any fundamental difference between source and header files in C?

c

提问by Delan Azabani

I don't quite understand how things should be separated in C's source and header files. I often see many projects with two sets of files with the same name (sans the extension denoting source and header files).

我不太明白在 C 的源文件和头文件中应该如何分离事物。我经常看到许多项目有两组同名文件(没有表示源文件和头文件的扩展名)。

So far, from this lack of understanding, when I've written libraries, I've chucked all the class and class method code into one file, with indecision as to choosing the file extension.

到目前为止,由于缺乏理解,当我编写库时,我将所有类和类方法代码都放在一个文件中,对选择文件扩展名犹豫不决。

What should be in headers and what should be in the source files? How do I implement this separation?

头文件中应该包含哪些内容,源文件中应该包含哪些内容?我如何实现这种分离?

回答by Thomas

There is no technicaldifference. The compiler will happily let you include a .cfile, or compile a .hfile directly, if you want to.

没有技术差异。如果您愿意,编译器会很乐意让您包含一个.c文件,或者.h直接编译一个文件。

There is, however, a huge culturaldifference:

然而,存在巨大的文化差异:

  • Declarations(prototypes) go in .hfiles. The .hfile is the interfaceto whatever is implemented in the corresponding .cfile.

  • Definitionsgo in .cfiles. They implementthe interface specified in the .hfile.

  • 声明(原型)在.h文件中。该.h文件是相应文件中实现的任何内容的接口.c

  • 定义.c文件中。它们实现.h文件中指定的接口。

The difference is that a .hfile can (and usually will) be #included into multiple compilation units (.cfiles). If you definea function in a .hfile, it will end up in multiple .ofiles, and the linker will complain about a multiply defined symbol. That's why definitions should not go in .hfiles. (Inline functions are the exception.)

不同之处在于一个.h文件可以(并且通常会)被#include分成多个编译单元(.c文件)。如果你在一个文件中定义一个函数.h,它最终会出现在多个.o文件中,并且链接器会抱怨一个多重定义的符号。这就是为什么定义不应该放在.h文件中的原因。(内联函数是个例外。)

If a function is defined in a .cfile, and you want to use it from other .cfiles, a declaration of that function needs to be available in each of those other .cfiles. That's why you put the declaration in a .h, and #includethat in each of them. You could also repeat the declaration in each .cfile, but that leads to lots of code duplication and an unmantainable mess.

如果在.c文件中定义了一个函数,并且您想从其他.c文件中使用它,则需要在每个其他.c文件中都可以使用该函数的声明。这就是为什么您将声明放在 a 中.h,并将#include其放在每个中。您也可以在每个.c文件中重复声明,但这会导致大量代码重复和难以维护的混乱。

If a function is defined in a .cfile, but you don'twant to use it from other .cfiles, there's no need to declare it in the header. It's essentially an implementation detail of that .cfile. In that case, make the function staticas well, so it doesn't conflict with identically-named functions in other files.

如果一个函数在定义.c文件,但你希望从其他使用它.c的文件,没有必要宣布它的头。它本质上是该.c文件的实现细节。在这种情况下,也制作该函数static,这样它就不会与其他文件中的同名函数发生冲突。

回答by dirkgently

What should be in headers and what should be in the source files?

头文件中应该包含哪些内容,源文件中应该包含哪些内容?

Typically headers contain one or more of the following:

通常标头包含以下一项或多项:

  • Function declaration (except statics)
  • Variable declaration (typically global)
  • User defined type declaration (read struct, unionetc.)
  • Macro definition
  • 函数声明(静态除外)
  • 变量声明(通常是全局的)
  • 用户定义的类型声明(读structunion等)
  • 宏定义

Source files on the other hand have:

另一方面,源文件有:

  • Function/variable definition
  • Static function declaration and definition (you don't want to expose these to your clients)
  • Variable definition
  • Some prefer to define inline functions (C99) in a header
  • 函数/变量定义
  • 静态函数声明和定义(您不想将这些暴露给您的客户)
  • 变量定义
  • 有些人更喜欢在头文件中定义内联函数 (C99)

How do I implement this separation?

我如何实现这种分离?

The One Definition Rule is your friend.

单一定义规则是您的朋友。

Remember, if you are writing a library, this is what your client gets to see. So, be helpful and provide all the information you can for them to use your library. The source files are typically compiled and supplied in binary form.

请记住,如果您正在编写一个库,这就是您的客户所看到的。因此,请提供帮助并提供您可以使用的所有信息,以便他们使用您的图书馆。源文件通常以二进制形式编译和提供。

And by the way, C does not have the concept of classes.

顺便说一下,C 没有类的概念。

回答by Dipstick

There is little fundamental difference between .c and .h files (though some compilers may refuse to compile a raw .h file). The difference is more by convention.

.c 和 .h 文件之间几乎没有根本区别(尽管一些编译器可能拒绝编译原始 .h 文件)。区别更多的是惯例。

Typically the .h file provides the API and the .c provides the implementation.

通常,.h 文件提供 API,.c 提供实现。

Therefore the .h file would contain only things needed by other source files to access the facilities provided by your .c file. So the .h files would provide the function prototypes of global functions, declarations of global variables (if you really must have them), and the structures and other types used by them. (Don't expose a structure if the only a pointer to the structure is required by the API.)

因此,.h 文件将仅包含其他源文件访问 .c 文件提供的工具所需的内容。因此 .h 文件将提供全局函数的函数原型、全局变量的声明(如果您确实必须拥有它们)以及它们使用的结构和其他类型。(如果 API 只需要指向结构的指针,则不要公开结构。)

In-line functions are also often included in .h files but some coding guidelines prefer the use of a separate extension (e.g. .inl)

内嵌函数也经常包含在 .h 文件中,但一些编码指南更喜欢使用单独的扩展名(例如 .inl)

All other function implementations, the definition and initialisation of variables and declarations of local (static) variables and functions would be in the .c file.

所有其他函数实现、变量的定义和初始化以及局部(静态)变量和函数的声明都将在 .c 文件中。

回答by Federico klez Culloca

Usually, header files contain declarations, source files contain code.

通常,头文件包含声明,源文件包含代码。

So, if in source file A.cyou need a function implemented in source file B.c, you just include B.hto have its declaration.

所以,如果在源文件中A.c你需要一个在源文件中实现的函数B.c,你只需包含B.h它的声明。