Files
2020-08-16 16:51:37 -04:00

1.3 KiB

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
cd Libs; mkdir build
  1. Build the C++ library
cd build; cmake -DCMAKE_BUILD_TYPE=DEBUG ..; make
  1. Copy the library to the root of the project
cp libTestExample.so ../../

Building .NET console software

  1. Ensure that you are in the root of the project. Then build the software
dotnet build
  1. Copy the C++ library to where the executable is build
cp libTestExample.so bin/Debug/netcoreapp3.1/
  1. Run the software
dotnet run
  1. The .NET code is calling a function from the C++ library that was built. The result is the sum of two integers