DotNet andd C++ example
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
cmake_minimum_required (VERSION 3.10)
|
||||||
|
|
||||||
|
project (TestExample CXX)
|
||||||
|
|
||||||
|
set (SOURCES
|
||||||
|
src/Arithmetic.cpp
|
||||||
|
src/Entry.cpp)
|
||||||
|
|
||||||
|
set (HEADERS
|
||||||
|
include/Arithmetic.h)
|
||||||
|
|
||||||
|
add_library(TestExample SHARED ${SOURCES} ${HEADERS})
|
||||||
|
include_directories(include)
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef ARITHMETIC_H_
|
||||||
|
#define ARITHMETIC_H_
|
||||||
|
|
||||||
|
class Arithmetic {
|
||||||
|
public:
|
||||||
|
Arithmetic() = default;
|
||||||
|
|
||||||
|
int add(const int, const int);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#include "Arithmetic.h"
|
||||||
|
|
||||||
|
int Arithmetic::add(const int a, const int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include "Arithmetic.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
int add(int, int);
|
||||||
|
|
||||||
|
|
||||||
|
int add(int a, int b) {
|
||||||
|
Arithmetic matics;
|
||||||
|
|
||||||
|
return matics.add(a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace DotnetAndCPP
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var a = 54;
|
||||||
|
var b = 23;
|
||||||
|
|
||||||
|
var result = add(a, b);
|
||||||
|
|
||||||
|
Console.WriteLine($"a: {a} + b: {b} = {result}");
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport("libTestExample.so")]
|
||||||
|
public static extern int add(int a, int b);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
Quick test on how to dip into C/C++ from C# using your own shared C++ libraries
|
||||||
|
|
||||||
|
|
||||||
|
# Building
|
||||||
|
Building and testing is quite quick and only involves several steps. Before proceeding, it should be noted that this quick test targets Linux. This makes a difference because in Linux C++ shared libraries have a *.so file extension compared to Windows having a *.dll file extension. The ``Program.cs`` file is looking for a shared library with an extension ending is *.so. As is this test will not work on Windows without some modifications.
|
||||||
|
|
||||||
|
### Required
|
||||||
|
* Linux
|
||||||
|
* .NET Core 3.1
|
||||||
|
* GCC >= 7 or clang >= 7
|
||||||
|
|
||||||
|
## Building C++ library
|
||||||
|
1. Change into the C++ Libs directory and create a build directory
|
||||||
|
```Bash
|
||||||
|
cd Libs; mkdir build
|
||||||
|
```
|
||||||
|
2. Build the C++ library
|
||||||
|
```Bash
|
||||||
|
cd build; cmake -DCMAKE_BUILD_TYPE=DEBUG ..; make
|
||||||
|
```
|
||||||
|
3. Copy the library to the root of the project
|
||||||
|
```Bash
|
||||||
|
cp libTestExample.so ../../
|
||||||
|
```
|
||||||
|
|
||||||
|
## Building .NET console software
|
||||||
|
1. Ensure that you are in the root of the project. Then build the software
|
||||||
|
```Bash
|
||||||
|
dotnet build
|
||||||
|
```
|
||||||
|
2. Copy the C++ library to where the executable is build
|
||||||
|
```Bash
|
||||||
|
cp libTestExample.so bin/Debug/netcoreapp3.1/
|
||||||
|
```
|
||||||
|
3. Run the software
|
||||||
|
```Bash
|
||||||
|
dotnet run
|
||||||
|
```
|
||||||
|
4. The .NET code is calling a function from the C++ library that was built. The result is the sum of two integers
|
||||||
Reference in New Issue
Block a user