如何使用H文件 [英] How to use H-files

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

问题描述

Hallo allemaal,

这是我给这个小组的第一条消息。到目前为止,我主要在Turbo Pascal中编写了

程序。但是现在我参与了一个项目,需要了解C的知识。我不是很熟悉C,但最后一次

我用它可能是8年前的。


我在TP中编写了很多程序一个目标是将它们转换成C.将
转换为C.及时我创建了一些常见的,所谓的单位。我已经发现我可以使用''#include" unit.c"''来包含C源代码。但是

我知道的所有程序都使用''#include< xxxx.h>''。我发现了一些程序

使用在其他程序中声明的例程,两者似乎都被一个H文件链接了

。但无论我尝试什么,我收到了错误消息。 (我是

暂时使用Turbo C 3.1)


我当然试图在互联网上搜索答案,但我要么

一无所获或被淹没了没有提供答案的链接

我需要(主要是因为不是母语英语)。可以

有人给我一些链接,告诉我如何使用#include或告诉我

这里?


非常感谢提前!

-

___

/ __ | __

/ / | _ / Groetjes,Ruud Baltissen

\ \__ | _ \

\ ___ | http://Ruud.C64.org

解决方案
年10月16,1:29 *时,路德< Ruud.Baltis ... @ apg.nlwrote:

Hallo allemaal,


这是我给这个小组的第一条消息。到目前为止,我主要在Turbo Pascal中编写了

程序。但是现在我参与了一个项目,需要了解C的知识。我不是很熟悉C,但最后一次

我用它可能是8年前的。


我在TP中编写了很多程序一个目标是将它们转换成C.将
转换为C.及时我创建了一些常见的,所谓的单位。我已经发现我可以使用''#include" unit.c"''来包含C源代码。但是

我知道的所有程序都使用''#include< xxxx.h>''。我发现了一些程序

使用在其他程序中声明的例程,两者似乎都被一个H文件链接了

。但无论我尝试什么,我收到了错误消息。 (我是

暂时使用Turbo C 3.1)


我当然试图在互联网上搜索答案,但我要么

一无所获或被淹没了没有提供答案的链接

我需要(主要是因为不是母语英语)。可以

任何人都给我一些链接,告诉我如何使用#include或告诉我

在这里?



来自C-FAQ:


10.6:我正在将一个程序分成多个源文件

第一次,我想知道什么放入.c文件和什么

放入.h文件。 (无论如何,。h是什么意思?)


答:作为一般规则,你应该把这些东西放在标题中(.h)

文件:


