- Linkage (software)
In programming languages, particularly
C++ , linkage describes how symbols are represented in an executable orobject 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 anyconst
global as file scope unless it is explicitly declaredextern
, unlike C in whichextern
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 inmain()
inside demo1.c refers tofoo
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 thatstatic
has several distinct meanings in C/C++; 'internal linkage' is only one of them. Please refer to other materials for other meanings ofstatic
).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.