Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
description: "Learn more about: Exporting C Functions for Use in C or C++ Language Executables"
title: "Exporting C Functions for Use in C or C++ Language Executables"
ms.date: "11/04/2016"
description: "Learn more about: Exporting C functions for use in C or C++ language executables"
title: "Export C functions for use in C or C++ language executables"
ms.date: 05/24/2022
helpviewer_keywords: ["functions [C], exporting", "functions [C], C or C++ executables and", "__cplusplus macro", "exporting DLLs [C++], C functions in C++ executables", "exporting functions [C++], C functions in C++ executables"]
ms.assetid: b51d6e5e-37cf-4c1c-b0bf-fcf188c82f00
---
# Exporting C Functions for Use in C or C++ Language Executables
# Export C functions for use in C or C++ language executables

If you have functions in a DLL written in C that you want to access from a C language or C++ language module, you should use the **__cplusplus** preprocessor macro to determine which language is being compiled, and then declare these functions with C linkage if being used from a C++ language module. If you use this technique and provide header files for your DLL, these functions can be used by C and C++ users with no change.
If you have functions in a DLL written in C, you can use a preprocessor macro to make them easy to access from both C language and C++ language code. The **`__cplusplus`** preprocessor macro indicates which language is being compiled. You may use it to declare the functions with C linkage when called from C++ language code. If you use this technique and provide header files for your DLL, these functions can be used by C and C++ users with no change.

The following code shows a header file that can be used by C and C++ client applications:
The following code shows a header file that both C and C++ client applications can use:

```h
// MyCFuncs.h
Expand All @@ -26,7 +26,7 @@ __declspec( dllimport ) void AnotherCFunc();
#endif
```

If you need to link C functions to your C++ executable and the function declaration header files have not used the above technique, in the C++ source file, do the following to prevent the compiler from decorating the C function names:
Sometimes you may need to link C functions to your C++ executable, but the function declaration header files haven't used the above technique. You can still call the functions from C++. In the C++ source file, wrap the `#include` directive to prevent the compiler from decorating the C function names:

