在头文件中的全局变量 [英] Global variables in header file
问题描述
我有一个2模块(.c文件)和一个.H头文件:
file1.c中:
的#include<&stdio.h中GT;
#包括global.h诠释的main()
{
我= 100;
的printf(%d个\\ N,I);
富();
返回0;
}
file2.c中
的#include<&stdio.h中GT;
#包括global.h无效美孚()
{
我= 10;
的printf(%d个\\ N,I);
}
global.h
INT I;
EXTERN无效美孚()
当我这样做GCC在file1.c file2.c中的一切工作正常,我也得到了预期的输出。现在,当我初始化的变量'我'在头文件中说0,重新编译我得到一个链接错误:
/tmp/cc0oj7yA.o :( BSS +为0x0):`的我'多重定义
/tmp/cckd7TTI.o:(.bss+0x0):首先这里定义
如果我只是编译头文件初始化file1.c中(除去调用美孚())即GCC file1.c中,一切工作正常。这是怎么回事?
有3方案,您描述:
- 与2
.C
文件和INT I;
在头 。 - 随着2
.C
文件和INT I = 100;
在标题(或任何其他值;这并不重要)。 - 在1
.C
文件和INT I = 100;
在头 。
在每个场景,想象插入 .C
文件头文件的内容和 .C
编译成的.o
文件,然后这些文件连在一起。
然后出现以下情况:
-
工作正常,因为已经提到的暂定定义的:每个
的.o
文件包含其中之一,因此链接器说OK。 p> -
不工作,因为这两个
的.o
文件包含一个值,该碰撞(即使它们具有相同的值)的定义 - 也有可能只有一个与其中在给定时间连接在一起的所有的.o
文件的任何给定的名称。 -
的作品当然,因为你只有一次
的.o
文件,所以没有可能性的碰撞。
恕我直言,一个干净的事情将是
- 要放要么
的extern INT I;
或只是INT I;
进入头文件, - ,然后把我的真实的定义(即
INT I = 100;
)到file1.c中
。在这种情况下,这个初始化得到在节目的开始,并在相应的行中使用的main()
可以省略。 (此外,我希望命名仅仅是一个例子;请不要名字,我在实际程序的全局变量)
I have a 2 modules (.c files) and one .h header file:
file1.c:
#include <stdio.h>
#include "global.h"
int main()
{
i = 100;
printf("%d\n",i);
foo();
return 0;
}
file2.c
#include <stdio.h>
#include "global.h"
void foo()
{
i = 10;
printf("%d\n",i);
}
global.h
int i;
extern void foo()
When I do gcc file1.c file2.c everything works fine and I get the expected output. Now, when I initialize variable 'i' in the header file to say 0 and compile again I get a linker error:
/tmp/cc0oj7yA.o:(.bss+0x0): multiple definition of `i'
/tmp/cckd7TTI.o:(.bss+0x0): first defined here
If I just compile file1.c (removing call to foo()) with the initialization in the header file i.e. gcc file1.c, everything works fine. What is going on?
There are 3 scenarios, you describe:
- with 2
.c
files and withint i;
in the header. - With 2
.c
files and withint i=100;
in the header (or any other value; that doesn't matter). - With 1
.c
file and withint i=100;
in the header.
In each scenario, imagine the contents of the header file inserted into the .c
file and this .c
file compiled into a .o
file and then these linked together.
Then following happens:
works fine because of the already mentioned "tentative definitions": every
.o
file contains one of them, so the linker says "ok".doesn't work, because both
.o
files contain a definition with a value, which collide (even if they have the same value) - there may be only one with any given name in all.o
files which are linked together at a given time.works of course, because you have only one
.o
file and so no possibility for collision.
IMHO a clean thing would be
- to put either
extern int i;
or justint i;
into the header file, - and then to put the "real" definition of i (namely
int i = 100;
) intofile1.c
. In this case, this initialization gets used at the start of the program and the corresponding line inmain()
can be omitted. (Besides, I hope the naming is only an example; please don't name any global variables asi
in real programs.)
这篇关于在头文件中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!