Understanding how to use extern variables in C/C++
Start with a visual studio project.
create 5 files, and copy/paste the contents as show below in them.
Alternatively, esnips code package! (thanks esnips!)
externVarDeclaration.h
#ifndef EXTERN_VAR_DECL_H #define EXTERN_VAR_DECL_H extern int i ; // This is like a "function prototype", only // for variables. // The extern declaration says, "this variable doesn't exist yet, // but it WILL exist, and its value is going to be defined // in another file that will be part of this project when it compiles." // (see externVarDefinition.cpp) // So, using an EXTERN is how you share a single global variable // ACROSS MULTIPLE FILES. // Call it a SUPERGLOBAL. TO understand this, think about // functions and function prototypes. // Defining a global function, you kind of expect it to have // "superglobal" status.. // like, if you define a function somewhere, you should be // able to break it apart into some prototypes and #include // those prototypes in ANY FILE that you want // to use those functions. Right? // So, the reason you have prototypes is so that multiple // files can #include the same prototypes (function // DECLARATIONS) then the actual code for the // DEFINITION of the functions is in one place, // a .cpp file (like "function_set.cpp"). This way you don't // have multiple re-definitions of the functions declared in // function_set.h, but you may have multiple re-declarations // (the compiler might see the same prototype several times // in a row, which is fine, as long as it sees the BODY // definition only once.) // So, an extern'd variable is a lot like that. // You basically treat it like a function prototype, where the ///// extern int i ; // part is the DECLARATION (like a function prototype), // and the part in externVarDefinition.cpp: // int i = 500 ; // is like the DEFINITION, or function body. // C++ can only come across the DEFINITION (value giving part) __ONCE__ for any // given variable. // So with externs, you can #include the "extern variable declaration" into as // many files as you like (just like you can #include the function prototypes // into as many different files as you wish), so long as // the "DEFINITION" (the int i = 500 part) only occurs once, in one file. // If you uncomment this line below, you will see // its ILLEGAL. //////////int j = 50 ; ////ILLEGAL // Surprised? Well, very illegal! // Reason: EVEN WITH the #ifndef #include guards // on this file.. because this "externVarDeclaration.h" file is // #included in more than one other file, for some reason the C++ // LINKER (__NOT__ the compiler) will flag it as an error: // "int j" already defined in main.obj. // One day I'd like to be able to fully understand why this is // myself, but for now, "EXTERN solves this problem". #endif //EXTERN_VAR_DECL_H
function_set.h
#ifndef FUNCTION_SET_H #define FUNCTION_SET_H #include <stdio.h> #include "externVarDeclaration.h" void print() ; void print2() ; void print3() ; void changeI() ; #endif //FUNCTION_SET_H
externVarDefinition.cpp
#include "externVarDeclaration.h" int i = 500 ; // HERE we define the VALUE of the extern'd variable i.
function_set.cpp
#include "function_set.h" void print() { printf("func1 %d\n", i); } void print2() { printf("func2 %d\n", i); } void print3() { printf("func3 %d\n", i); } void changeI() { // i is shared across the whole project i = 2000402; }
main.cpp
#include <stdio.h> #include "externVarDeclaration.h" #include "function_set.h" int main() { printf( "The extern'd var is %d\n", i ) ; i = 20 ; print(); i = 333 ; print2(); changeI(); printf("My oh my! the extern'd var has changed to %d\n", i ) ; }
15 Comments
Thanks a lot for your useful codes.
Regards,
Meysam
Very Thanx for ur posting
Thank you for this article, now I understand it. :)
Take a look at this :
http://www.cplusplus.com/forum/general/13759/
You’ll understand why the guards are not always working.
Regards,
Kosta
I was wondering if I can share a variable value between tow modules.
nice explanation of EXTERN. tenks..
Thank you for nice explanation of externs
Asante sana kwa maelezo yako mazuri mno, ubarikiwe
nice :)
This is a very good explanation of how to use externs. Thankyou very much for taking the time to post a nice useful explanation with such a thorough working example. This really helped me out!
I just want to say thank you for explaining one very good use of ‘extern’.
It was valuable as a quick fix for global variables depended on by multiple files. Others told me that globals are bad in large projects, but this turned out to be a real solution. Thanks again.
good!
Just a friendly suggestion. It can be hard for some people to read dark-grey on a black background.
@nom nom cookies Your email address was hilarious.
I’ve wanted to change the theme, but it would mean revising all my posts..
thank you very much, great explained