Requirements: Some algorithm libraries are developed in C++, and there is no C# version, how to use C# to call functions in C++?
First, create a new demo.h header file and define an add addition function, the code is as follows:
Create a new demo.cpp file with the following code:
Download the mingw64 tool at:The hyperlink login is visible.
Extract to the D:\mingw64 folder, temporarily add the mingw64/bin folder to the environment variable in the cmd window, and use the following command:
Call the g++ command to compile the source code into a dynamic link library with the following command:
As shown below:
Two files are generated, demo.dll and demo.lib.
Create a new .NET 8 console project
After referencing the CppSharp package, the .csproj project file reads:
Create a new SampleLibrary object to implement the ILibrary interface, the code is as follows:
by .h, . lib file generates a .cs file that can be called by C# from a function in C++, the code is as follows:
As shown below:
Try calling the add method inside demo.dll, as shown in the image below:
Full Code:
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
Reference:
The hyperlink login is visible.
The hyperlink login is visible.
|