admin 管理员组

文章数量: 888144


2023年12月19日发(作者:源码下载之后如何运行)

c语言头文件详解(C language header files detailed)

C language header files detailed

C language in the.H file and I know its long-standing, although

the method is not very complicated, but I was after a few months

of "do not understand" period, "a few years" period before

gradually understand his true colours. The reason I pulled,

blunt and eager to learn and read without thorough

understanding is one of the reasons, but there are also other

reasons. One reason: for smaller projects, the role is not easy

to be fully developed; in other words, even if you do not know

his detailed method of use, the project continues, the program

still run on the computer. Reason two: now the C language books

are not only detailed on the C language grammar in detail, but

for the entire program file structure is not, for several of

the more famous book C language, but not a.H file is used to

write a thorough comparison. I would venture to me in my way

to the understanding of.H, to introduce.

Let our thoughts take the time machine back to the first year

of college. C the teacher was on the platform talking about our

first C language program: Hello world!

File name First.c

(main)

{

Printf ("Hello world!");

}

Routine -1

Look at the program above and there is no.H file. Yes, no,

everything in the world is a process without experience. Our

understanding of.H, I think, needs to begin with this process.

You really don't need the.H file at this time, because the

program is too simple to be needed at all. So how do you need

it? Let's make this program a little more complicated. Look at

this one below,

File name First.c

PrintStr ()

{

Printf ("Hello world!");

}

(main)

{

PrintStr ()

}

Routine -2

Not yet. Let's change the program a little bit more

File name First.c

(main)

{

PrintStr ()

}

PrintStr ()

{

Printf ("Hello world!");

}

Routine -3

Wait, not just change the order, but the result is quite

different. Let's compile the routine -2

As with the routine -3, you'll see that the routine -3 is

compiled, but then we need to get to know the concepts in another

C language: scope

We only tell the top-level scope related to.H files here, the

top-level scope is from the point source program extends to the

end of text, printStr () this function, he does not have a

separate statement, only the definition, then from his

definition of the start line to the first.c file that is to say,

in the end. -2 (main) in the routine reference point function,

is his scope. -3 (main) routine reference point function, is

not his scope, so will compile error. How to do this? There are

two ways, one is to let us return to the routine -2 sequence

for us, not what, after the first who do not like it, as long

as the compiler, the program can run, let (main) file is always

put at the end of it. Let us see another routine, let's take

a look at this method is not at any time will be Use.

File name First.c

Play2 ()

{

.................. .

Play1 ()

.................. .

}

Play1 ()

{

........................ .

Play2 ()

........................

);

}

(main)

{

Play1 ()

}

Routine -4

Maybe most of them will be seen, and that's one of the algorithms

that are often used. Nested functions, then let's see, which

of these two functions, play1 and play2, should be put in the

front?

At this point, we need to use the second method, using the

declaration

File name First.c

Play1 ();

play2 ().

play2 ()

{

.

play1 ()

.

}

play1 ()

{

.

play2 ()

.

);

}

main ()

{

play1 ()

}

例程 - 4

经历了我的半天的唠叨, 加上四个例程的说明, 我们终于开始了用量变引起的质变, 这篇文章的主题.h文件快要出现了.

一个大型的软件项目, 可能有几千个, 上万个play, 而不只是play1, play2这么简单, 这样就可能有n个类似 play1 (), (),

play2 这样的声明, 这个时候就需要我们想办法把这样的play1 (),

(), play2 也另行管理, 而不是把他放在.c文件中, 于是.h文件出现了.

文件名 first.h

play1 ().

play2 ().

文件名 first.

# include "first.h"

play2 ()

{

.

play1 ()

.

}

play1 ()

{

.

play2 ()

.

);

}

main ()

{

play1 ()

}

例程 - 4

各位有可能会说, 这位janders大虾也太罗嗦了, 上面这些我也知道, 你还讲了这么半天, 请原谅, 如果说上面的内容80% 的人都知道的话, 那么我保证, 下面的内容, 80% 的人都不完全知道. 而且这也是我讲述一件事的一贯作风, 我总是想把一个东西说明白, 让那些刚刚接触c的人也一样明白.

上面是.h文件的最基本的功能, 那么.h文件还有什么别的功能呢?

让我来描述一下我手头的一个项目吧.

这个项目已经做了有10年以上了, 具体多少年我们部门的人谁都说不太准确, 况且时间并不是最主要的, 不再详查了. 是一个通讯设备的前台软件, 源文件大小共 51.6m, 大小共1601个文件, 编译后大约10m, 其庞大可想而知, 在这里充斥着错综复杂的调用关系, 如在second.c中还有一个函数需要调用first.c文件中的play1函数,

如何实现呢?

sencond.h 文件

play1 ().

sencond.c文件

(* * *)

{

.

play ().

.

}

例程.

在sencond.h文件内声明play1函数, 怎么能调用到first.c文件中的哪个play1函数中呢? 是不是搞错了, 没有搞错, 这里涉及到c语言的另一个特性: 存储类说明符.