```cpp
extern "C" {
Expand All @@ -36,23 +36,23 @@ extern "C" {

## What do you want to do?

- [Export from a DLL using .def files](exporting-from-a-dll-using-def-files.md)
- [Export from a DLL using `.def` files](exporting-from-a-dll-using-def-files.md)

- [Export from a DLL using __declspec(dllexport)](exporting-from-a-dll-using-declspec-dllexport.md)
- [Export from a DLL using `__declspec(dllexport)`](exporting-from-a-dll-using-declspec-dllexport.md)

- [Export and import using AFX_EXT_CLASS](exporting-and-importing-using-afx-ext-class.md)
- [Export and import using `AFX_EXT_CLASS`](exporting-and-importing-using-afx-ext-class.md)

- [Determine which exporting method to use](determining-which-exporting-method-to-use.md)

- [Import into an application using __declspec(dllimport)](importing-into-an-application-using-declspec-dllimport.md)
- [Import into an application using `__declspec(dllimport)`](importing-into-an-application-using-declspec-dllimport.md)

- [Initialize a DLL](run-time-library-behavior.md#initializing-a-dll)

## What do you want to know more about?

- [Decorated names](reference/decorated-names.md)

- [Using extern to Specify Linkage](../cpp/extern-cpp.md)
- [Using `extern` to specify linkage](../cpp/extern-cpp.md)

## See also

Expand Down
4 changes: 1 addition & 3 deletions docs/build/reference/showincludes-list-include-files.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Learn more about: /showIncludes (List include files)"
title: "/showIncludes (List include files)"
ms.date: 04/15/2021
ms.date: 05/24/2022
f1_keywords: ["VC.Project.VCCLWCECompilerTool.ShowIncludes", "VC.Project.VCCLCompilerTool.ShowIncludes", "/showincludes"]
helpviewer_keywords: ["include files", "/showIncludes compiler option [C++]", "include files, displaying in compilation", "-showIncludes compiler option [C++]", "showIncludes compiler option [C++]"]
---
Expand Down Expand Up @@ -30,8 +30,6 @@ Note: including file: d:\temp\2.h

In this case, *`2.h`* was included from within *`1.h`*, causing the indentation.

The **`/showIncludes`** option emits to `stderr`, not `stdout`.

### To set this compiler option in the Visual Studio development environment

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).
Expand Down
14 changes: 7 additions & 7 deletions docs/code-quality/c26495.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
---
title: C26495
ms.date: 03/22/2018
ms.date: 05/23/2022
ms.topic: reference
f1_keywords: ["C26495"]
helpviewer_keywords: ["C26495"]
description: CppCoreCheck rule that enforces C++ Core Guidelines Type.6
---
# C26495 MEMBER_UNINIT

Variable '%variable%' is uninitialized. Always initialize a member variable (type.6).
> Variable '*identifier*' is uninitialized. Always initialize a member variable (type.6).

## See also
## Remarks

C++ Core Guidelines [Type.6](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#SS-type) and [C.48](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers)
A member variable isn't initialized by a constructor or by an initializer. Make sure all variables are initialized by the end of construction. For more information, see C++ Core Guidelines [Type.6](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#SS-type) and [C.48](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers).

## Example

```cpp
struct MyStruct
{
int value;
MyStruct() {}; // C26495, MyStruct::value is uninitialized
MyStruct() {} // C26495, MyStruct::value is uninitialized
};
```

To fix the warning, add in-class initializers to all of the member variables. See the above linked C++ Core Guidelines pages for additional information.
To fix the warning, add in-class initialization to all of the member variables:

```cpp
struct MyStruct
{
int value{};
MyStruct() {}; // no warning, MyStruct::value is set via default member initialization
MyStruct() {} // no warning, MyStruct::value is set via default member initialization
};
```
28 changes: 13 additions & 15 deletions docs/cpp/dllexport-dllimport.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
---
description: "Learn more about: dllexport, dllimport"
title: "dllexport, dllimport"
ms.date: "11/04/2016"
ms.date: 05/24/2022
f1_keywords: ["dllimport_cpp", "dllexport_cpp"]
helpviewer_keywords: ["dllexport __declspec keyword", "__declspec keyword [C++], dllexport", "dllimport __declspec keyword", "__declspec keyword [C++], dllimport"]
ms.assetid: ff95b645-ef55-4e72-b848-df44657b3208
---
# dllexport, dllimport
# `dllexport`, `dllimport`

**Microsoft Specific**

The **`dllexport`** and **`dllimport`** storage-class attributes are Microsoft-specific extensions to the C and C++ languages. You can use them to export and import functions, data, and objects to or from a DLL.

## Syntax

```
__declspec( dllimport ) declarator
__declspec( dllexport ) declarator
```
> **`__declspec( dllimport )`** *`declarator`*\
> **`__declspec( dllexport )`** *`declarator`*

## Remarks

These attributes explicitly define the DLL's interface to its client, which can be the executable file or another DLL. Declaring functions as **`dllexport`** eliminates the need for a module-definition (.def) file, at least with respect to the specification of exported functions. The **`dllexport`** attribute replaces the **__export** keyword.
These attributes explicitly define the DLL's interface to its client, which can be the executable file or another DLL. Declaring functions as **`dllexport`** eliminates the need for a module-definition (`.def`) file, at least with respect to the specification of exported functions. The **`dllexport`** attribute replaces the **`__export`** keyword.

If a class is marked declspec(dllexport), any specializations of class templates in the class hierarchy are implicitly marked as declspec(dllexport). This means that class templates are explicitly instantiated and the class's members must be defined.
If a class is marked `__declspec(dllexport)`, any specializations of class templates in the class hierarchy are implicitly marked as `__declspec(dllexport)`. It means that class templates are explicitly instantiated and the class's members must be defined.

**`dllexport`** of a function exposes the function with its decorated name. For C++ functions, this includes name mangling. For C functions or functions that are declared as `extern "C"`, this includes platform-specific decoration that's based on the calling convention. For information on name decoration in C/C++ code, see [Decorated Names](../build/reference/decorated-names.md). No name decoration is applied to exported C functions or C++ `extern "C"` functions using the **`__cdecl`** calling convention.
**`dllexport`** of a function exposes the function with its decorated name, sometimes known as "name mangling". For C++ functions, the decorated name includes extra characters that encode type and parameter information. C functions or functions that are declared as `extern "C"` include platform-specific decoration that's based on the calling convention. No name decoration is applied to exported C functions or C++ `extern "C"` functions that use the **`__cdecl`** calling convention. For more information on name decoration in C/C++ code, see [Decorated names](../build/reference/decorated-names.md).

To export an undecorated name, you can link by using a Module Definition (.def) file that defines the undecorated name in an EXPORTS section. For more information, see [EXPORTS](../build/reference/exports.md). Another way to export an undecorated name is to use a `#pragma comment(linker, "/export:alias=decorated_name")` directive in the source code.
To export an undecorated name, you can link by using a Module Definition (`.def`) file that defines the undecorated name in an `EXPORTS` section. For more information, see [`EXPORTS`](../build/reference/exports.md). Another way to export an undecorated name is to use a `#pragma comment(linker, "/export:alias=decorated_name")` directive in the source code.

When you declare **`dllexport`** or **`dllimport`**, you must use [extended attribute syntax](../cpp/declspec.md) and the **`__declspec`** keyword.

Expand All @@ -53,17 +51,17 @@ DllExport int n;

For more information, see:

- [Definitions and Declarations](../cpp/definitions-and-declarations-cpp.md)
- [Definitions and declarations](../cpp/definitions-and-declarations-cpp.md)

- [Defining Inline C++ Functions with dllexport and dllimport](../cpp/defining-inline-cpp-functions-with-dllexport-and-dllimport.md)
- [Defining inline C++ functions with `dllexport` and `dllimport`](../cpp/defining-inline-cpp-functions-with-dllexport-and-dllimport.md)

