将变量放在标头中与将变量放在源中之间的区别

发布于 2024-08-28 22:17:49 字数 289 浏览 4 评论 0原文

假设我声明一个带有变量的头文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜`诱少女 2024-09-04 22:17:49

您只需要一个 count 变量,对吧?那么这一行:

int count;

为您定义一个 count 变量。如果您将其保留在多个文件中(通过将其包含在标头中),那么您将拥有多个 count 变量,每个文件一个,并且您会收到错误,因为它们都具有同名。

extern 关键字所做的就是说在其他文件中定义了一个 count 变量,我们只是让编译器知道它,以便我们可以在这个文件。因此 extern 声明就是您想要放入标头中以供其他文件包含的内容。将 int count; 定义放入 one 源文件中。

You only want one count variable, right? Well this line:

int count;

Defines a count variable for you. If you stick that in multiple files (by including it in a header), then you'll have multiple count 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 a count 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 the extern declaration is what you want to put in your header to be included by your other files. Put the int count; definition in one source file.

橪书 2024-09-04 22:17:49

如果您确实将其放入标头中,那么是的,您可以在源文件中使用它,而无需任何进一步的声明(无论如何,在标头被#included之后)。

#include "someheader.h" 实际上只是复制了 someheader.h 的内容,就好像它当时已全部直接写入包含文件中一样。

然而,这不是您应该使用标头的方式。 int count; 是一个暂定定义 - 您应该只将声明放在头文件中。因此,someheader.h 应该具有:(

extern int count;

这只是一个声明),并且应用程序中的恰好一个源文件应该定义 count

int count = 0;

其他人只需#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 #included, anyway).

#include "someheader.h" effectively just copies the contents of someheader.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. So someheader.h should have:

extern int count;

(which is just a declaration), and exactly one source file in your application should define count:

int count = 0;

The others can just #include "someheader.h" and use count.

别挽留 2024-09-04 22:17:49

区别在于,如果您放入

int count;

标头,如果标头包含在多个源文件中,您将收到重新定义错误仅将该行放入源文件中将导致仅在该源文件中可用的 var (代码在其他源文件不会知道该 var,并且您将能够使用该名称声明另一个 var)。

在标头中,您必须将

extern int count;

和 放入源文件中,

int count;

这将导致声明一个全局变量,可用于包含标头的所有源文件。并且没有重新定义错误。

The difference if that if you put

int count;

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

extern int count;

and in the sounrce file

int count;

This will result in declaring a global var, available to all source files that include your header. And no redefinition errors.

世界和平 2024-09-04 22:17:49

实际编译的文件是预处理器输出的文件,而不是 source.c 文件。

因此,如果您将 int count; 放入头文件中,则 #include 作为标头的每个源文件都将获得其自己的 count 副本。

在这种情况下,您将获得如下所示的源代码:

int count; 

...

extern int 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 #includes the header will get it's own copy of count.

In this case you'll have source which looks like this:

int count; 

...

extern int count; 

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 an extern 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 type float count;. In the first file, you say count = 1, and all the sudden the count in the second file becomes -0.0. Moral of the story? Avoid global variables, and if you must use them, make them static.

List item

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文