如何创建C头文件?

时间:2022-03-15 01:59:56

I want to be able to create a collection of functions in a header file that I could #include in one of my C Programs.

我希望能够在头文件中创建一个函数集合,我可以在我的C程序中#include。

2 个解决方案

#1


134  

  1. Open your favorite text editor
  2. 打开您最喜欢的文本编辑器
  3. Create a new file named whatever.h
  4. 创建一个名为whatever.h的新文件
  5. Put your function prototypes in it
  6. 把函数原型放在里面

DONE.

完成了。

Example whatever.h

例子whatever.h

#ifndef WHATEVER_H_INCLUDED
#define WHATEVER_H_INCLUDED
int f(int a);
#endif

Note: include guards (preprocessor commands) added thanks to luke. They avoid including the same header file twice in the same compilation. Another possibility (also mentioned on the comments) is to add #pragma once but it is not guaranteed to be supported on every compiler.

注意:包括警卫(预处理器命令)添加感谢luke。它们避免在同一编译中两次包含相同的头文件。另一种可能性(也在注释中提到)是添加#pragma一次,但是不能保证每个编译器都支持它。

Example whatever.c

例子whatever.c

#include "whatever.h"

int f(int a) { return a + 1; }

And then you can include "whatever.h" into any other .c file, and link it with whatever.c's object file.

然后你可以加上“随便什么”。h"到任何其他的。c文件,并连接它与任何。c的对象文件。

Like this:

是这样的:

sample.c

sample.c

#include "whatever.h"

int main(int argc, char **argv)
{
    printf("%d\n", f(2)); /* prints 3 */
    return 0;
}

To compile it (if you use GCC):

要编译它(如果您使用GCC):

$ gcc -c whatever.c -o whatever.o
$ gcc -c sample.c -o sample.o

To link the files to create an executable file:

连接文件以创建可执行文件:

$ gcc sample.o whatever.o -o sample

You can test sample:

你可以测试样品:

$ ./sample
3
$

#2


3  

Header files can contain any valid C code, since they are injected into the compilation unit by the pre-processor prior to compilation.

头文件可以包含任何有效的C代码,因为它们在编译之前由预处理器注入编译单元。

If a header file contains a function, and is included by multiple .c files, each .c file will get a copy of that function and create a symbol for it. The linker will complain about the duplicate symbols.

如果头文件包含一个函数,并且包含多个.c文件,那么每个.c文件将获得该函数的副本并为其创建一个符号。链接器会抱怨重复的符号。

It is technically possible to create static functions in a header file for inclusion in multiple .c files. Though this is generally not done because it breaks from the convention that code is found in .c files and declarations are found in .h files.

可以在头文件中创建静态函数,以便将其包含在多个.c文件中。虽然这通常不会实现,因为它打破了在.c文件中找到代码和在.h文件中找到声明的惯例。

See the discussions in C/C++: Static function in header file, what does it mean? for more explanation.

参见C/ c++中的讨论:头文件中的静态函数,这意味着什么?更多的解释。

#1


134  

  1. Open your favorite text editor
  2. 打开您最喜欢的文本编辑器
  3. Create a new file named whatever.h
  4. 创建一个名为whatever.h的新文件
  5. Put your function prototypes in it
  6. 把函数原型放在里面

DONE.

完成了。

Example whatever.h

例子whatever.h

#ifndef WHATEVER_H_INCLUDED
#define WHATEVER_H_INCLUDED
int f(int a);
#endif

Note: include guards (preprocessor commands) added thanks to luke. They avoid including the same header file twice in the same compilation. Another possibility (also mentioned on the comments) is to add #pragma once but it is not guaranteed to be supported on every compiler.

注意:包括警卫(预处理器命令)添加感谢luke。它们避免在同一编译中两次包含相同的头文件。另一种可能性(也在注释中提到)是添加#pragma一次,但是不能保证每个编译器都支持它。

Example whatever.c

例子whatever.c

#include "whatever.h"

int f(int a) { return a + 1; }

And then you can include "whatever.h" into any other .c file, and link it with whatever.c's object file.

然后你可以加上“随便什么”。h"到任何其他的。c文件,并连接它与任何。c的对象文件。

Like this:

是这样的:

sample.c

sample.c

#include "whatever.h"

int main(int argc, char **argv)
{
    printf("%d\n", f(2)); /* prints 3 */
    return 0;
}

To compile it (if you use GCC):

要编译它(如果您使用GCC):

$ gcc -c whatever.c -o whatever.o
$ gcc -c sample.c -o sample.o

To link the files to create an executable file:

连接文件以创建可执行文件:

$ gcc sample.o whatever.o -o sample

You can test sample:

你可以测试样品:

$ ./sample
3
$

#2


3  

Header files can contain any valid C code, since they are injected into the compilation unit by the pre-processor prior to compilation.

头文件可以包含任何有效的C代码,因为它们在编译之前由预处理器注入编译单元。

If a header file contains a function, and is included by multiple .c files, each .c file will get a copy of that function and create a symbol for it. The linker will complain about the duplicate symbols.

如果头文件包含一个函数,并且包含多个.c文件,那么每个.c文件将获得该函数的副本并为其创建一个符号。链接器会抱怨重复的符号。

It is technically possible to create static functions in a header file for inclusion in multiple .c files. Though this is generally not done because it breaks from the convention that code is found in .c files and declarations are found in .h files.

可以在头文件中创建静态函数,以便将其包含在多个.c文件中。虽然这通常不会实现,因为它打破了在.c文件中找到代码和在.h文件中找到声明的惯例。

See the discussions in C/C++: Static function in header file, what does it mean? for more explanation.

参见C/ c++中的讨论:头文件中的静态函数,这意味着什么?更多的解释。