﻿# Linkage

Linkage is an identifier's property\. It allows the compiler in some cases to create one common entity for several same\-name declarations in different translation units\. Together with the scope, linkage determines from which translation units and their scopes you can access the entity\.

There are 4 types of linkage: _no linkage_, _internal linkage_, _external linkage,_ and _module linkage_\.

## No linkage

The following entities declared in the block scope have _no linkage_:

* Local variables without the _extern_ specifier;
* Local classes and their methods;
* Other identifiers, except function names, such as aliases or enumerations\.

For each name with no linkage the compiler generates a separate object\.

## Internal linkage

Any identifier with _internal linkage_ is accessible throughout the current translation unit\. For several declarations of the same name with internal linkage within one translation unit, the compiler generates a single object\. For identifiers of the same name with _internal linkage_ in different translation units, the compiler creates different objects\.

The following entities declared in a namespace have internal linkage:

* Variables and functions declared _static_;
* Non\-template variables declared _const_ and neither declared _inline_ and _volatile_, nor _extern\. _Additionally, previously such a variable should not be declared to have external linkage;
* Data members of an anonymous union;
* Any names in an unnamed namespace, including those declared _extern_\.

## External linkage

Any identifier with _external linkage _is accessible from other translation units\. For the identifiers of the same name with external linkage, the compiler creates a single object\.

The identifier with _external linkage_ also has a_ language linkage_, through which translation units written in different programming languages link together\.

The following entities declared in a namespace have _external linkage_:

* Functions without the _static_ specifier;
* Variables without the _const_ and _extern specifiers_;
* Variables declared _extern_;
* Enumerations;
* Names of classes and their methods, as well as nested classes and enumerations;
* Functions declared in a class with the _friend_ specifier;
* Templates declared _static_ unless they are function or variable templates\.

The following entities declared in the block scope have _external linkage_:

* Variables declared _extern_;
* Functions\.

## Module linkage

Starting from the C\+\+20 standard, an identifier can have _module linkage_\. Such an identifier is accessible from the translation units belonging to the module in which it is declared\. Accordingly, for several identical declarations of the same name entities with _module linkage_, one object will be created within the same module\.

Identifiers have _module linkage_ when they are attached to a named module and not declared with the _export_ keyword\. Code example:

```cpp
// cute_module_main.h
module CuteModule:moduleVariable;

int moduleVariable = 0;

// cute_module_calculator.h
export module CuteModule;

import :moduleVariable;

export int GetSquaredModuleVariable() {
    return ::moduleVariable * ::moduleVariable;
}
```

Here, _moduleVariable _has _module linkage\._