ArticleslgStudy

computer science

Linkage (software)

Linkage (software) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Linkage (software) rather than just read about it. In short: 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.

Key takeaways

  • Linkage (software) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Linkage (software) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Linkage (software) from memory before moving on to harder problems.

Reference excerpt

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.

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.

The following is a common example of linkage: In demo1.c:

In demo2.c:

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.

Linking C and C++ code

C++ C++ offers the following forms of linkage:

No linkage Names with no linkage are those which may only be reference from the exact scope in which they are declared. For example, local variables, function parameters, and local classes.

Internal linkage

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.

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.

External linkage

Names with external linkage are those which may be referenced from any translation unit across the entire program.

Module linkage 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.

Language linkage 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.

The main function must have external linkage, and cannot have static or inline specifiers, however since C++26 it may have the extern "C++" specifier. This is in particular used for module implementation units to test internal entities.

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:

Then, in C++:

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). Language linkage for templates may not necessarily work due to implementation-specific language linking rules, but it is tolerated by the standard as of C++29.

D 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)

Rust In Rust, extern "C" is used to mark C linkage, and is always unsafe, as Rust cannot guarantee the safety of foreign functions.

Meanwhile, to expose Rust functions to C:

Rust offers the following external blocks:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Linkage (software)

Start with the simplest possible case. Write down what Linkage (software) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Linkage (software) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Linkage (software) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Linkage (software)

In research
Linkage (software) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Linkage (software) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Linkage (software) is common in secondary-school and first-year university syllabi. It links to neighbouring topics C (programming language), Compilers, Programming language implementation, so understanding it makes those chapters shorter.
In everyday life
Look for Linkage (software) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Linkage (software)” →

How to study Linkage (software) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Linkage (software) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Linkage (software) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Linkage (software) in simple terms?

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.

Why does Linkage (software) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Linkage (software)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Linkage (software).

Tags

  • C (programming language)
  • Compilers
  • Programming language implementation

Keep exploring