Linkage


Linkage is a property of data objects and functions and is most usually associated with a more common description, the term global. Global objects and data are those that can be accessed from different translation units within the program and the technical term for this is external linkage. In fact there is also another class of linkage called internal linkage which is what allows an object or function to be accessed from different functions within its own translation unit.

The linkage of an identifier is partly determined by the scope at which it is declared. All identifiers which are defined at file scope, (i.e. outside a function) have either internal or external linkage. Identifiers with external linkage can be declared but not defined at block scope, (i.e. inside a function). Identifiers defined at block scope have no linkage.

Functions are always defined at file scope. If their definition includes the storage class qualifier static, they have internal linkage and can only be called from within their own translation unit; otherwise they are global functions having external linkage and can be called from any other translation unit in the program.

A data object defined at file scope has either internal or external linkage just as a function does; when defined at block scope it has no linkage, which means that it cannot be referenced outside the block in which it is defined. Note that, the keyword static has no effect on linkage when applied at block scope.

Declaration rules

if (declared at file scope and declared static)
     linkage is internal

else if ( declared extern or function declared with neither static or extern)
     if (there is a previous declaration visible)
         linkage is the same as in previous declaration
     else
         linkage is external

else if ( a previous file scope declaration exists with neither static or extern )
     linkage is external

else
     no linkage

Linkage is often confused with scope because the two attributes are related. The rules of C allow the same entity to be declared:

Index