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/definitions-and-declarations-cpp.md
+55-58Lines changed: 55 additions & 58 deletions
Original file line number
Diff line number
Diff line change
@@ -31,63 +31,60 @@ translation.priority.ht:
31
31
- "zh-tw"
32
32
---
33
33
# Definitions and Declarations (C++)
34
-
## Microsoft Specific
35
-
The DLL interface refers to all items (functions and data) that are known to be exported by some program in the system; that is, all items that are declared as **dllimport** or `dllexport`. All declarations included in the DLL interface must specify either the **dllimport** or `dllexport` attribute. However, the definition must specify only the `dllexport` attribute. For example, the following function definition generates a compiler error:
36
-
37
-
```
38
-
__declspec( dllimport ) int func() { // Error; dllimport
39
-
// prohibited on definition.
34
+
## Microsoft Specific
35
+
The DLL interface refers to all items (functions and data) that are known to be exported by some program in the system; that is, all items that are declared as `dllimport` or `dllexport`. All declarations included in the DLL interface must specify either the `dllimport` or `dllexport` attribute. However, the definition must specify only the `dllexport` attribute. For example, the following function definition generates a compiler error:
36
+
37
+
```
38
+
__declspec( dllimport ) int func() { // Error; dllimport
39
+
// prohibited on definition.
40
40
return 1;
41
-
}
42
-
```
43
-
44
-
This code also generates an error:
45
-
46
-
```
47
-
#define DllImport __declspec( dllimport )
48
-
49
-
__declspec( dllimport ) int i = 10; // Error; this is a
50
-
// definition.
51
-
```
52
-
53
-
However, this is correct syntax:
54
-
55
-
```
56
-
__declspec( dllexport ) int i = 10; // Okay--export definition
57
-
```
58
-
59
-
The use of `dllexport` implies a definition, while **dllimport** implies a declaration. You must use the `extern` keyword with `dllexport` to force a declaration; otherwise, a definition is implied. Thus, the following examples are correct:
60
-
61
-
```
62
-
#define DllImport __declspec( dllimport )
63
-
#define DllExport __declspec( dllexport )
64
-
65
-
extern DllImport int k; // These are both correct and imply a
66
-
DllImport int j; // declaration.
67
-
```
68
-
69
-
The following examples clarify the preceding:
70
-
71
-
```
72
-
static __declspec( dllimport ) int l; // Error; not declared extern.
73
-
74
-
void func() {
75
-
static __declspec( dllimport ) int s; // Error; not declared
76
-
// extern.
77
-
__declspec( dllimport ) int m; // Okay; this is a
78
-
// declaration.
79
-
__declspec( dllexport ) int n; // Error; implies external
80
-
// definition in local scope.
81
-
extern __declspec( dllimport ) int i; // Okay; this is a
82
-
// declaration.
83
-
extern __declspec( dllexport ) int k; // Okay; extern implies
84
-
// declaration.
85
-
__declspec( dllexport ) int x = 5; // Error; implies external
86
-
// definition in local scope.
87
-
}
88
-
```
89
-
90
-
**END Microsoft Specific**
91
-
92
-
## See Also
41
+
}
42
+
```
43
+
44
+
This code also generates an error:
45
+
46
+
```
47
+
__declspec( dllimport ) int i = 10; // Error; this is a definition.
48
+
```
49
+
50
+
However, this is correct syntax:
51
+
52
+
```
53
+
__declspec( dllexport ) int i = 10; // Okay--export definition
54
+
```
55
+
56
+
The use of `dllexport` implies a definition, while `dllimport` implies a declaration. You must use the `extern` keyword with `dllexport` to force a declaration; otherwise, a definition is implied. Thus, the following examples are correct:
57
+
58
+
```
59
+
#define DllImport __declspec( dllimport )
60
+
#define DllExport __declspec( dllexport )
61
+
62
+
extern DllImport int k; // These are both correct and imply a
63
+
DllImport int j; // declaration.
64
+
```
65
+
66
+
The following examples clarify the preceding:
67
+
68
+
```
69
+
static __declspec( dllimport ) int l; // Error; not declared extern.
70
+
71
+
void func() {
72
+
static __declspec( dllimport ) int s; // Error; not declared
73
+
// extern.
74
+
__declspec( dllimport ) int m; // Okay; this is a
75
+
// declaration.
76
+
__declspec( dllexport ) int n; // Error; implies external
77
+
// definition in local scope.
78
+
extern __declspec( dllimport ) int i; // Okay; this is a
79
+
// declaration.
80
+
extern __declspec( dllexport ) int k; // Okay; extern implies
81
+
// declaration.
82
+
__declspec( dllexport ) int x = 5; // Error; implies external
0 commit comments