In this question I asked why
//foo.h
namespace foo{
int bar;
}
gave me a linker error when I include foo.h in multiple files. Turns out I need extern int bar; to prevent the error. Why do I need extern? I don’t want to type extern before every variable in every namespace that I want access to in multiple translation units. Why doesn’t int bar; do what I expected it to? Why does the C++ Standards Committee insist on making me type extern everywhere?
Extern says that the storage declaration for the variable will be made elsewhere. The linker then goes and looks for the symbol in another file at link time. Without extern you are telling the compiler, allocate some space for a global. If you do this in more than one file the linker will see multiple instances of the same symbol.