如何用C语言创建自己的头文件 [英] How do I create own header files in C language

查看:1161
本文介绍了如何用C语言创建自己的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以详细解释如何用C语言创建自己的头文件。



我尝试了什么:



我尝试过我的胜利,但结果并没有太大效果。

Can I have some detailed explanation of creating a own header file in C language.

What I have tried:

I tried it my won but the result was not much effective.

推荐答案

这取决于究竟是什么编辑器和所以你使用它,但它很简单:只需创建一个带有.h扩展名的文件,并用你的函数原型,常量定义等填充它。



请参阅: C头文件 [ ^ ]
It depends on exactly what editor and so forth you use, but it's very simple: just create a file with the ".h" extension and fill it with your function prototypes, constant definitions, and so forth.

See here: C Header Files[^]


很难在不知道头文件应该是什么的情况下给出详细解释包含所有。



另请注意,头文件的创建只不过是创建文本文件。



一般来说,标题f iles应该包含声明(比如指定名称和类型),源文件应该包含定义(实现)。



头文件通常连接到源文件。这意味着它包含相应源文件的声明。然后它包含在源文件和任何其他需要至少一个声明的文件(源或头文件)中(例如,当调用头文件中声明的函数时)。



特殊情况是一般头文件,涵盖项目特定设置,包含在项目的所有或大多数文件中。



举一些例子你应该在至少指定您是否使用C或C ++(类)和/或从源文件中提供一些示例。或者告诉我们你尝试了什么以及失败的地方。
It is hard to give a detailed explanation without knowing what the header file should contain at all.

Note also the the "creation" of a header file is nothing else than the creation of a text file.

In general, header files should contain declarations (like specifying names and types) and source files should contain definitions (implementations).

A header file is usually "connected" to a source file. That means it contains the declarations for the corresponding source file. It is then included by that source file and any other file (source or header) that needs at least one of the declarations (e.g. when calling a function declared in the header file).

A special case are general header files covering project specific settings that are included by all or most files of a project.

To give some examples you should at least specify if you are using C or C++ (classes) and/or give some examples form your source file. Or show us what you have tried and where it failed.


Quote:

我如何创建自己的C语言中的头文件

How do I create own header files in C language



与.c文件完全相同。

这里的头文件是为了避免再次输入结构声明和函数原型在每个使用它们的.c文件中。



想象一下:你是一个大项目,你已经构建了一堆可以在任何地方重用的函数,你可以键入在每个.c文件中构造声明和函数原型。



然后你需要更改结构,在这种情况下你需要编辑每个使用它的.c文件。这很乏味,任何错误都会导致奇怪的行为并且很难发现错误。

如果你使用.h头文件,你可以为整个项目改变一个地方。


Exactly the same as a .c file.
A header file is here to avoid typing again and again structures declarations and functions prototypes in every .c files that use them.

Imagine: you are on a big project, you have built a bunch of functions that are reused everywhere, you can type the structures declarations and functions prototypes in every .c file.

Then you need to change the structures, in this case you need to edit every single .c file that use it. It is tedious and any error will lead to weird behavior and very difficult to spot bugs.
If you use a .h header file, you have 1 place to change for the whole project.


这篇关于如何用C语言创建自己的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