Home Segments Index Top Previous Next

702: Mainline

Next, you need to learn how to distribute global-variable declarations and global-variable definitions among header files and source-code files.

As explained in Segment 129, when you define a global variable, you tell the compiler to allocate a chunk of memory for the variable. Consider the following definition, for example:

int mode; 

Such a definition, appearing outside of any function definition, tells the C++ compiler to allocate storage for the mode variable.

If you wish to make use of the mode global variable in another file, you must so inform the C++ compiler by inserting a declaration in that other file. When you declare a global variable, you tell the compiler that the global variable is defined in an object file available to the C++ linker when it is time to link together object files into an executable program.