For faster navigation, this Iframe is preloading the Wikiwand page for 头文件.

头文件

头文件(英語:Header file),或包含文件,是个在程序设计時,特别是在C语言C++中使用的文件,通常是源代码的形式,由编译器在处理另一个源文件的时候自动包含进来。一般来说,程序员通过编译器指令将头文件包含进其他源文件的开始(或头部)。

头文件一般包含子程序变量和其他标识符的前置声明。需要在一个以上源文件中被声明的标识符可以被放在一个头文件中,并在需要的地方包含这个头文件。

C语言C标准函式库C++C++标准函式库中,标准库函数习惯上在头文件中声明。

作用

[编辑]

在大多数现代计算机编程语言中,程序可以被分成如子程序的更小的组件,这些组件可以通过许多物理源文件分发,这些源文件被单独编译。当一个子程序在定义的位置以外的地方被使用时,就需要引入前置声明和函数原型的概念。例如,一个函数在一个源文件中有如下定义:

 int add(int a, int b)
 {
     return a + b;
 }

在另一个源文件中引用的时候就可以声明成这样(包含函数原型):

 int add(int, int);
 
 int triple(int x)
 {
     return add(x, add(x, x));
 }

但是,这个简单的方法需要程序员为add在两个地方维护函数声明,一个是包含函数实现的文件,以及使用该函数的文件。如果函数的定义改变了,程序员必须要更改散布在程序中的所有的原型。

头文件提供了解决办法。模块的头文件声明作为模块公共接口一部分的每一个函数、对象以及数据类型。例如,在下面的情况下,头文件仅包含add的声明。每一个引用了add的源文件使用#include来包含头文件:

 /* File add.h */
 #ifndef ADD_H
 #define ADD_H
 
 int add(int, int);
 
 #endif /* ADD_H */
 /* File triple.c */
 #include "add.h"
 
 int triple(int x)
 {
     return add(x, add(x, x));
 }

这样就减少了维护的负担:当定义改变的时候,只须更新声明的一个独立副本(在头文件中的那个)。在包含对应的定义的源文件中也可以包含头文件,这给了编译器一个检查声明和定义一致性的机会。

 /* File add.c */
 #include "add.h"
 
 int add(int a, int b)
 {
     return a + b;
 }

通常,头文件被用来唯一指定接口,且多少提供一些文档来说明如何使用在该文件中声明的组件。在这个例子中,子程序的实现放在一个单独的源文件中,这个源文件被单独编译。(在C和C++中有个例外,即内联函数。内联函数通常放在头文件中,因为大多数实现如果不知道其定义,在编译时便无法适当的展开内联函数。)

替代

[编辑]

在访问声明在不同文件中的标识符问题上,头文件不是唯一的解决方法。他们也有缺点,当定义改变的时候可能仍然需要在两个地方来修改(头文件和源文件)。一些更新的语言(如Java)省略掉了头文件,而使用命名方案英语naming scheme(naming scheme),这就允许编译器来定位与接口和类实现相关的源文件。

#include语句的两种语法

[编辑]

#include语句有两种方式包含头文件,分别是使用双引号" "与左右尖括号< >。其区别是(对于不是使用完全文件路径名的)头文件的搜索顺序不同:

使用双引号" "的头文件的搜索顺序:

  1. 包含该#include语句的源文件所在目录;
  2. 包含该#include语句的已经打开的头文件的逆序(因为头文件可以#include另一个头文件构成一个序列);
  3. 编译选项-I所指定的目录
  4. 环境变量INCLUDE所定义的目录

使用左右尖括号< >的头文件的搜索顺序:

  1. 编译选项-I所指定的目录
  2. 环境变量INCLUDE所定义的目录

参见

[编辑]

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
头文件
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?