Linkage (software)

Linkage (software)

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. [http://www.open-std.org/JTC1/SC22/WG14/www/standards ISO/IEC 9899] . Official C99 documents, including technical corrigenda and a rationale. As of 2007 the latest version of the standard is PDFlink| [http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf ISO/IEC 9899:TC3] |3.61 MiB ]
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

* Application binary interface (ABI)
* Compatibility of C and C++
* Name mangling

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Linkage — generally means the manner or style of being united , and can refer to:*Genetic linkage *Linkage (mechanical) *Linkage (policy) *Linkage (linguistics) *Linkage (software) *Linkage (BitTorrent client)*Linkage is also a commonly used phrase in most …   Wikipedia

  • Linkage disequilibrium — In population genetics, linkage disequilibrium is the non random association of alleles at two or more loci, not necessarily on the same chromosome. It is not the same as linkage, which describes the association of two or more loci on a… …   Wikipedia

  • Linkage (mechanical) — This article is about assemblies of links designed to manage forces and movement. For other uses, see Linkage. Variable stroke engine (Autocar Handbook, Ninth edition) A mechanical linkage is an assembly of bodies connected together to manage… …   Wikipedia

  • Software-Wiederverwendung — Eine Programmbibliothek bezeichnet in der Programmierung eine Sammlung von Programmfunktionen für zusammengehörende Aufgaben. Bibliotheken sind im Unterschied zu Programmen keine eigenständig lauffähigen Einheiten, sondern Hilfsmodule, die… …   Deutsch Wikipedia

  • Record linkage — (RL) refers to the task of finding entries that refer to the same entity across different data sources (e.g., files, books, websites, databases, etc.). Record linkage is an appropriate technique when you have to join data sets that do not already …   Wikipedia

  • Watt's linkage — (also known as the parallel linkage) is a type of mechanical linkage invented by James Watt (19 January 1736 ndash; 25 August 1819) to constrain the movement of a steam engine piston in a straight line. The idea of its genesis using links is… …   Wikipedia

  • Peaucellier-Lipkin linkage — The Peaucellier Lipkin linkage (or Peaucellier Lipkin cell), invented in 1864, was the first linkage capable of transforming rotary motion into perfect straight line motion, and vice versa. It is named after Charles Nicolas Peaucellier (1832… …   Wikipedia

  • Chebyshev linkage — The Chebyshev linkage is a mechanical linkage that converts rotational motion to approximate straight line motion. It was invented by the 19th century mathematician Pafnuty Chebyshev who studied theoretical problems in kinematic mechanisms. One… …   Wikipedia

  • Hoekens linkage — The Hoekens linkage is a four bar mechanism that converts rotational motion to approximate straight line motion. ee also*Straight line mechanism *Peaucellier Lipkin linkage *Four bar linkageExternal links*… …   Wikipedia

  • Fedora (software) — Fedora (or Flexible Extensible Digital Object Repository Architecture) (not to be confused with the Linux distribution named Fedora) is a modular architecture built on the principle that interoperability and extensibility is best achieved by the… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”