- [General Rules and Limitations](../cpp/general-rules-and-limitations.md)
- [General rules and limitations](../cpp/general-rules-and-limitations.md)

- [Using dllimport and dllexport in C++ Classes](../cpp/using-dllimport-and-dllexport-in-cpp-classes.md)
- [Using `dllimport` and `dllexport` in C++ classes](../cpp/using-dllimport-and-dllexport-in-cpp-classes.md)

**END Microsoft Specific**

## See also

[__declspec](../cpp/declspec.md)<br/>
[`__declspec`](../cpp/declspec.md)\
[Keywords](../cpp/keywords-cpp.md)
2 changes: 1 addition & 1 deletion docs/cpp/welcome-back-to-cpp-modern-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Macros in C and C++ are tokens that are processed by the preprocessor before com
constexpr int size = 10; // modern C++
```

### Uniform initialization
## Uniform initialization

In modern C++, you can use brace initialization for any type. This form of initialization is especially convenient when initializing arrays, vectors, or other containers. In the following example, `v2` is initialized with three instances of `S`. `v3` is initialized with three instances of `S` that are themselves initialized using braces. The compiler infers the type of each element based on the declared type of `v3`.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
---
description: "Learn more about: Compiler Warning (level 1) C4273"
title: "Compiler Warning (level 1) C4273"
ms.date: "11/04/2016"
ms.date: 05/23/2022
f1_keywords: ["C4273"]
helpviewer_keywords: ["C4273"]
ms.assetid: cc18611d-9454-40a4-ad73-69823d5888fb
---
# Compiler Warning (level 1) C4273

'function' : inconsistent DLL linkage
> '*function*' : inconsistent DLL linkage

Two definitions in a file differ in their use of [dllimport](../../cpp/dllexport-dllimport.md).
Two definitions in a file differ in their use of [`dllimport`](../../cpp/dllexport-dllimport.md).

## Examples

The following sample generates C4273.
The following sample generates C4273, and shows how to fix it.

```cpp
// C4273.cpp
Expand All @@ -23,11 +23,16 @@ char __declspec(dllimport) c;
char c; // C4273, delete this line or the line above to resolve
```

The following sample generates C4273.
The following sample generates C4273. To fix it, delete the redeclaration of `printf_s`.

```cpp
// C4273_b.cpp
// compile with: /W1 /clr /c
#include <stdio.h>
extern "C" int printf_s(const char *, ...); // C4273
```

## See also

[`dllexport`, `dllimport`](../../cpp/dllexport-dllimport.md)\
[Export C functions for use in C or C++ language executables](../../build/exporting-c-functions-for-use-in-c-or-cpp-language-executables.md)
10 changes: 5 additions & 5 deletions docs/error-messages/tool-errors/linker-tools-error-lnk1112.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Learn more about: Linker Tools Error LNK1112"
title: "Linker Tools Error LNK1112"
ms.date: "11/04/2016"
ms.date: 05/23/2022
f1_keywords: ["LNK1112"]
helpviewer_keywords: ["LNK1112"]
ms.assetid: 425793d8-37e6-4072-9b6e-c3d4e9c12562
Expand All @@ -12,16 +12,16 @@ ms.assetid: 425793d8-37e6-4072-9b6e-c3d4e9c12562

## Remarks

The object files specified as input were compiled for different computer types.
The object files specified as input were compiled for a different target platform.

For example, if you try to link an object file compiled with **/clr** and an object file compiled with **/clr:pure** (machine type CEE), the linker will generate the error LNK1112. The **/clr:pure** compiler option is deprecated in Visual Studio 2015 and unsupported in Visual Studio 2017.
For example, if you try to link an object file compiled with **`/clr`** and an object file compiled with **`/clr:pure`** (machine type CEE), the linker will generate the error LNK1112. The **`/clr:pure`** compiler option is deprecated in Visual Studio 2015 and unsupported in Visual Studio 2017.

Similarly, if you create one module with the x64 compiler and another module with the x86 compiler, and try to link them, the linker will generate LNK1112.

A possible reason for this error is if you are developing a 64-bit application but have not installed one of the Visual C++ 64-bit compilers. In this case, 64-bit configurations will not be available. To fix this issue, run the installer for Visual Studio and install the missing C++ components.
A possible reason for this error is if you're developing a 64-bit application but haven't installed one of the Visual C++ 64-bit compilers. Or, you're targeting an ARM or ARM64 platform, but you don't have the ARM or ARM64 build tools installed. To fix this issue, run the Visual Studio Installer and install the missing C++ components.

This error can also occur if you change the **Active solution configuration** in the **Configuration Manager** and then try to build the project before you delete the intermediate project files. To resolve this error, select **Rebuild Solution** from the **Build** menu. You can also select **Clean Solution** from the **Build** menu and then build the solution.

## See also

- [Linker Tools Errors and Warnings](../../error-messages/tool-errors/linker-tools-errors-and-warnings.md)
- [Linker tools errors and warnings](../../error-messages/tool-errors/linker-tools-errors-and-warnings.md)
Loading