Exception: System.DllNotFoundException - Invoke CoolProp (Native C++ library) functions with .NET Core 2.1
Asked Answered
G

1

6

I have a .NET project written in C# that has a dependency with the CoolProp library (available here https://github.com/CoolProp/CoolProp). It calls the CoolProp functions using PInvoke.

Unluckily I have to run this program in a linux environment (precisely the AWS lambda env https://docs.aws.amazon.com/en_us/lambda/latest/dg/current-supported-versions.html).

For now, I want to execute it with .NET core (command dotnet run) on my PC with Ubuntu OS but I get always the following error:

Unhandled Exception: System.DllNotFoundException: Unable to load shared library 'libCoolProp.so' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibCoolProp.so.so: cannot open shared object file: No such file or directory at Test1.Program.PropsSI(String Output, String Name1, Double Prop1, String Name2, Double Prop2, String Ref) at Test1.Program.Main(String[] args) in /home/user/Desktop/TestDllInUbuntu/Test1/Program.cs:line 23

The test program is:

using System; 
using System.Runtime.InteropServices;

namespace Test1 
{ 
    class Program 
    { 
        [DllImport("libCoolProp.so")] 
        private static extern double PropsSI(string Output, string Name1, double Prop1, string Name2, double Prop2, string Ref); 

        static void Main(string[] args) 
        {
            double propsRes = PropsSI("H", "T", 300.0, "Q", 0.0, "R410A"); 
            Console.WriteLine(propsRes); 
        } 
    } 
}

The Program.cs is in the same folder of libCoolProp.so.

Notes:

  • The same program in Windows 10 compiled and executed with .Net Core with its libCoolProp.dll works.
  • The same program in Ubuntu 18 compiled and executed with Mono Runtime works.

How to solve the compatibility issue between CoolProp lib and .Net Core runtime?

Gora answered 19/10, 2018 at 15:19 Comment(0)
G
0

I found the solution.

The executable file build by .NET core is inside bin/debug/netcoreapp2.1/, hence is sufficient to link the library with the correct path:

[DllImport("../../../libCoolProp.so")]

This is not necessary for Windows 10 as the .NET core runtime search the dll inside the folder in which the command dotnet run is called.

For more info look up the issue: https://github.com/dotnet/core/issues/2015

Gora answered 27/10, 2018 at 8:59 Comment(2)
While this does work, this is not ideal. I am personally a bit concerned about the fact that adding a .so as an EmbeddedResource in .csproj file does not cause it to be part of the build. It is unclear if this is a debug build problem, and maybe release has it (and debug misses it), but there must be a smart way to get your .so and DLL files to part of your executableEpitasis
Yes, that was for an accademic project. Today I will definitely go with the .csproj file in which you can put <None Include="libCoolProp.so" CopyToOutputDirectory="Always" />. This should copy the libCoolProp.so file in your bin/debug/netcoreapp/ directory hence you only need to write [DllImport("libCoolProp")] (but I did't test it)Gora

© 2022 - 2024 — McMap. All rights reserved.