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
title: "Program and Linkage (C++) | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "04/06/2018"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology: ["cpp-language"]
@@ -16,31 +16,24 @@ manager: "ghogen"
16
16
ms.workload: ["cplusplus"]
17
17
---
18
18
# Program and Linkage (C++)
19
-
A translation unit consists of an implementation file (.cpp, .hpp, .cxx, etc) and all the headers that it includes directly or indirectly. A program consists of one or more translation units linked together. The concept of *linkage* refers to the visibility of global symbols (such as variable, 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 when execution enters the block in which they are defined; the concept of linkage does not apply to them.
20
19
21
-
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.
20
+
In a C++ program, each global symbol can be defined only once, even if the program consists of multiple translation units. A translation unit consists of an implementation file (.cpp, .hpp, .cxx, etc) and all the headers that it includes directly or indirectly. A program consists of one or more translation units linked together. The concept of *linkage* refers to the visibility of global symbols (such as variable, 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 when execution enters the block in which they are defined; the concept of linkage does not apply to them.
22
21
23
-
In a C++ program, each global symbol can be defined only once.
22
+
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.
24
23
25
-
Program execution begins in the translation unit that contains the **main** function. (For more information on translation units, see [Phases of Translation](../preprocessor/phases-of-translation.md), in the *Preprocessor Reference*.) For more information about the **main** function, see [Program Startup: the main Function](../cpp/main-program-startup.md).)
26
-
24
+
### Extern constexpr linkage
27
25
28
-
from conformance updates:
29
-
Extern constexpr linkage
30
26
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.
31
27
32
-
C++
33
-
Copy
28
+
```cpp
29
+
externconstexprint x = 10; //error LNK2005: "int const x" already defined
30
+
```
34
31
35
-
extern constexpr int x = 10;
36
-
Output
37
-
Copy
38
-
error LNK2005: "int const x" already defined
39
-
If a header file contains a variable declared extern constexpr, it needs to be marked __declspec(selectany) in order to correctly have its duplicate declarations combined:
40
-
41
-
C++
42
-
Copy
32
+
If a header file contains a variable declared extern constexpr, it needs to be marked **__declspec(selectany)** in order to correctly have its duplicate declarations combined:
0 commit comments