Jump to content

Linkage (software): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
SmackBot (talk | contribs)
m Date maintenance tags and general fixes
No edit summary
Line 7: Line 7:
A symbol's linkage is related to, but distinct from, its [[scope (programming)|scope]]. For instance, a symbol with global scope may have internal linkage, making it accessible within one file only, or external linkage, making it accessible within other files that reference it externally.
A symbol's linkage is related to, but distinct from, its [[scope (programming)|scope]]. For instance, a symbol with global scope may have internal linkage, making it accessible within one file only, or external linkage, making it accessible within other files that reference it externally.


Linkage between languages must be done with some care, as different languages [[adorn (programming)|adorn]] their external symbols differently.
Linkage between languages must be done with some care, as different languages [[Name mangling|adorn]] their external symbols differently.


== Linkage in C ==
== Linkage in C ==

Revision as of 18:17, 15 January 2009

In programming languages, particularly C++, linkage describes how symbols are represented in an executable or object file.

The static keyword is used in C to restrict a function or global variable to file scope (internal linkage). This is also valid in C++, although C++ deprecates this usage in favor of anonymous namespaces (which are not available in C). Also, C++ implicitly treats any const global as file scope unless it is explicitly declared extern, unlike C in which extern is the default. Conversely, inline functions in C are of file scope whereas they have external linkage by default in C++.

A symbol's linkage is related to, but distinct from, its scope. For instance, a symbol with global scope may have internal linkage, making it accessible within one file only, or external linkage, making it accessible within other files that reference it externally.

Linkage between languages must be done with some care, as different languages adorn their external symbols differently.

Linkage in C

Definition of 'linkage' quoted from ISO/IEC 9899:TC3 (C99 Standard):

An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage.[1]

The following is a common example of linkage:

 /* file demo1.c */
 
 extern void foo();
 
 int main()
 {
   foo();
   return 0;  
 }
 /* file demo2.c */
 
 void foo()
 {
   ...
 }

Function foo is declared in two files, with its function body defined in demo2.c. Via linkage, foo called in main() inside demo1.c refers to foo in demo2.c. This is an example of external linkage for function.

There are three kinds of linkage: external, internal, and none.

External linkage, as shown in the example, happens over the set of translation units and libraries that constitutes an entire program. In the example above, it occurs over the object files of demo1.c and demo2.c (no explicit libraries in this case). The keyword extern is used to specify this type of linkage.

Internal linkage happens within one translate unit (one source code file). The keyword static is used to specify this type of linkage (Please note that static has several distinct meanings in C/C++; 'internal linkage' is only one of them. Please refer to other materials for other meanings of static).

For identifier with no linkage, each declaration denotes a unique entity. Function parameter has no linkage; local variable declared in a block without extern also has no linkage.

Due to historical issue, the usage of extern is a bit confusing in C. To specify the external linkage, extern should be used in the declaration of a variable or function, except for:

1. variable declaration with storage allocated; a variable declaration with extern will not get the storage allocated.

2. function declaration; a function has external linkage by default.

See also

References

  1. ^ ISO/IEC 9899. Official C99 documents, including technical corrigenda and a rationale. As of 2007 the latest version of the standard is Template:PDFlink