全局变量C隐含的extern,当它发生,它是如何工作 [英] C implicit extern for global variable, when does it happen, how does it work

查看:148
本文介绍了全局变量C隐含的extern,当它发生,它是如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解其中C全局变量可以将多个文件(编译单元)之间共享的方式。我读过的优秀问答 href=\"http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c\">。做了一些测试,我还是留下了一些东西后,但我不明白:

基本上我的问题是:如果在头的声明的变量(但没有定义)没有的extern 关键字,它是确定以简单地包括在不同的头编译单元,以使可用的变量,所有这些编译单元?在这种情况下它暗示了一个(且只有一个)编译单元包含code初始化(定义?)这个变量,而之前其他编译单元尝试做与该变量东西它会先被调用。如果这一切是真的,这是程序什么所谓的隐性外部?

我要说明我的问题用一个例子:

页眉MyCommonHeader.h载:

  // MyCommonHeader.h
INT *我; //指向int

文件MyFirstHeader.h包括:

  // MyFirstHeader.h
无效的changeit(INT newValue)以;

文件MyFirstSource.c包括:

  // MyFirstSource.c
#包括MyFirstHeader.h 无效的changeit(INT newValue)以{
    * I =为newValue;
 }

文件MySecondSource.c包括:

  // MySecondSource.c
#包括MyCommonHeader.h
#包括MyFirstHeader.h无效的主要(){
   I =的malloc(sizeof的(INT));
   的changeit(10);
   * I = 23;
}

是否高于code。与到处都一样我的变量进行操作?我是否需要添加的extern 地方?


解决方案

  / * * file.h /
INT *我;

I 变量的暂定定义。这意味着,如果存在用于在翻译单元,其可变没有其他(外部)的定义,它会被刚刚定义一次(初始化为 0 )。如果存在的正好一个匹配(外部)定义我在翻译单元别处,该定义将被使用,并且上述暂定定义将表现为一个声明。

作为一种常见的扩展,编译器扩展了跨翻译单元此行为。这意味着,这样的编译器,你可以放心地包括在尽可能多的翻译单元的头文件,只要你想,并且仍然会有只有一个定义I

这本来是不同的,如果你还明确初始化 I 在头文件:

  / * * file.h /
为int * I = 0;

这是一个实际的定义(不暂定),你可以只包含一个编译单元的头文件,或者你会得到一个多重定义错误。

更好的方法,就是定义一个.c文件的变量,并在头文件中使用的extern

  / * * file.h /
EXTERN为int *我;/ * * file.c中/
为int * I = 0;

这使得它绝对清楚地表明,只有一个定义(一个在.c文件),这其中包括头文件将引用该定义每个编译单元。

I'm trying to understand the ways in which a C global variable can be shared between multiple files (compilation units). I've read the excellent question and answer here. However after doing a few tests I'm still left with some stuff I don't get:

Basically my question would be: if there's a variable declared (but not defined) in a header WITHOUT the extern keyword, is it ok to simply include that header in various compilation units in order to make available that variable to all those compilation units? In this scenario it's implied that one (and only one) compilation unit contains the code for initializing (defining?) that variable, and it will be called first before the other compilation units try to do anything with that variable. If all this is true, is this procedure what's called "implicit extern" ?

I'll illustrate my question with an example:

Header "MyCommonHeader.h" contains:

//MyCommonHeader.h
int* i; //pointer to an int

File MyFirstHeader.h contains:

//MyFirstHeader.h
void changeIt(int newValue);

File MyFirstSource.c contains:

//MyFirstSource.c
#include "MyFirstHeader.h"

 void changeIt(int newValue) {
    *i = newValue;
 }

File MySecondSource.c contains:

//MySecondSource.c
#include "MyCommonHeader.h"
#include "MyFirstHeader.h"

void main() {
   i = malloc(sizeof(int));
   changeIt(10);
   *i = 23;
}

Does the above code operates with the same i variable everywhere? Do I need to add externanywhere?

解决方案

/* file.h */
int* i;

is a tentative definition of the i variable. This means that if there is no other (external) definition for that variable in the translation unit, it will be defined just once (initialized to 0). If there is exactly one matching (external) definition of i elsewhere in the translation unit, that definition will be used, and the tentative definition above will behave as a declaration.

As a common extension, compilers extend this behavior across translation units. This means, for such compilers, you can safely include that header file in as many translation units as you want, and there would still be only one definition of i.

It would have been different if you had also explicitly initialized i in the header file :

/* file.h */
int* i = 0;

This is an actual definition (not tentative), and you can only include that header file in one compilation unit, or you'd get a multiple definition error.

The better way, is to define the variable in a .c file, and use extern in the header file :

/* file.h */
extern int* i;

/* file.c */
int* i = 0;

This makes it absolutely clear that there is only one definition (the one in the .c file), and that every compilation unit where the header file is included will refer to that definition.

这篇关于全局变量C隐含的extern,当它发生,它是如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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