The Problem:
Global variables have External linkage, i.e: They are visible throughout the program.
if 2 different files have variable with same symbol name then it breaks the ODR(One Definition Rule).
The One Definition Rule (ODR), as the name suggests, requires that an object with external linkage, a non-inline function, a class, an enumeration or a template shall have exactly one definition in the program.
The Solution:
Your global variables in the files should have Internal Linkage to avoid violation of ODR.
There are two ways to achieve this:
Make your global variables in file static
Or
Use Anonymous/Unnamed namespace.
Good Read:
Anonymous/Unnamed namespace
Speaking Standardese: the One Definition Rule
For Standerdese Fans:
C++11 3.2 One Definition Rule [basic.def.odr]:
No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.
Para 3:
Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.
C++11 7.3.1.1 Unnamed namespaces [namespace.unnamed]
1/ An unnamed-namespace-definition behaves as if it were replaced by
inlineoptnamespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }
where inline appears if and only if it appears in the unnamed-namespace-definition, all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.94
Note: C++03 deprecated usage of static
for declaring objects in namespace scope but this deprecation was removed in C++11.