Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't extern link to a static variable?

Tags:

c

static

extern

Why does extern int n not compile when n is declared (in a different file) static int n, but works when declared int n? (Both of these declarations were at file scope.)

Basically, why is int n in file scope not the same as static int n in the same scope? Is it only in relation to extern? If so, what about extern am I missing?

like image 276
Jared Pochtar Avatar asked May 15 '10 21:05

Jared Pochtar


People also ask

Can you extern a static variable?

3.1. Static variables in C have the following two properties: They cannot be accessed from any other file. Thus, prefixes “ extern ” and “ static ” cannot be used in the same declaration. They maintain their value throughout the execution of the program independently of the scope in which they are defined.

Can static functions extern?

You cannot use extern and static together they are mutually exclusive. You need to use only extern if you need External Linkage.

Can you declare a variable is both static and extern?

It cannot be accessed from other module, even if they declare extern int i . People are using the keyword static (in this context) to keep i localize. Hence having i both declared as being defined somewhere else, AND defined as static within the module seems like an error.

Can we use @value for static variables?

However, when we try to apply it to a static field, we'll find that it will still be null: @Value("${name}") private static String NAME_NULL; That's because Spring doesn't support @Value on static fields.


1 Answers

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:

  1. 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.
  2. extern int blah; — blah must be defined elsewhere and is referenced from this file.
  3. 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. (*)
  4. 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.

like image 80
bmargulies Avatar answered Sep 23 '22 19:09

bmargulies