The whole and entire purpose of static
is to declare that a variable is private to the source file that it is declared in. Thus, it is doing precisely its job in preventing a connection from an extern.
Keep in mind that there are four flavors of file-scope variable definition:
int blah = 0;
— blah is defined in this file and accessible from other files. Definitions in other files are duplicates and will lead to errors.
extern int blah;
— blah must be defined elsewhere and is referenced from this file.
int blah;
— This is the moral equivalent of FORTRAN COMMON
. You can have any number of these in files, and they are all resolved by the linker to one shared int
. (*)
static int blah;
(optionally with an initializer) — This is static. It is completely private to this file. It is not visible to externs in other files, and you can have many different files that all declare static TYPE blah;
, and they are all different.
For the purists in the audience: 'file' = compilation unit.
Note that static inside functions (not at file scope) are even more tightly scoped: if two functions declare static int bleh = 0;
even in the same file, they are unrelated.
(*): for those of you not familiar: in the usual pattern, one compilation unit has to define a global variable, and others can reference it. It 'lives' in that compilation unit. In case (3), above, no file (or all the files) defines it. If two files say int blah = 0;
, the linker will complain of multiple definitions. If two files say int blah;
the linker cheerfully creates a single global int
and causes all the code to refer to it.