Question

Sorry for this type of question. But, I am very curious about the keyword extern in C\C++.

while searching for explanation for extern I got to know that extern tell the compiler that the variable or function is already defined in some other file or program.

But if this is a case then why we use extern?

as I tried some codes as follows:

extern int var;
int main(void)
{
 var = 10;
 return 0;
}

This code is giving me an error message as unresolved external symbol "int var" (?var@@3HA).

and if I am using some code like:

extern int var;
int main(void)
{
 int var = 10;
 return 0;
}

It is not showing any error and gives value same as I have defined in main function.

So, Can any one help me about the behavior of extern? I am little confused on this. Please forgive me if it is not a valid question. Thank you in Advance.

Was it helpful?

Solution

extern is used to refer to a variable in another translation unit ("source file"). For instance, your code in main.c looks like this:

extern int var;
int main(void)
{
    var = 10;
    return 0;
}

This code contains a declaration for an integer named var, but no definition, because extern explicitly says: "the definition for this is somewhere else"

You could define another source file, say, other.c:

int var = 0;

Then, after you add this new translation unit to your build command, your program will link fine, and the code in both files can operate on the shared var variable.

In the second version, you just override the declaration of the extern var with a local variable in your main function. Since the extern var is not (ODR-)used anymore, the linker does not require it, so your executable can build successfully.

OTHER TIPS

The first version fails since you must also provide a definition of the extern-declared variable, i.e. by linking with some other C file that has

int var;

in its global scope.

The second one succeeds since the int var = 10; line in main() shadows the global declaration; this is generally a bad thing to be doing if you wanted the variable to be extern, i.e. shared among many C files. Of course, such sharing in itself is often quite bad too (see: global variables).

extern is used to indicate that a variable exists in another compilation unit. If you had:

main.cpp

extern int var;
int main(void)
{
 var = 10;
 return 0;
}

and:

var.cpp

int var;

Then you wouldn't have got the linker error as the linker would have hooked up the use of var in main.cpp with it's decleration in var.cpp

In your second example you've defined an extern int var but you never reference it. The int var = 10 is a completely different int var from the extern one. When they linker runs it notices that you never use the extern int var variable so doesn't bother to search for it.

I would suggest you must clear your understanding of definition and declaration of variable.

extern keyword is used when you want to say that the variable was allocated memory you want only to declare the variable.

When you declare the any variable using extern keyword compiler will try to find the definition of the same variable which can be present before or after declaration.

In the first case you are just declaring the variable not assigning it any memory i.e not defining variable.

you can go through this for better understanding of extern keyword.

If you have declared a global variable, let's say double myvar=5 in file1.cpp and you want to access that variable in file2.cpp, then in your file2.cpp you declare as

extern double myvar;

In C++, each file is called a compilation unit. For the file to be compiled to an object file, you need all variables declared however you do not need to initialize or do an assignment on those variables.

Returning to the example:

File1.cpp: double myvar=5 this is both a declaration and initialization. You can compile file1.cpp to file1.o.

File2.cpp: Assuming that you are using myvar somewhere in file2.cpp and also you want to access the value (maybe after a calculation) in file1.cpp. Therefore, in order to be able to compile file2.cpp into file2.o, you must declare extern double myvar, so that file2.cpp can be compiled. This will please the compiler and leaves the task to linker.

Now that you have compiled these files, you will have object files (translation units) named as (if you are using g++) file1.o and file2.o. Now it is linker's job to link them together and let file2.o to access myvar.

Declaring a variable extern tells the compiler that the actual instance of the variable is somewhere else. If you don't provide it somewhere else, the link stage will fail.

In your second example you don't refer to the extern declared variable at all. You declare a new one in the scope of your main function that shadows the global declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top