头文件问题 [英] Header files question

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

问题描述

嗯,K& R终于得到头文件!!!


我可以问这个问题。 (已查看常见问题解答)


我正在使用Xcode编译程序,所以很可能这个

做了一些奇怪的事情。


头文件有什么独特之处?


例如,如果我这样定义一个整数:


i = 96;


我可以使用i,例如main.c而没有先声明它(使用

extern i),如果是这样,是什么让头文件不同说,

" other_file.c"具有相同的定义?


提前致谢。

Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)

I am using "Xcode" to compile the program, so it may well be that this
is doing something strange.

What is unique to a header file?

For example, if I define an integer thus:

i=96;

can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file.c" with the same definition?

Thanks in advance.

推荐答案

我的意思是 int i = 96;

mdh写道:
I meant "int i=96;
mdh wrote:

嗯,K& R终于得到头文件!!!


我可以问这个问题。 (已查看常见问题解答)


我正在使用Xcode编译程序,所以很可能这个

做了一些奇怪的事情。


头文件有什么独特之处?


例如,如果我这样定义一个整数:


int i = 96;


我可以使用i在,例如main.c而没有首先声明它(使用

extern i),如果是这样,是什么使得头文件不同说,

" other_file。 C"具有相同的定义?


提前致谢
Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)

I am using "Xcode" to compile the program, so it may well be that this
is doing something strange.

What is unique to a header file?

For example, if I define an integer thus:

int i=96;

can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file.c" with the same definition?

Thanks in advance.


mdh写道:
mdh wrote:

我的意思是int i = 96;


mdh写道:
I meant "int i=96;
mdh wrote:

>好吧,K& R终于得到了头文件!!!

请问这个问题。 (已查看常见问题解答)

我正在使用Xcode编译程序,所以很可能这个做了一些奇怪的事情。

头文件有什么独特之处?
>Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)

I am using "Xcode" to compile the program, so it may well be that this
is doing something strange.

What is unique to a header file?



很少。这非常类似于你将所有内容放在.c文件中的代码

而不是说#include" myheader.h"

Little. It is very much like if you had placed everything in the code
in your .c file instead of saying #include "myheader.h"


>例如,如果我这样定义一个整数:

int i = 96;

我可以吗使用i in,例如main.c而不首先声明它(使用
extern i),如果是这样,是什么使得头文件不同说,
" other_file.c"有相同的定义?
>For example, if I define an integer thus:

int i=96;

can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file.c" with the same definition?



没什么。如果你包含一个定义超过

一个.c文件的头文件,你得到多个定义 - 你不想要的。

完全定义它你的一个.c文件,如果你需要

使其可用于其他.c文件,你可以例如在头文件中放置一个

声明(使用extern关键字)

你需要包含的地方。

Nothing. If you include a header file with a definition in more than
one .c file, you get mutiple definition - which you don''t want.
Define it in exactly one of your .c files, and if you need to
make it available to other .c files, you can e.g. place a
declaration (use the extern keyword) in a header file which
you include where needed.







mdh写道:
mdh wrote:

嗯,K& ; R终于得到头文件!!!


我可以问这个问题。 (已查看常见问题解答)
Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)


头文件有什么独特之处?
What is unique to a header file?



Nils O. Sel?sdal写道:

Nils O. Sel?sdal wrote:


很少。这非常类似于你在.c文件中的代码中放置了所有内容而不是说#include" myheader.h"
Little. It is very much like if you had placed everything in the code
in your .c file instead of saying #include "myheader.h"


int i = 96;我可以使用i,例如main.c而不首先声明它(使用

extern i),如果是这样,是什么让头文件不同说,

" other_file.c"有相同的定义?
int i=96; can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file.c" with the same definition?



没什么。 ...............在头文件中放置一个声明(使用extern关键字)

你需要包含哪些内容。


Nothing. ............... place a declaration (use the extern keyword) in a header file which
you include where needed.



所以Nils,你在说什么?

头文件中的声明?

other_c_files中的定义?


因此,名称是.h。简单地说方便如果你有一个庞大的

程序,它可以让调试等更容易,或者如果有多个人在工作吗?根据你的说法,你可以将所有声明放在

中说,my_specialfile.c,在必要时使用#include,但它会不会那么清楚?


谢谢


So Nils, what you are saying? is
Declarations in the header file?
Definitions in other_c_files?

So, is the designation ".h" simply "convenience" if you have a massive
program, it makes it easier debugging etc or if multiple people are
working on it? From what you say, you could put all declarations in
say, "my_specialfile.c", use # include where necessary, but it would
not be as clear?

Thanks


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

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