C implicit extern for global variable, when does it happen, how does it work

风格不统一 提交于 2019-12-24 05:01:49

问题


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?


回答1:


/* 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.




回答2:


If you declare your variable in a header and have any source file declaring this same variable too, each compilation unit will have a different instance of this variable.

Declaring it in one single compilation unit, and adding an "extern ..." in your header will make all compilation units access the same global variable.




回答3:


Declaring in a header file which is then included in multiple places, you'll end up with multiple instances of i (and potentially compile or link problems).so it's a better idea to use extern in header and define it in one .c file .After this you can include header without repeting variables definition.




回答4:


Does the above code operates with the same i variable everywhere?

Yes.

Do I need to add externanywhere?

In this example, not needed. Because MyCommonHeader.h is included only once where i is defined.

In the following example, extern is more useful.

//main.c

#include <stdio.h>
int globaldata;
int main()
{
..
}

//Includes.h    
extern int globaldata;


//Feature1.c
#include "Includes.h"

int func()
{
   globaldata++;
}

//Feature2.c
#include "Includes.h"

int func_new()
{
   globaldata = globaldata * 100;
}

globaldata is a global variable used by feature1.c and feature2.c. If u define that variable, then it will be error, like multiple declaration error.

so always extern is used in this scnario and that header only will be included.



来源:https://stackoverflow.com/questions/16542258/c-implicit-extern-for-global-variable-when-does-it-happen-how-does-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!