在Header文件中声明变量 [英] Declaring variables in a Header file

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

问题描述

我有一个头文件header.h,其中包含

I have a header file header.h which contains

int a=10

它是在Header中定义和声明的。我知道只有声明发生在Header文件中



引用header.h的源文件。如果我把

It a is defined and declared in Header . I know only declaration takes place inside Header file

Source file where header.h is referenced. If i put a

printf("%d",a); 

它打印10。将在源中创建变量如何工作?我想知道这个基本的工作原理。

It prints 10 . Will variable be created in source how this works? I wish to know how this basic works .

推荐答案

头文件是课程代码的独立部分,就像你可以拥有两个独立的.C文件一样将它们组合成一个程序。理论上,您可以将.H文件中的所有内容都删除,并将其直接粘贴到.C文件中,它将完全相同。我们不这样做,因为它会使.C文件变得庞大而混乱,并且很难在不同的源文件中使用相同的标头。例如,如果在代码中定义一个常量,即您可以处理的FooBar对象的最大数量,则在.H文件中声明它意味着从不同的.C文件中调用代码的函数可以使用相同的常量和总是得到与你相同的价值。



如果你在代码中声明并将其复制到另一个文件中,那么如果你需要它就不会得到更新由于某种原因导致它减半,然后事情开始分崩离析并且代码中断没有明显的原因。


如果你在.H文件中声明变量,那么与在函数外部的.C文件中声明它们一样,以及全局变量。



就个人而言,我不会在.H文件中声明变量 - 只是因为它很危险 - 如果你在两个地方使用.H,你会得到两个不同的同名变量,并且它们的价值不一样并不明显。
Header files are separate parts of the course code, in the same way that you can have two separate .C files and combine them into a single program. In theory, you can take everything in a .H file, and paste it directly into the .C file and it will work exactly the same. We don''t do that, because it makes the .C file big and cluttered, and it makes it difficult to use the same headers in different source files. For example, if you define a constant in your code which is the maximum number of FooBar objects you can handle, then declaring it in a .H file means that the functions which call you code from different .C file can use the same constant and always get the same value as you do.

If you declare it in your code and copy it into a different file then it won''t get updated if you need to half it for some reason, and then things start to fall apart and code breaks for no obvious reason.

If you declare variables in a .H file, then that is the same as declaring them in your .C file, outside the functions, so as global variables.

Personally, I don''t declare variables in .H files - simply because it is dangerous - if you use the .H in two places, you will get two different variables with the same name, and it isn''t obvious that they are not the same value.


由于古老的C ++技术基于头文件及其链接,即使是这么简单也不是那么简单。

如果你在头文件中定义(不仅仅是声明)一个变量,只要包含这个文件,这个声明就会重复,这将被视为尝试创建多个对象。



因此,定义应该只有一个,在一个C文件中;在头文件中,它应该被引用为 extern



这在很多地方都有讨论。例如,请参阅: http:// stackoverflow。 com / questions / 2868651 / includes-c-header-file-with-lots-of-global-variables [ ^ ]。



First of所有,我建议一定要避免全局变量。如果你仍然需要一些,我会建议避免这种情况,并尽可能依赖课程。你可以在上面提到的答案中找到一些样本。



-SA
Due to archaic C++ technology based on header files and its linking, even such trivial thing is not that simple.
If you define (not just declare) a variable in a header file, this declaration will be repeated whenever this file is included, which will be treated as an attempt to create more than one object.

Therefore, the definition should be only one, in a single C file; and in header file it should be referenced as extern.

This is discussed in many places. Please see, for example, here: http://stackoverflow.com/questions/2868651/including-c-header-file-with-lots-of-global-variables[^].

First of all, I would advise to avoid global variables by all means. If you still need some, I would advise to avoid such situation and rely on classes whenever possible. You can find some sample in one of the answers referenced above.

—SA


AC / C ++程序由''单元''组成。这些单元是源文件,C中的.c或C ++中的.cpp。在编译开始之前,会发生一个称为转换的过程:首先,所有#include指令都被解析,这基本上意味着包含的头文件的内容被复制到.c / .cpp文件中,代替#include指令。然后,解析宏,并将生成的.c / .cpp文件称为翻译单元。



现在,声明变量的问题在包含在多个源文件中的标题中,它是在多个翻译单元中生成具有相同名称的变量的声明。链接器不喜欢它,因为所有符号必须是唯一可识别的。因此,您有两个选择,直接在源文件中声明它们,或者使用extern关键字,如已经指示的那样。
A C/C++ program consists of ''units''. These units are the source files, .c in C or .cpp in C++. Before the compilation starts a process called translation takes place: first, all the #include directives are resolved, which basically means the content of the included header files is copied in the .c/.cpp file, in the place of the #include directive. Then, resolving of macros takes place, and the resulting .c/.cpp file is called a ''translation unit''.

Now, the problem with declaring variables in a header that is including in multiple source files, is that it generates declarations of variables with same name in multiple translation units. The linker doesn''t like that, because all symbols must be uniquely identifiable. Therefore you have two choices, declare them in the source file directly, or use the extern keyword, as already instructed.


这篇关于在Header文件中声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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