Static variables are local to the compilation unit. A compilation unit is basically a .cpp
file with the contents of the .h
file inserted in place of each #include
directive.
Now, in a compilation unit you can't have two global variables with the same name. This is what's happening in your case: main.cpp
includes file1.h
and file.h
, and each of the two headers defines its own Var1
.
If logically these are two distinct variables, give them different names (or put them in different namespaces).
If these are the same variable, move it into a separate header file, var1.h
, and include var1.h
from both file1.h
and file2.h
, not forgetting the #include guard in var1.h
.