异常:System.DllNotFoundException - 使用.NET Core 2.1调用CoolProp(本机C++库)函数

Edu*_*Bic 6 c# pinvoke shared-libraries .net-core c#-native-library

我有一个用C#编写的.NET项目,它与CoolProp库有关(可在https://github.com/CoolProp/CoolProp下载).它使用PInvoke调用CoolProp函数.

不幸的是,我必须在Linux环境中运行该程序(恰好是AWS lambda env https://docs.aws.amazon.com/en_us/lambda/latest/dg/current-supported-versions.html).

现在,我想用dotnet run我的PC上的.NET core(命令)和Ubuntu OS 执行它,但我总是得到以下错误:

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

测试程序是:

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); 
        } 
    } 
}
Run Code Online (Sandbox Code Playgroud)

Program.cs是在同一文件夹中libCoolProp.so.

笔记:

  • 在Windows 10中的相同程序编译和执行对.NET核心与它的libCoolProp.dll作品.
  • Ubuntu 18中的相同程序使用Mono Runtime编译和执行.

如何解决CoolProp lib和.Net Core运行时之间的兼容性问题?

Edu*_*Bic 0

我找到了解决方案。

.NET core 构建的可执行文件位于 inside 中bin/debug/netcoreapp2.1/,因此足以将库链接到正确的路径:

[DllImport("../../../libCoolProp.so")]
Run Code Online (Sandbox Code Playgroud)

对于 Windows 10 来说这不是必需的,因为 .NET Core 运行时会在调用dll该命令的文件夹内部进行搜索。dotnet run

有关更多信息,请查找问题:https://github.com/dotnet/core/issues/2015