Skip to content

Commit 59dc7ab

Browse files
author
3836425+corob-msft@users.noreply.github.com
committed
Address issue 3301
1 parent 953a087 commit 59dc7ab

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
description: "Learn more about: Translation units and linkage"
33
title: "Translation units and linkage (C++)"
4-
ms.date: "12/11/2019"
4+
ms.date: 08/12/2021
55
ms.assetid: a6493ba0-24e2-4c89-956e-9da1dea660cb
66
---
77
# Translation units and linkage
88

9-
In a C++ program, a *symbol*, for example a variable or function name, can be declared any number of times within its scope, but it can only be defined once. This rule is the "One Definition Rule" (ODR). A *declaration* introduces (or re-introduces) a name into the program. A *definition* introduces a name. If the name represents a variable, a definition explicitly initializes it. A *function definition* consists of the signature plus the function body. A class definition consists of the class name followed by a block that lists all the class members. (The bodies of member functions may optionally be defined separately in another file.)
9+
In a C++ program, a *symbol*, for example a variable or function name, can be declared any number of times within its scope. However, it can only be defined once. This rule is the "One Definition Rule" (ODR). A *declaration* introduces (or reintroduces) a name into the program, along with enough information to later associate the name with a definition. A *definition* introduces a name and provides all the information needed to create it. If the name represents a variable, a definition explicitly creates storage and initializes it. A *function definition* consists of the signature plus the function body. A class definition consists of the class name followed by a block that lists all the class members. (The bodies of member functions may optionally be defined separately in another file.)
1010

1111
The following example shows some declarations:
1212

@@ -27,35 +27,35 @@ public:
2727
};
2828
```
2929

30-
A program consists of one or more *translation units*. A translation unit consists of an implementation file and all the headers that it includes directly or indirectly. Implementation files typically have a file extension of *cpp* or *cxx*. Header files typically have an extension of *h* or *hpp*. Each translation unit is compiled independently by the compiler. After the compilation is complete, the linker merges the compiled translation units into a single *program*. Violations of the ODR rule typically show up as linker errors. Linker errors occur when the same name has two different definitions in different translation units.
30+
A program consists of one or more *translation units*. A translation unit consists of an implementation file and all the headers that it includes directly or indirectly. Implementation files typically have a file extension of *`.cpp`* or *`.cxx`*. Header files typically have an extension of *`.h`* or *`.hpp`*. Each translation unit is compiled independently by the compiler. After the compilation is complete, the linker merges the compiled translation units into a single *program*. Violations of the ODR rule typically show up as linker errors. Linker errors occur when the same name has two different definitions in different translation units.
3131

32-
In general, the best way to make a variable visible across multiple files is to put it in a header file. Then add an #include directive in every *cpp* file that requires the declaration. By adding *include guards* around the header contents, you ensure that the names it declares are only defined once.
32+
In general, the best way to make a variable visible across multiple files is to put it in a header file. Then add an #include directive in every *`.cpp`* file that requires the declaration. By adding *include guards* around the header contents, you ensure that the names it declares are only defined once.
3333

3434
In C++20, [modules](modules-cpp.md) are introduced as an improved alternative to header files.
3535

36-
In some cases it may be necessary to declare a global variable or class in a *cpp* file. In those cases, you need a way to tell the compiler and linker what kind of *linkage* the name has. The type of linkage specifies whether the name of the object applies just to the one file, or to all files. The concept of linkage applies only to global names. The concept of linkage does not apply to names that are declared within a scope. A scope is specified by a set of enclosing braces such as in function or class definitions.
36+
In some cases, it may be necessary to declare a global variable or class in a *`.cpp`* file. In those cases, you need a way to tell the compiler and linker what kind of *linkage* the name has. The type of linkage specifies whether the name of the object applies just to the one file, or to all files. The concept of linkage applies only to global names. The concept of linkage doesn't apply to names that are declared within a scope. A scope is specified by a set of enclosing braces such as in function or class definitions.
3737

3838
## External vs. internal linkage
3939

40-
A *free function* is a function that is defined at global or namespace scope. Non-const global variables and free functions by default have *external linkage*; they are visible from any translation unit in the program. Therefore, no other global object can have that name. A symbol with *internal linkage* or *no linkage* is visible only within the translation unit in which it is declared. When a name has internal linkage, the same name may exist in another translation unit. Variables declared within class definitions or function bodies have no linkage.
40+
A *free function* is a function that is defined at global or namespace scope. Non-const global variables and free functions by default have *external linkage*; they're visible from any translation unit in the program. No other global object can have that name. A symbol with *internal linkage* or *no linkage* is visible only within the translation unit in which it's declared. When a name has internal linkage, the same name may exist in another translation unit. Variables declared within class definitions or function bodies have no linkage.
4141

42-
You can force a global name to have internal linkage by explicitly declaring it as **`static`**. This limits its visibility to the same translation unit in which it is declared. In this context, **`static`** means something different than when applied to local variables.
42+
You can force a global name to have internal linkage by explicitly declaring it as **`static`**. This keyword limits its visibility to the same translation unit in which it's declared. In this context, **`static`** means something different than when applied to local variables.
4343

4444
The following objects have internal linkage by default:
4545

46-
- const objects
47-
- constexpr objects
48-
- typedefs
49-
- static objects in namespace scope
46+
- **`const`** objects
47+
- **`constexpr`** objects
48+
- **`typedef`** objects
49+
- **`static`** objects in namespace scope
5050

51-
To give a const object external linkage, declare it as **`extern`** and assign it a value:
51+
To give a **`const`** object external linkage, declare it as **`extern`** and assign it a value:
5252

5353
```cpp
5454
extern const int value = 42;
5555
```
5656

57-
See [extern](extern-cpp.md) for more information.
57+
For more information, see [`extern`](extern-cpp.md).
5858

5959
## See also
6060

61-
[Basic Concepts](../cpp/basic-concepts-cpp.md)
61+
[Basic concepts](../cpp/basic-concepts-cpp.md)

0 commit comments

Comments
 (0)