Sharing variables between files of code in C - extern
Archive - Originally posted on "The Horse's Mouth" - 2010-01-14 23:06:52 - Graham EllisIn C, If you want to share a variable between a number of functions, you can declare it at the top of your file, outside the functions. It's then an element that's held in memory all the time and available to all your functions. Since your functions are separate elements which are joined together at load time (when you make up the executable file), the variable you're sharing also has to be know to the loaded - it becomes one of the names (symbols) that it needs to load / allocate a position to, just like each of the functions is.
Should you use the same name (i.e. same named variable) in multiple source files, you will be sharing that variable across between them (In other languages, this is often called - a poor name - a global.
With compilers such as gcc (The Gnu Compiler Collection), the occurrence of the same named and shared variable in multiple source files doesn't present the loader with a problem, but with some other compilers it does - that's because the loader has two definitions of the same variable. It's the same problem that you see when you get "double definition" messages from the loaded when a function appears in two different source files. And the solution is to add the keyword extern in front of the declaration in all but one of the files. Thus:
int childcount;
in the one set of source where you want the variable to be located and:
extern int childcount;
in every other set of source where you want in to be used (but not actually located) - the word "extern" is short for external which meand that the sybol is known about, but external to the blocks of code in the current object file.
That's good ... it works well ... but if you're coding a major application, you need to manage it - otherwise you can have a major administration job as you maintain and upgrade your program.
The scheme that's usually followed is to place all the variable that you want to share between a suite of source files into a single header file, and then to use the C preprocessor to prepend each declaration with the word extern unless it's the single case where you want the variables to be physically located in memory.
Here's a sample header file declaring shared variables across an application's C source files:
#ifdef GLOBALS
#define EXTERN
#else
#define EXTERN extern
#endif
EXTERN int nbeasts;
EXTERN float hippos;
And in one (and ONLY one of your source files, you'll add the line:
#define GLOBALS 1
before your
#include "myglobals.h"
or whatever (better) name you have chosen for the file.
There's a complete example from the C Course I have just completed on our web site ... the included file with its preprocessor headers is [here], the file of functions with which the variables are loaded is [here], and the sample file (which could be one of many) that uses those variables is [here].
An element of caution. You should use parameters to pass variables around between your functions most of the time. Shared variables as described in this article do have their place - typically when you're using data that's referenced by a whole series of closeknit routines - but I do not recommend them as the normal way of transferring data back and forth!