宏定义(预处理器#defines)

结构,联合和枚举声明

typedef声明
外部函数声明(另见问题1.11)

全局变量声明


将声明或定义放入其中尤为重要

a头文件将在其他几个

文件之间共享。 (特别是,永远不要把外部函数原型

放在.c文件中。另见问题1.7。)


另一方面,当定义或声明时应该

对一个.c文件保密,可以将它保留在那里。


另见问题1.7和10.7。


参考文献:K& R2 Sec。 4.5页.81-2; H& S Sec。 9.2.3 p。 267; CT& P

秒4.6 pp.66-7。


10月16日21:29,Ruud< Ruud.Baltis ... @ apg.nlwrote:


我在TP中编写了一些程序,其中一个目标是将它们翻译成C. b $ b。然后我创建了一些常见的,所谓的单位。我已经发现我可以使用''#include" unit.c"''来包含C源代码。



否。不要这样做。强调:不要那样做。包含带有.c的

文件扩展是一个设计不良的代码,这可能会导致潜在的维护者立刻感到恶心。


但是

我知道的所有程序都使用''#include< xxxx.h>''。我发现了一些程序

使用在其他程序中声明的例程,两者似乎都被一个H文件链接了

。但无论我尝试什么,我收到了错误消息。 (我是

暂时使用Turbo C 3.1)



你究竟尝试了什么?典型的设置是:


ac:函数foo的定义。

啊:函数foo的声明。

main。 c:main的定义,包括ah和引用foo。


单独编译main.c和ac。

链接main.o和ao生成可执行文件。


你尝试了什么不起作用?


10月16日,9:07 * pm,William Pursell < bill.purs ... @ gmail.comwrote:


10月16日21:29,Ruud< Ruud.Baltis ... @ apg.nlwrote :


我在TP中编写了相当多的程序,其中一个目标是将它们翻译成C. b $ b到C.及时我创建了一些常见的,所以叫,单位。我已经发现我可以使用''#include" unit.c"''来包含C源代码。



No. *不要这样做。 *强调:不要那样做。 *包含带有.c的

文件。扩展是一个设计不良的代码,这可能会导致潜在的维护者立刻感到恶心。



我有时会进行优化(它允许编译器为某些编译器内联

)。

它确实有另一个有益的副作用 - 它通常会强制你

来创建幂等头文件。


然后我创建两个项目 - 正常构建项目有单独的C

文件,优化项目有一个这样的文件:


#include" project.h"

的#include" file01.c"

的#include" file02.c"

的#include" file03.c"

#包括" file04.c"

的#include" file05.c"

的#include" file06.c"

的#include" ; file07.c"

的#include" file08.c"

的#include" file09.c"

的#include" file10 .c"


当然,实际的文件会有比这更好的名字。

另一方面,我做的很棒和你在一起除非你需要最后一点点油腻的速度,否则那种邪恶的诡计就不是个好主意。

而且有些编译器足够聪明,可以完成同样的事情。 />
没有俗气的内联技巧。

[snip]


Hallo allemaal,
This is my first message to this group. Until now I mainly wrote
programs in Turbo Pascal. But now I''m involved in a project were
knowledge of C is needed. I''m not unfamiliar with C, but the last time
I used it was maybe 8 years ago.

I wrote quite some programs in TP and one goal is to translate them
into C. In time I created some common, so called, units. I already
found out that I can include a C source using ''#include "unit.c"''. But
all programs I know of use ''#include <xxxx.h>''. I found some programs
using routines declared in other programs and both seemed to be linked
by an H file. But whatever I tried, I received error messages. (I''m
using Turbo C 3.1 for the moment)

Of course I tried to search the Internet for answers but I either
found nothing or was flooded with links that didn''t provide the answer
I needed (mostly caused by not being native English spoken). Can
anybody give me some links to show me how to use #include or tell me
here?

Many thanks in advance!
--
___
/ __|__
/ / |_/ Groetjes, Ruud Baltissen
\ \__|_\
\___| http://Ruud.C64.org

解决方案

On Oct 16, 1:29*pm, Ruud <Ruud.Baltis...@apg.nlwrote:

Hallo allemaal,

This is my first message to this group. Until now I mainly wrote
programs in Turbo Pascal. But now I''m involved in a project were
knowledge of C is needed. I''m not unfamiliar with C, but the last time
I used it was maybe 8 years ago.

I wrote quite some programs in TP and one goal is to translate them
into C. In time I created some common, so called, units. I already
found out that I can include a C source using ''#include "unit.c"''. But
all programs I know of use ''#include <xxxx.h>''. I found some programs
using routines declared in other programs and both seemed to be linked
by an H file. But whatever I tried, I received error messages. (I''m
using Turbo C 3.1 for the moment)

Of course I tried to search the Internet for answers but I either
found nothing or was flooded with links that didn''t provide the answer
I needed (mostly caused by not being native English spoken). Can
anybody give me some links to show me how to use #include or tell me
here?

From the C-FAQ:

10.6: I''m splitting up a program into multiple source files for the
first time, and I''m wondering what to put in .c files and what
to put in .h files. (What does ".h" mean, anyway?)

A: As a general rule, you should put these things in header (.h)
files:

macro definitions (preprocessor #defines)
structure, union, and enumeration declarations
typedef declarations
external function declarations (see also question 1.11)
global variable declarations

It''s especially important to put a declaration or definition in
a header file when it will be shared between several other
files. (In particular, never put external function prototypes
in .c files. See also question 1.7.)

On the other hand, when a definition or declaration should
remain private to one .c file, it''s fine to leave it there.

See also questions 1.7 and 10.7.

References: K&R2 Sec. 4.5 pp. 81-2; H&S Sec. 9.2.3 p. 267; CT&P
Sec. 4.6 pp. 66-7.


On 16 Oct, 21:29, Ruud <Ruud.Baltis...@apg.nlwrote:

I wrote quite some programs in TP and one goal is to translate them
into C. In time I created some common, so called, units. I already
found out that I can include a C source using ''#include "unit.c"''.

No. Do not do that. For emphasis: DO NOT DO THAT. Including a
file with a ".c" extension is a sure sign of poorly designed
code that will cause instant nausea in a potential maintainer.

But
all programs I know of use ''#include <xxxx.h>''. I found some programs
using routines declared in other programs and both seemed to be linked
by an H file. But whatever I tried, I received error messages. (I''m
using Turbo C 3.1 for the moment)

What exactly did you try? The typical set up is:

a.c: definition of function foo.
a.h: declaration of function foo.
main.c: definition of main which includes a.h and references foo.

compile main.c and a.c separately.
link main.o and a.o to produce executable.

What did you try that didn''t work?


On Oct 16, 9:07*pm, William Pursell <bill.purs...@gmail.comwrote:

On 16 Oct, 21:29, Ruud <Ruud.Baltis...@apg.nlwrote:

I wrote quite some programs in TP and one goal is to translate them
into C. In time I created some common, so called, units. I already
found out that I can include a C source using ''#include "unit.c"''.


No. *Do not do that. *For emphasis: DO NOT DO THAT. *Including a
file with a ".c" extension is a sure sign of poorly designed
code that will cause instant nausea in a potential maintainer.

I do it sometimes for optimization (it allows the compiler to inline
better for some compilers).
It does have another beneficial side effect -- it usually forces you
to create idempotent header files.

Then I create two projects -- the normal build project has separate C
files and the optimization project has one file like this:

#include "project.h"
#include "file01.c"
#include "file02.c"
#include "file03.c"
#include "file04.c"
#include "file05.c"
#include "file06.c"
#include "file07.c"
#include "file08.c"
#include "file09.c"
#include "file10.c"

Of course, the actual files will have better names than that.
On the other hand, I do agree with you. That sort of evil trickery is
not a good idea unless you need the last little greasy bit of speed.
And some compilers are smart enough to accomplish the same thing
without the cheesy inline trick.
[snip]


这篇关于如何使用H文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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