Skip to content

Commit 75f76a2

Browse files
author
mikeblome
committed
updates to content on linkage
1 parent c51a84e commit 75f76a2

9 files changed

+241
-491
lines changed

docs/cpp/TOC.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@
4949
#### [Attributes](attributes2.md)
5050
### [Scope (Visual C++)](scope-visual-cpp.md)
5151
### [Program and Linkage](program-and-linkage-cpp.md)
52-
#### [Types of Linkage](types-of-linkage.md)
53-
#### [Linkage in Names with File Scope](linkage-in-names-with-file-scope.md)
54-
#### [Linkage in Names with Class Scope](linkage-in-names-with-class-scope.md)
55-
#### [Linkage in Names with Block Scope](linkage-in-names-with-block-scope.md)
56-
#### [Names with No Linkage](names-with-no-linkage.md)
57-
#### [Using extern to Specify Linkage](using-extern-to-specify-linkage.md)
52+
#### [extern](using-extern-to-specify-linkage.md)
5853
### [Startup and Termination](startup-and-termination-cpp.md)
5954
#### [main: Program Startup](main-program-startup.md)
6055
##### [Using wmain Instead of main](using-wmain-instead-of-main.md)

docs/cpp/linkage-in-names-with-block-scope.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

docs/cpp/linkage-in-names-with-class-scope.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

docs/cpp/linkage-in-names-with-file-scope.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

docs/cpp/names-with-no-linkage.md

Lines changed: 0 additions & 106 deletions
This file was deleted.

docs/cpp/program-and-linkage-cpp.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,29 @@ In a C++ program, each global symbol can be defined only once, even if the progr
2121

2222
## Linkage vs. scope
2323

24-
The concept of *linkage* refers to the visibility of global symbols (such as variables, type names and function names) within the program as a whole across translation units. The concept of *scope* refers to symbols that are declared within a block such as a namespace, class, or function body. Such symbols are visible only within the scope in which they are defined; the concept of linkage does not apply to them.
24+
The concept of *linkage* refers to the visibility of global symbols (such as variables, type names and function names) within the program as a whole across translation units. The concept of *scope* refers to symbols that are declared within a block such as a namespace, class, or function body. Such symbols are visible only within the scope in which they are defined; the concept of linkage does not apply to them.
2525

2626
## External vs. internal linkage
2727

28-
Non-const global variables and free functions by default have external linkage; they are visible from any translation unit in the program. You can override this behavior by explicity declaring them as **static** which limits their visiblity to the same translation unit in which they are declared. This meaning of **static** is different than its meaning when applied to local variables. Variables declared as **const** have internal linkage by default.
28+
Non-const global variables and free functions by default have external linkage; they are visible from any translation unit in the program. When a name has internal linkage, the same name may exist in another translation unit, in which case it refers to a different object or a different class. You can force a global name to have internal linkage by explicity declaring it as **static**. This limits their visiblity to the same translation unit in which they are declared. Note that in this context, **static** means something different than when applied to local variables.
29+
30+
The following objects have internal linkage by default:
31+
- const objects
32+
- constexpr objects
33+
- typedefs
34+
- static objects in namespace scope
35+
36+
To give a const object external linkage, declare it as **extern** and assign it a value:
37+
38+
```cpp
39+
extern const int value = 42;
40+
```
41+
42+
See [extern](using-extern-to-specify-linkage.md) for more information.
2943

3044
## Extern constexpr linkage
3145

32-
In earlier versions of Visual Studio, the compiler always gave a constexpr variable internal linkage even when the variable was marked extern. In Visual Studio 2017 version 15.5, a new compiler switch (/Zc:externConstexpr) enables correct standards-conforming behavior. Eventually this will become the default.
46+
In Visual Studio 2017 version 15.3 and earlier, the compiler always gave a constexpr variable internal linkage even when the variable was marked extern. In Visual Studio 2017 version 15.5, a new compiler switch ([/Zc:externConstexpr](../build/reference/zc-externconstexpr.md)) enables correct standards-conforming behavior. Eventually this will become the default.
3347

3448
```cpp
3549
extern constexpr int x = 10; //error LNK2005: "int const x" already defined

0 commit comments

Comments
 (0)