c语言的存储类说明符有以下几个, 我来列表说明一下

说明符 用法

self 只在块内变量声明中被允许, 表示变量具有本地生存期.

出现在顶层或块的外部变量函数与变量声明中 表示声明的对象

extern.

具有静态生存期, 连接程序知道其名字.

static 可以放在函数与变量声明中. 在函数定义时, 其只用于指定函数

名, 而不将函数导出到连接程序. 在函数声明中, 表示其后面会有

定义声明的函数, 存储类为static. 在数据声明中, 总是表示定义

的声明不导出到连接程序.

无疑, 在例程 - 5中的second.h和first.h中, 需要我们用extern

标志符来修饰play1函数的声明, 这样, play1 函数就可以被导出到连接程序 ().

It is the call in the first.c file, or call in the second.c file,

the program will be very smart connected with our wishes, his

connection to the definition of the play1 function in the

first.c file. Instead we also have to write a play1 function

in the second.c file.

But then there is a small problem, in routine -5, we did not

use the extern flag to play1 ah, this involves another problem,

C language has the default storage class identifier specified

in the C99. All of the top, the default storage class identifier

is extern. If the original ah ha ha., recall the routine -4,

that is, we are in ignorance of the situation, even accidentally,

use the extern modifier, the play1 function in the first.h

statement or if not be connected in the program is derived, then

we call play2 () in him, is to find the actual definition of

location.

So how are we to tell which header statement are defined in the

corresponding.C file, which is not? It may not be necessary,

because no matter which is defined in the file, the smart

connection program will be there to help us find, and exported

to the connection program, but I think he did necessary. Because

we need to know what is the specific content of this function,

have what function, a new demand after I may change it, I need

in a short time can find the definition of this function, then

I introduce in the C language for the specification of a person:

If the function declared in the.H file is defined in its

corresponding.C file, we do not use the extern modifier when

this function is declared, and, if it is otherwise, the extern

modifier must be displayed

In this way, in the C language.H file, we see two types of

function declarations, with extern, without extern, simple and

straightforward, one that refers to external functions, and one

that is defined by our own lives

As follows:

Sencond.h file

Extern, play1 ();

There are copious and fluent writing so much for the function,

and in fact the.H file is not as a function of the queen. Open

a.H file of our project we found that in addition to the function,

there are other things, it is a global variable.

In large projects, the use of global variables is inevitable,

for example, use a global variable to G_test in first.c, so we

can define the TPYE G_test. in first.h, and use of function

similar to that in second.c our developers found that he also

need to use the global variables, and that, to be the same with

the first.c in how to deal with? Yes, we can follow treatment

function, in the second.h TPYE G_test again, according to the

usage of extern and C in the default language storage type, in

a statement two head of the TPYE G_test, in fact, the storage

type is extern, that is to say no we worry about the connection

program will help us all. But how can we distinguish between

global variables which is the definition statement, which is

quoted statement? This function is more complex than the

general. In the C language, there are several models to

distinguish between:

1, initialization statement model

In the top level declaration, the initialization statement

exists to indicate that the declaration is a definition

declaration, and the other statement is a reference declaration.

Of all the files in the C language, there is only one definition

declaration.

According to this model, we can define the following TPYE

G_test=1 in first.h; then make sure the definition declaration

is in first, and all other declarations are reference

statements.

2, omit the storage type specification

In this model, all references to display the statement includes

a storage class extern, and the only definition statement for

each external variable is omitted from the storage class.

This is similar to the way we deal with functions, but no more

examples.

There is also a need to explain, and this is not very related

to this article, but the previous paragraph, a friend

encountered this problem, I believe many people will encounter,

it is an array of global variables.

The problems he encountered were as follows:

When declaring a definition, the array is defined as follows:

Int G_glob[100];

In another file, the following statement is quoted:

Int * G_glob;

In VC, you can compile it. This situation is fuzzy and requires

attention. The array is similar to the pointer, but it doesn't

mean that the variable is pointer to the declaration of the

array. The above mentioned procedures found problems in

operation, in the file reference statement, using this pointer

always prompt memory access errors, we do not have the original

connection program pointer and array with connection, don't

treat them as a single definition, but that is not related to

the two definition of course, there will be mistakes. The

correct method of use is to declare as follows in the citation

statement:

Int G_glob[10];

And it would be better to add a extern to make it clearer.

Extern int G_glob[10];

It should be noted that in the quoted statement because it does

not need to involve the allocation of memory, can be simplified

as follows, such changes in the need for the global variable

length, do not have to declare all all references to modify.

Extern int G_glob[];

The C language is now at the bottom of the core programming,

the most widely used language before, will not have changed much,

although now Java,.Net and other languages and tools have a

certain impact on the C, but we see on the computer as the core,

the other language is in any case can not be replaced. And this

area is what we are obsessed by computers for programmers.


本文标签: 函数 声明 文件 变量 语言