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
@@ -26,6 +26,7 @@ Here are the **`/Zc`** compiler options:
26
26
|[`/Zc:auto`](zc-auto-deduce-variable-type.md)| Enforce the new Standard C++ meaning for **`auto`** (on by default). |
27
27
|[`/Zc:char8_t`](zc-char8-t.md)| Enable or disable C++20 native `u8` literal support as `const char8_t` (off by default, except under **`/std:c++20`**). |
28
28
|[`/Zc:__cplusplus`](zc-cplusplus.md)| Enable the `__cplusplus` macro to report the supported standard (off by default). |
29
+
|[`/Zc:externC`](zc-externc.md)| Enforce Standard C++ rules for `extern "C"` functions (implied by **`/permissive-`**). |
29
30
|[`/Zc:externConstexpr`](zc-externconstexpr.md)| Enable external linkage for **`constexpr`** variables (off by default). |
30
31
|[`/Zc:forScope`](zc-forscope-force-conformance-in-for-loop-scope.md)| Enforce Standard C++ **`for`** scoping rules (on by default). |
31
32
|[`/Zc:hiddenFriend`](zc-hiddenfriend.md)| Enforce Standard C++ hidden friend rules (implied by **`/permissive-`**) |
# `/Zc:externC` (Use Standard C++ `extern "C"` rules)
10
+
11
+
The **`/Zc:externC`** compiler option tells the compiler to conform to the C++ standard and enforce consistent parameter declarations for functions declared as `extern "C"`.
12
+
13
+
## Syntax
14
+
15
+
> **`/Zc:externC`**\
16
+
> **`/Zc:externC-`**
17
+
18
+
## Remarks
19
+
20
+
The **`/Zc:externC`** compiler option checks the definitions of functions declared by using `extern "C"`.
21
+
22
+
The **`/Zc:externC`** option is available starting in Visual Studio 2019 version 16.3. It's off when the [`/permissive-`](permissive-standards-conformance.md) option isn't set. In earlier versions of Visual Studio, and by default or if **`/Zc:externC-`** is specified, Visual Studio is permissive about matching declarations of `extern "C"` functions. The **`/permissive-`** option enables **`/Zc:externC`**, so it's on by default in projects that use **`/std:c++20`** or **`/std:c++latest`**. The **`/Zc:externC`** option must come after a **`/permissive-`** option on the command line.
23
+
24
+
Mismatched `extern "C"` declarations can cause compiler errors [C2116](../../error-messages/compiler-errors-1/compiler-error-c2116.md) and [C2733](../../error-messages/compiler-errors-2/compiler-error-c2733.md). In C++ code, an error can occur if you declare an `extern "C"` function more than once and use different parameter types, even if the types have the same definitions. The **`/Zc:externC-`** option relaxes this check, and doesn't produce these errors.
25
+
26
+
### To set this compiler option in Visual Studio
27
+
28
+
1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md).
The **`extern`** keyword may be applied to a global variable, function, or template declaration. It specifies that the symbol has *external linkage*. For background information on linkage and why the use of global variables is discouraged, see [Translation units and linkage](program-and-linkage-cpp.md).
13
13
@@ -17,11 +17,11 @@ The **`extern`** keyword has four meanings depending on the context:
17
17
18
18
- In a **`const`** variable declaration, it specifies that the variable has external linkage. The **`extern`** must be applied to all declarations in all files. (Global **`const`** variables have internal linkage by default.)
19
19
20
-
-**extern "C"** specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block.
20
+
-**`extern "C"`** specifies that the function is defined elsewhere and uses the C-language calling convention. The `extern "C"` modifier may also be applied to multiple function declarations in a block.
21
21
22
22
- In a template declaration, **`extern`** specifies that the template has already been instantiated elsewhere. **`extern`** tells the compiler it can reuse the other instantiation, rather than create a new one at the current location. For more information about this use of **`extern`**, see [Explicit instantiation](explicit-instantiation.md).
23
23
24
-
## extern linkage for non-const globals
24
+
## `extern` linkage for non-`const` globals
25
25
26
26
When the linker sees **`extern`** before a global variable declaration, it looks for the definition in another translation unit. Declarations of non-**`const`** variables at global scope are external by default. Only apply **`extern`** to the declarations that don't provide the definition.
27
27
@@ -40,7 +40,7 @@ int i = 43; // LNK2005! 'i' already has a definition.
40
40
externint i = 43; // same error (extern is ignored on definitions)
41
41
```
42
42
43
-
## extern linkage for const globals
43
+
## `extern` linkage for `const` globals
44
44
45
45
A **`const`** global variable has internal linkage by default. If you want the variable to have external linkage, apply the **`extern`** keyword to the definition, and to all other declarations in other files:
46
46
@@ -52,9 +52,9 @@ extern const int i = 42; // extern const definition
52
52
externconstint i; // declaration only. same as i in FileA
53
53
```
54
54
55
-
## extern constexpr linkage
55
+
## `extern constexpr` linkage
56
56
57
-
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 and later, the [/Zc:externConstexpr](../build/reference/zc-externconstexpr.md) compiler switch enables correct standards-conforming behavior. Eventually the option will become the default. The [/permissive-](../build/reference/permissive-standards-conformance.md) option doesn't enable **/Zc:externConstexpr**.
57
+
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 and later, the [`/Zc:externConstexpr`](../build/reference/zc-externconstexpr.md) compiler switch enables correct standards-conforming behavior. Eventually the option will become the default. The [`/permissive-`](../build/reference/permissive-standards-conformance.md) option doesn't enable **`/Zc:externConstexpr`**.
58
58
59
59
```cpp
60
60
externconstexprint x = 10; //error LNK2005: "int const x" already defined
@@ -66,11 +66,11 @@ If a header file contains a variable declared **`extern`** **`constexpr`**, it m
66
66
externconstexpr__declspec(selectany) int x = 10;
67
67
```
68
68
69
-
## extern "C" and extern "C++" function declarations
69
+
## `extern "C"` and `extern "C++"` function declarations
70
70
71
71
In C++, when used with a string, **`extern`** specifies that the linkage conventions of another language are being used for the declarator(s). C functions and data can be accessed only if they're previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.
72
72
73
-
Microsoft C++ supports the strings **"C"** and **"C++"** in the *string-literal* field. All of the standard include files use the **extern "C"** syntax to allow the run-time library functions to be used in C++ programs.
73
+
Microsoft C++ supports the strings **`"C"`** and **`"C++"`** in the *string-literal* field. All of the standard include files use the **`extern "C"`** syntax to allow the run-time library functions to be used in C++ programs.
If a function has more than one linkage specification, they must agree. It's an error to declare functions as having both C and C++ linkage. Furthermore, if two declarations for a function occur in a program — one with a linkage specification and one without — the declaration with the linkage specification must be first. Any redundant declarations of functions that already have linkage specification are given the linkage specified in the first declaration. For example:
114
+
If a function has more than one linkage specification, they must agree. It's an error to declare functions as having both C and C++ linkage. Furthermore, if two declarations for a function occur in a program, one with a linkage specification and one without, the declaration with the linkage specification must be first. Any redundant declarations of functions that already have linkage specification are given the linkage specified in the first declaration. For example:
115
115
116
116
```cpp
117
117
extern "C" int CFunc1();
@@ -126,6 +126,8 @@ extern "C" int CFunc2(); // Error: not the first declaration of
126
126
// specifier.
127
127
```
128
128
129
+
Starting in Visual Studio 2019, when **`/permissive-`** is specified, the compiler checks that the declarations of `extern "C"` function parameters also match. You can't overload a function declared as `extern "C"`. Starting in Visual Studio 2019 version 16.3, you can override this check by using the [`/Zc:externC-`](../build/reference/zc-externc.md) compiler option after the **`/permissive-`** option.
description: "Learn more about: Compiler Error C2116"
3
3
title: "Compiler Error C2116"
4
-
ms.date: "11/04/2016"
4
+
ms.date: 12/02/2021
5
5
f1_keywords: ["C2116"]
6
6
helpviewer_keywords: ["C2116"]
7
7
ms.assetid: 0089a23f-e6bd-4956-9b58-3bcca09ab5ad
8
8
---
9
9
# Compiler Error C2116
10
10
11
-
function parameter lists differed
11
+
> function parameter lists do not match between declarations
12
12
13
-
The parameters in the default parameter list do not match the formal parameter list.
13
+
The parameter list of a redeclared function doesn't match the parameter list used in an earlier declaration.
14
+
15
+
## Remarks
16
+
17
+
This error can occur if you use different types for the parameters when you redeclare an `extern "C"` function.
18
+
19
+
This error may occur after an upgrade because of conformance changes in Visual Studio 2019. Starting in Visual Studio 2019 version 16.3, the [`/Zc:externC-`](../../build/reference/zc-externc.md) compiler option relaxes this check. The option must come after any [`/permissive-`](../../build/reference/permissive-standards-conformance.md) option on the command line.
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-2/compiler-error-c2733.md
+13-3Lines changed: 13 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,20 @@
1
1
---
2
2
description: "Learn more about: Compiler Error C2733"
3
3
title: "Compiler Error C2733"
4
-
ms.date: "11/04/2016"
4
+
ms.date: 12/02/2021
5
5
f1_keywords: ["C2733"]
6
6
helpviewer_keywords: ["C2733"]
7
7
ms.assetid: 67f83561-c633-407c-a2ee-f9fd16e165bf
8
8
---
9
9
# Compiler Error C2733
10
10
11
-
second C linkage of overloaded function 'function' not allowed
11
+
> you cannot overload a function with 'C' linkage
12
12
13
-
More than one overloaded function is declared with C linkage. When using C linkage, only one form of a specified function can be external. Since overloaded functions have the same undecorated name, they cannot be used with C programs.
13
+
More than one overloaded function is declared with `extern "C"` linkage. When using `"C"` linkage, only one form of a specified function can be external. Since overloaded functions have the same undecorated name, they can't be used with C programs.
14
+
15
+
This error may occur after an upgrade because of conformance changes in Visual Studio 2019. Starting in Visual Studio 2019 version 16.3, the [`/Zc:externC-`](../../build/reference/zc-externc.md) compiler option relaxes this check. The option must come after any [`/permissive-`](../../build/reference/permissive-standards-conformance.md) option on the command line.
0 commit comments