Essentially, all you have to do is declare the variable once in one code file and declare it in others as extern (making sure NOT to initialize it when you declare it extern, or some compilers will ignore the extern keyword, giving compiler errors to you.)
The easiest way to do this is to use a macro in the header file, for example:
#pragma once #ifdef __MAIN__ #define __EXTERN(type, name, value) type name = value #else #define __EXTERN(type, name, value) extern type name; #endif
and then declare the variables in the same header file, for example:
__EXTERN(volatile int, MyVolatileInteger, 0);
from any ONE file in the project, include the header file, for example:
#define __MAIN__ #include "Globals.h"
from everything else, just turn it on as usual, for example:
#include "Globals.h"
Presto, everything is ready. Variables are declared only once and are initialized in a string. This is very convenient, and it eliminates the need to declare everything twice.
BrainSlugs83
source share