
Keith B. answered 07/28/19
Software Engineer and Math Geek
Think of extern as meaning, "defined elsewhere, but not in this file."
As you learn more of the language and your projects get larger, maintaining your code in a single file becomes impractical, so using multiple files becomes the norm (think one class per file, a .cpp and a .h per). Tools such as make and cmake help to maintain these large projects -- they can track changes and only build the files that have been touched, reducing build time. Trust me -- some of the projects I work on have a full build time, meaning recompile everything can take hours, and often time are scheduled to be done overnight.
In these cases, if you want a variable to be global, accessible everywhere, to all files, you can only declare it one file. To everyone else who needs it, it would be declared extern. One good practice is to put the extern declaration in the .h file of the .cpp file where it is declared/instantiated.
To the files that need to use it, the extern acts in the same manner as a prototype -- it tells the compiler "here is the definition of this variable being used, but don't set up storage space for it here, that is managed elsewhere" -- your "declaration without definition." One caveat -- it must be actually declared somewhere.
In the build process, files first get compiled (transformed into an object file), then linked. It is during the link process that all routines and variables must be resolved. If you get an error of "unresolved external" than you most likely have an extern reference without an actual definition.