Global variables in header file
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?
Solution 1:
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.)
Solution 2:
Don't initialize variables in headers. Put declaration in header and initialization in one of the c
files.
In the header:
extern int i;
In file2.c:
int i=1;
Solution 3:
You should not define global variables in header files. You can declare them as extern
in header file and define them in a .c
source file.
(Note: In C, int i;
is a tentative definition, it allocates storage for the variable (= is a definition) if there is no other definition found for that variable in the translation unit.)
Solution 4:
The currently-accepted answer to this question is wrong. C11 6.9.2/2:
If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
So the original code in the question behaves as if file1.c
and file2.c
each contained the line int i = 0;
at the end, which causes undefined behaviour due to multiple external definitions (6.9/5).
Since this is a Semantic rule and not a Constraint, no diagnostic is required.
Here are two more questions about the same code with correct answers:
- Tentative definitions in C and linking
- How do I use extern to share variables between source files?