encyclopegia.org

Linkage (software)

In programming, 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 and C++03 deprecated this usage in favor of anonymous namespaces, 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. 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.…

In programming, 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 and C++03 deprecated this usage in favor of anonymous namespaces, 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. 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" to link C++ and C code. // the "extern" qualifier is optional // by default functions have external linkage void foo(); // equivalently: extern void foo(); int main() { foo(); return 0; } In : void foo() { // actual implementation here... } Function foo() is declared in two files, with its function body defined in . Via linkage, foo() called in main() inside refers to foo in . This is an example of external linkage for a function. Names with internal linkage are those which may be referenced from any scope within the same translation unit. With internal linkage, each translation unit receives its own isolated copy of the symbol. A symbol gets internal linkage if declared static at namespace scope. By default, const , constexpr , and typedef objects also have internal linkage. However, using the inline specifier on an const variable at namespace scope gives it external linkage. // static symbols receive internal linkage static int double(int n) { return 2 * n; } // constexpr implicitly grants internal linkage constexpr size_t MAX_ATTEMPTS = 3; // inline constexpr grants external linkage inline constexpr size_t MAX_CONNECTIONS = 100; All entities placed inside an "unnamed namespace" (also known as an "anonymous namespace") are given internal linkage. An unnamed namespace should not be confused for the global namespace. namespace { class Foo; } namespace mylib { namespace { void bar(); } } // A.cpp int i = 1; // Actual declaration and definition of i // B.cpp extern int i; // Declares the existence of i // C.cpp int i = 2; // Error: i is already defined (in A.cpp) extern int i = 3; // Same error Names with module linkage are those which may be shared across different translation units but only if they belong to the same named module. Such symbols may be accessed by any file part of the module itself, but not from code that imports that module, unless it is explicitly declared export . C linkage is denoted with extern "C" , which disables C++ name mangling. It allows C++ programs to link against compiled libraries written purely in C, and allows calling of C++ functions from C. extern "C" code may not expose types or features not present in C (such as classes, templates or exception throwing). C++ linkage is denoted with extern "C++" , and is used to explicitly force C++ linkage conventions. This may be used within extern "C" blocks to force a symbol to have C++ linkage. Additionally, it is used in C++ modules to force something in the module purview to be attached to the global module, allowing symbols with internal linkage from a header to still be attached to the module. export module wikipedia.examples.mylib; export extern "C++" { // Forces the contents in the header to have global module attachment when exported #include } Some compilers may support additional language linkages. For example, GCC supports Java linkage with extern "Java" , which is used when referencing Java code compiled by GCJ and used in C++ code to indicate that a symbol that is consumed originates from Java. For example, from Java: package org.wikipedia.examples; public class Foo { public int add(int a, int b) { return a + b; } } Then, in C++: include // Automatically generated header include using org::wikipedia::examples::Foo; extern "Java" { int addThroughJava(int a, int b) { Foo* foo = new Foo(); return foo->add(a, b); // Note: foo should not be deleted, as it is a Java-managed garbage collected object } } The string that appears after extern is unevaluated, that is, a string literal is expected but it is not evaluated by the compiler. IBM's compilers for z/OS supported extern "COBOL" for COBOL linkage , extern "FORTRAN" for Fortran linkage , and extern "PLI" for PL/I linkage . Additionally, it supported extern "OS" as the calling convention for C++ in IBM Integrated Language Environment (ILE). In D, a block of code that is meant to be declared as originating from another language can be marked extern (...) : extern (C) (for C) extern (C++) (for C++) extern (Objective-C) (for Objective-C) extern (D) In Rust, extern "C" is used to mark C linkage, and is always unsafe , as Rust cannot guarantee the safety of foreign functions. use std::ffi::{CString, c_char}; unsafe extern "C" { fn puts(s: *const c_char) -> i32; } fn main() { let s = CString::new("Hello from Rust!").unwrap(); unsafe { puts(s.as_ptr()); } } Meanwhile, to expose Rust functions to C: [unsafe(no_mangle)] pub extern "C" fn add(a: i32, b: i32) -> i32 { a + b } Rust offers the following external blocks: unsafe extern "Rust" (default calling convention on Rust) unsafe extern "C" (the default C ABI) Other system-specific blocks exist, many of which correspond to C/C++ compiler attributes, for example unsafe extern "stdcall" corresponding to gnu::stdcall . The cxx library, a third-party library offering interoperability between C++ and Rust, offers a unsafe extern "C++" block, for declaring C++ symbols available to Rust. ==References==

References

No reference rows published for this article.

Cite this article

MLA

"Linkage (software)." encyclopegia.org, 2026, https://encyclopegia.org/wiki/linkage-software. Accessed 25 November 2026.

Chicago

"Linkage (software)." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/linkage-software.

APA

encyclopegia.org. (2026). Linkage (software). Retrieved November 25, 2026, from https://encyclopegia.org/wiki/linkage-software

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Ubuntu Software Center

stub · references available

customs software

stub · references available

access software

stub · references available

Free Software Song

stub · references available

software framework

stub · references available

route planning software

stub · references available

data loss prevention software

stub · references available

Sarrus linkage

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1378180052 · 0.5 · snapshot enwiki-001378180052

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.