static and extern define storage duration and linkage characteristics of objects, #pragma once is a non-standard implementation defined program control.
#pragma once is another way to create a header file inclusion guard.
why can't I access the static variable from within a header in second source file? why does it has [sic] to be extern? |
static specifies the linkage to be internal, only visible in the translation unit (.cpp file) it was defined. extern specifies the linkage to be external, defined in one translation unit and visible to any translation unit that has access to the extern declaration (usually in a header file).
The storage duration, static and extern, acts like the object was defined in the global namespace. Created when the program starts, destroyed when the program ends.
Ok, I'll be honest, the above is really simplified, so could be less than 100% accurate. If you can successfully slog your way through what the C++ standard says about #pragma and extern/static, here's a couple of links to read:
http://en.cppreference.com/w/cpp/preprocessor/impl
http://en.cppreference.com/w/cpp/language/storage_duration