Reputation: 15219
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 extern
anywhere?
Upvotes: 4
Views: 2726
Reputation: 2474
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.
Upvotes: 1
Reputation: 16243
/* 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.
Upvotes: 6
Reputation: 9474
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.
Upvotes: 1
Reputation: 6606
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.
Upvotes: 1