将变量放在标头中与将变量放在源中之间的区别
假设我声明一个带有变量的头文件:
int count;
然后在源文件中,我想使用 count
。我是否必须将其声明为:
extern int count
或者我可以在源文件中使用它吗?所有这些都假设我有#include "someheader.h"
。或者我应该在源文件中声明它? 将 count
放入头文件与源文件中有何区别?还是没关系?
Say I declare a header file with a variable:
int count;
Then in the source file, I want to use count
. Do I have to declare it as:
extern int count
Or can I just use it in my source file? All assuming that I have #include "someheader.h"
. Or should I just declare it in the source file? What is the difference between putting count
in the header file vs the source file? Or does it not matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需要一个
count
变量,对吧?那么这一行:为您定义一个
count
变量。如果您将其保留在多个文件中(通过将其包含在标头中),那么您将拥有多个count
变量,每个文件一个,并且您会收到错误,因为它们都具有同名。extern
关键字所做的就是说在其他文件中定义了一个count
变量,我们只是让编译器知道它,以便我们可以在这个文件。因此extern
声明就是您想要放入标头中以供其他文件包含的内容。将int count;
定义放入 one 源文件中。You only want one
count
variable, right? Well this line:Defines a
count
variable for you. If you stick that in multiple files (by including it in a header), then you'll have multiplecount
variables, one for each file, and you'll get errors because they'll all have the same name.All the
extern
keyword does is say that there is acount
variable defined in some other file, and we're just letting the compiler know about it so we can use it in this file. So theextern
declaration is what you want to put in your header to be included by your other files. Put theint count;
definition in one source file.如果您确实将其放入标头中,那么是的,您可以在源文件中使用它,而无需任何进一步的声明(无论如何,在标头被
#include
d之后)。#include "someheader.h"
实际上只是复制了someheader.h
的内容,就好像它当时已全部直接写入包含文件中一样。然而,这不是您应该使用标头的方式。
int count;
是一个暂定定义 - 您应该只将声明放在头文件中。因此,someheader.h
应该具有:(这只是一个声明),并且应用程序中的恰好一个源文件应该定义
count
:其他人只需
#include "someheader.h"
并使用count
。If you did put that into the header, then yes, you could just use it in the source file without any further declaration (after the point where the header has been
#include
d, anyway).#include "someheader.h"
effectively just copies the contents ofsomeheader.h
in, as if it had all been directly written in the including file at that point.However, this is not the way you're supposed to use headers.
int count;
is a tentative definition - you are supposed to only put declarations in header files. Sosomeheader.h
should have:(which is just a declaration), and exactly one source file in your application should define
count
:The others can just
#include "someheader.h"
and usecount
.区别在于,如果您放入
标头,如果标头包含在多个源文件中,您将收到重新定义错误仅将该行放入源文件中将导致仅在该源文件中可用的 var (代码在其他源文件不会知道该 var,并且您将能够使用该名称声明另一个 var)。
在标头中,您必须将
和 放入源文件中,
这将导致声明一个全局变量,可用于包含标头的所有源文件。并且没有重新定义错误。
The difference if that if you put
in the header, you'll get a redefinition error if the header's included in more than one source file Putting that line in the source file only will result in a var available only in that source file (code in other source files will not be aware of that var and you'll be able to declare another var with that name).
In the header, you have to put
and in the sounrce file
This will result in declaring a global var, available to all source files that include your header. And no redefinition errors.
实际编译的文件是预处理器输出的文件,而不是 source.c 文件。
因此,如果您将
int count;
放入头文件中,则#include
作为标头的每个源文件都将获得其自己的count
副本。在这种情况下,您将获得如下所示的源代码:
如果您使用的是 gcc,请尝试
gcc -E file.c
。这将指示它仅运行预处理器,以便您可以看到实际提供给编译器的内容。正如 @Neil 所建议的,您需要在 C 文件中声明
int count;
。如果您希望另一个 C 文件能够引用此变量,则可以在另一个文件中(或在另一个文件包含的头文件中)放置一个extern int count;
声明。顺便说一句,我最喜欢的 C 错误之一是当您声明一个全局变量时,如下所示:
int count;
,然后在另一个文件中声明另一个同名但类型不同的全局变量浮点计数;
。在第一个文件中,您输入count = 1
,突然间,第二个文件中的count
变为-0.0
。这个故事的寓意是什么?避免全局变量,如果必须使用它们,请将它们设置为静态
。列表项
The file that actually gets compiled is what the preprocessor spits out, not the source.c file.
So, if you put
int count;
in a header file, every source file which#include
s the header will get it's own copy ofcount
.In this case you'll have source which looks like this:
If you're using gcc, try
gcc -E file.c
. This will instruct it to only run the preprocessor so you can see what's actually being fed to the compiler.As @Neil suggested, you will want to declare
int count;
in a C file. If you want another C file to be able to reference this variable, then you put anextern int count;
declaration in the other file (or in a header file that the other includes).Incidentally, one of my favorite C bugs is when you declare a global variable like this:
int count;
, then in another file you declare another global variable with the same name, but a different typefloat count;
. In the first file, you saycount = 1
, and all the sudden thecount
in the second file becomes-0.0
. Moral of the story? Avoid global variables, and if you must use them, make themstatic
.List item