在头文件中的全局变量 [英] Global variables in header file

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

问题描述

我有一个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方案,您描述:


  1. 与2 .C 文件和 INT I; 在头

  2. 随着2 .C 文件和 INT I = 100; 在标题(或任何其他值;这并不重要)。

  3. 在1 .C 文件和 INT I = 100; 在头

在每个场景,想象插入 .C 文件头文件的内容和 .C 编译成的.o 文件,然后这些文件连在一起。

然后出现以下情况:


  1. 工作正常,因为已经提到的暂定定义的:每个的.o 文件包含其中之一,因此链接器说OK


  2. 不工作,因为这两个的.o 文件包含一个值,该碰撞(即使它们具有相同的值)的定义 - 也有可能只有一个与其中在给定时间连接在一起的所有的.o 文件的任何给定的名称。


  3. 的作品当然,因为你只有一次的.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:

  1. with 2 .c files and with int i; in the header.
  2. With 2 .c files and with int i=100; in the header (or any other value; that doesn't matter).
  3. With 1 .c file and with int 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:

  1. works fine because of the already mentioned "tentative definitions": every .o file contains one of them, so the linker says "ok".

  2. 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.

  3. 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 just int i; into the header file,
  • and then to put the "real" definition of i (namely int i = 100;) into file1.c. In this case, this initialization gets used at the start of the program and the corresponding line in main() can be omitted. (Besides, I hope the naming is only an example; please don't name any global variables as i in real programs.)

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

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