After reading "ruhalde" comment on Jul 28 '11 at 11:56, extern keyword usage
I would like some advice on how to organize many variables, using EXTERN vs STATIC.
In that article, 1) definer and initializer are in a CPP file. 2) declaration with "extern", but no definition, in a separate .h file.
Say I have 100 global variables (but related somehow) in MULTIPLE CPP files, grouping them all in a single .h file is a good way to share them.
But this requires the developer to maintain (these related) variables in MULTIPLE CPP(s) and a header file. I have seen other developer use "static" to group all variables in a single .h file.
e.g. myheader.h
static int var1 = 1;
static int var2 = 2;
...
This is easier to maintain. But as far as I understand, these variables are no longer "global". The "static" keyword reduces the scope to the cpp file that includes this header file.
i.e.
foo1.cpp
#include "myheader.h"
void foo1()
{
var1 +=1;
var2 +=2;
printf(....., var1, var2);
}
foo2.cpp
#include "myheader.h"
void foo2()
{
var1 +=100;
var2 +=200;
printf(....., var1, var2);
}
var1, var2 in foo1 are DIFFERENT variables from var1, var2 in foo2. More seriously, every variable in myheader.h is recreated for every function that includes it.
Question: (assuming I am using "extern" and "static" correctly)
By not using "extern" to create truly global variables, is using "static" for easier code maintenance an acceptable alternative?