Data objects and functions can be implicitly or explicitly assigned linkage. There are three kinds of linkage:
When more than one declaration of the same object or function is made, linkage is made. The linked declarations can be in the same scope or in different scopes. Externally linked objects are available to any function in any compilation unit used to create the executable file. Internally linked objects are available only to the compilation unit in which the declarations appear.
The concept of linkage and the static
and
extern
keywords are related, but not directly. Using
the extern
keyword in an object's declaration does not
guarantee external linkage. The following rules determine the actual
linkage of an object or function:
auto
or register
storage class has
no linkage.
extern
storage-class specification has linkage the same as any visible
declaration of the same identifier with file scope. If no such
declaration of the object or function is visible, then the object
or function has external linkage.
static
, which must be specified explicitly, cannot
be applied to a block scope function declaration, and results in
internal linkage.
extern
storage class specified, has external
linkage.
static
storage class has internal linkage.
extern
storage-class specification has no linkage.
Identifiers other than data objects and functions have no linkage. An identifier declared as a function parameter also has no linkage.
The following examples show declarations with different linkages:
extern int x; /* External linkage */ static int y; /* Internal linkage */ register int z; /* Illegal storage-class declaration */ main () /* Functions default to external linkage */ { int w; /* No linkage */ extern int x; /* External linkage */ extern int y; /* Internal linkage */ static int a; /* No linkage */ } void func1 (int arg1) /* arg1 has no linkage */ { }
In DEC C, a message is issued if the same object is declared with both internal and external linkage.