You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/program-and-linkage-cpp.md
+17-3Lines changed: 17 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,15 +21,29 @@ In a C++ program, each global symbol can be defined only once, even if the progr
21
21
22
22
## Linkage vs. scope
23
23
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.
25
25
26
26
## External vs. internal linkage
27
27
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
+
externconstint value = 42;
40
+
```
41
+
42
+
See [extern](using-extern-to-specify-linkage.md) for more information.
29
43
30
44
## Extern constexpr linkage
31
45
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.
33
47
34
48
```cpp
35
49
externconstexprint x = 10; //error LNK2005: "int const x" already defined
0 commit comments