In programming languages, particularly the compiled ones like
C,
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, and
D, linkage describes how names can or can not refer to the same entity throughout the whole program or one single
translation unit.
The
static
keyword is used in C to restrict the visibility of a function or variable to its translation unit. This is also valid in C++. (C++ 98/03 deprecated this usage in favor of
anonymous namespace
In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
Namespaces ...
s, but is no longer deprecated in C++ 11.) Also, C++ implicitly treats any
const
namespace-scope variable as having internal linkage unless it is explicitly declared
extern
, unlike C.
A name's linkage is related to, but distinct from, its
scope
Scope or scopes may refer to:
People with the surname
* Jamie Scope (born 1986), English footballer
* John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution
Arts, media, and entertainment
* Cinem ...
. The scope of a name is the part of a translation unit where it is visible. For instance, a name with global scope (which is the same as file-scope in C and the same as the global namespace-scope in C++) is visible in any part of the file. Its scope will end at the end of the translation unit, whether or not that name has been given external or internal linkage.
If the name has external linkage, the entity that name denotes may be referred to from another translation unit using a distinct declaration for that same name, and from other scopes within the same translation unit using distinct declarations. Were the name given internal linkage, such a declaration would denote a distinct entity, although using the same name, but its entity could be referred to by distinct declarations within the same translation unit. A name that has no linkage at all cannot be referred to from declarations in different scopes, not even from within the same translation unit. Examples of such names are parameters of functions and local variables. The details differ between C (where only objects and functions - but not types - have linkage) and C++ and between this simplified overview.
Linkage between languages must be done with some care, as different languages
adorn their external symbols differently.
A common idiom uses
extern "C"
The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to thi ...
to link C++ and C code.
Linkage in C
Definition of 'linkage' quoted from ISO/IEC 9899:TC3 (C99 Standard). C uses the term "identifier" where this article uses "name" (the latter of which is what C++ uses to formalize linkage):
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.[ISO/IEC 9899](_blank)
Official C99 documents, including technical corrigenda and a rationale. As of 2007 the latest version of the standard is
The following is a common example of linkage:
/* file demo1.c */
/* extern */ void foo(void); /* extern optional - it's the default */
int main(void)
/* file demo2.c */
void foo(void)
{
...
}
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 a function.
See also
*
Application binary interface
In computer software, an application binary interface (ABI) is an interface between two binary program modules. Often, one of these modules is a library or operating system facility, and the other is a program that is being run by a user.
An ' ...
(ABI)
*
Compatibility of C and C++
The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to thi ...
*
Linker (computing)
In computing, a linker or link editor is a computer system program that takes one or more object files (generated by a compiler or an assembler) and combines them into a single executable file, library file, or another "object" file.
A simpler ...
*
Name mangling
In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
It provides a way of e ...
References
C (programming language)
Compilers
Programming language implementation