commit bc545e7cafe9aa5f939c03941b164a06c81ba032 Author: kdeng00 Date: Sun Aug 16 16:51:37 2020 -0400 DotNet andd C++ example diff --git a/DotnetAndCPP.csproj b/DotnetAndCPP.csproj new file mode 100644 index 0000000..d453e9a --- /dev/null +++ b/DotnetAndCPP.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Libs/CMakeLists.txt b/Libs/CMakeLists.txt new file mode 100644 index 0000000..3c0b327 --- /dev/null +++ b/Libs/CMakeLists.txt @@ -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) diff --git a/Libs/include/Arithmetic.h b/Libs/include/Arithmetic.h new file mode 100644 index 0000000..943a50b --- /dev/null +++ b/Libs/include/Arithmetic.h @@ -0,0 +1,11 @@ +#ifndef ARITHMETIC_H_ +#define ARITHMETIC_H_ + +class Arithmetic { +public: + Arithmetic() = default; + + int add(const int, const int); +}; + +#endif diff --git a/Libs/src/Arithmetic.cpp b/Libs/src/Arithmetic.cpp new file mode 100644 index 0000000..0824a2b --- /dev/null +++ b/Libs/src/Arithmetic.cpp @@ -0,0 +1,5 @@ +#include "Arithmetic.h" + +int Arithmetic::add(const int a, const int b) { + return a + b; +} diff --git a/Libs/src/Entry.cpp b/Libs/src/Entry.cpp new file mode 100644 index 0000000..a2b3db6 --- /dev/null +++ b/Libs/src/Entry.cpp @@ -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); +} +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..1bab842 --- /dev/null +++ b/Program.cs @@ -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); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..f2536af --- /dev/null +++ b/README.md @@ -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