I would like to know why you can not declare a global with the same name in 2 different .cpp files. My understanding is considering scope, that it should only be visible to that particular .cpp file and no where else but it is obviously complaining. The reason I’m doing this is for commonality in code and that’s it. any ideas?
Edit for Clarity
a.cpp
int g_x;
b.cpp
int g_x;
To make a global variable (or function) only visible to the file it’s declared in,
static, orTo access a global variable (that is not
staticor in an anonymous namespace) that is declared in some other file, useextern.The reason why is the same reason why you can’t have a function with the same name in two different files. It confuses the linker because global variables by default have external linkage.
staticor being in an anonymous namespace gives them internal linkage, which makes them like a “local global variable”.