1
votes

i knew(i believe so) the difference between variable declaration and definition. i just wanted to know where(in which object) a variable will be defined which was declared with extern linkage(in a header file) and included this header file in many source file where the variable was used. i referred few declaration and definition links could not find info regarding this.

//globalheader.h//

extern int test_var;

//file1.c//
#include "globalheader.h"

static fn1();
fn1
{
   int a;
   a = test_var;
}

//file2.c//
#include "globalheader.h"

static fn2();
fn2
{
   int b = 1;
   test_var = b;
}

In the above code snippet test_var is declared with external linkage in globalheader.h which was included in both file1.c and file2.c. No where this test_var was defined but used, so where(in which object file) does this test_var will be allocated memory?

4
In the source file which define it, you may missing some files.moeCake
It's not defined automatically anywhere, while the opposite is true: a definition also declares the item (and most of the time a definition shouldn't conflict with earlier declaration)Aki Suihkonen

4 Answers

1
votes

Your test_var isn't defined anywhere, so it will not exist in any of your object files, and you will get a linker error stating that test_var is missing if you perform a link.

You are responsible for defining it in one of your compilation units, and it will be in whatever object file you decide, and it's up to the linker to finally decide any memory location.

Note that if you compile this particular code with optimization, the compiler might figure out that your static functions fn1/fn2 are never called, and eliminate them entirely, in which case there will be no code that uses the test_var.

As a side note, you can define a variable in several compilation unit in what's called a tentative definition. See also How do I use extern to share variables between source files?

1
votes

"extern" keyword is only informs the compiler that the memory location is available for the given variable so don't allocate a new memory area. Because of this when you will compile file1.c and file2.c the corresponding object code will not contain the memory area. You can also notice the object file size. if you declared any variable as extern then object size will not be increased for that variable, and once you remove extern and recompile then object size will increase. At the time of linking the linker will search for the object file where memory area is defined for that variable (which is defined as extern in another files) from the given object files. As per you code, you have not defined the variable anywhere so memory area for the variable test_var is not allocated anywhere, so at the time of linking linker will throw error "undefined reference test_var" .

0
votes

You haven't said which object file you would like the variable to live in so the linker should refuse to link your code. Some old linkers may allow this but you would be much better explicitly defining the variable.

0
votes

When you declare a variable as extern it is supposed to exist in some other file. In this case the linker will give an error if this is not the case.

There is an exception in C though (which doesn't exist int C++). If the variable is also assigned a value it will be declared as well.

If you write:

 extern int var;

and var doesn't exist in some other module it will fail.

If you write:

 extern int var = 0;

and var doesn't exist in some other module, it will succeed.

Obviously the variable will reside in the file where it is declared.