本文介绍了C#:在 Linux 上的 .NET Core 中引用或使用 .so 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 .NET Framework 上有一个项目,该项目引用了 Fico Xpress 求解器 dll.所需的 dll 是 –

We have a project on .NET Framework that references the Fico Xpress solver dll. The dll’s required are –

  • Xprb.dll
  • Xprbdn.dll
  • Xprsdn.dll

由于没有可用于使用 Fico Xpress Solver 的 nuget 包,我们安装了 Fico Xpress Solver 并将这些 dll 从安装目录复制到项目文件夹内名为 lib 的本地文件夹,并添加了路径引用到 lib 文件夹中的这些 dll.因此,在编译时,项目使用这些 dll 的引用(存在于 lib 文件夹中)进行编译.该项目成功构建.当我们的项目调用 Fico Xpress Solver 时,从 安装目录 中使用上述所需的 dll,这可能是通过 环境变量 访问的(本地文件夹中的 dll 只是为了成功编译代码,我们本可以将其指向实际的 Fico Xpress Solver 安装目录,但我们将 dll 放在 lib 文件夹中,以便我们可以将其添加到 SVN)并且项目成功运行使用Fico Xpress 求解器.

As no nuget packages are available for using Fico Xpress Solver, we installed the Fico Xpress Solver and copied these dll’s from the installation directory to our local folder named lib inside the project folder and added path reference to these dll’s inside the lib folder. So when compiling, the project uses the reference of these dll’s (present in the lib folder) to compile. This project successfully builds. When our project calls the Fico Xpress Solver, then the above required dll's are used from the installation directory which is probably accessed through the environment variables (the dll's in local folder are just for successful compilation of code and we could have pointed it to the actual Fico Xpress Solver installation directory but we put the dll's in lib folder so that we can add it to the SVN) and the project runs successfully using the Fico Xpress Solver.

现在,我们已将项目移植到 .NET Core,以便在 Linux 机器上运行.所以我们在Linux机器上安装了Fico Xpress Solver,并通过/opt/xpressmp/bin/文件夹内的优化器可执行文件(这是linux机器的默认安装目录)测试安装是否成功.安装成功,Fico Xpress Solver 运行正常(使用他们网站上提供的方法检查).

Now, we have ported the project to .NET Core so as to run the same on Linux machines. So we installed Fico Xpress Solver on the Linux machine and tested whether the installation was successful by using the optimizer executable inside the /opt/xpressmp/bin/ folder (this is the default installation directory for linux machines). Installation is successful and the Fico Xpress Solver runs correctly (checked it using the method given on their website).

当我们构建项目时,它编译成功,因为它仍然引用本地 lib 文件夹中所需的 dll.但是,当我们的项目在运行时调用 Fico Xpress Solver 时,它会失败,因为它无法加载所需的 dll(它可能在设置为 /opt/的 LD_LIBRARY_PATH 中搜索)xpressmp/lib/ 由安装手册中指定的 /opt/xpressmp/bin/xpvars.sh 脚本设置.该文件夹包含所有 .sostrong> 文件而没有 dll 文件.)错误如下 –

When we build the project, it compiles successfully, as it still refers to the required dll’s inside the local lib folder. But, when our project calls the Fico Xpress Solver during run time, it fails as it could not load the required dll’s (which it was probably searching in the LD_LIBRARY_PATH which is set to /opt/xpressmp/lib/ as set by the /opt/xpressmp/bin/xpvars.sh script which was specified in the installation manual. This folder contains all the .so files and no dll files.) The error is as below –

无法加载共享库xprb.dll"或其依赖项之一.为了帮助诊断加载问题,请考虑设置LD_DEBUG 环境变量:libxprb.dll:无法打开共享对象文件:没有那个文件或目录

我们不确定我们使用的方法(即使用 dll 编译和运行)是否正确,或者我们是否必须了解如何使用 .so 文件来编译和运行项目.由于代码构建成功,我们希望它可以运行,但找不到共享对象文件.

We are not sure whether the approach we are using i.e. using dll's to compile and run is correct or whether we have to some how use the .so files to compile and run the project. As the code was building successfully, we expected it to run, but the shared object files are not found.

有人可以指定一种在 linux 中使用 Xpress 求解器的方法,或者在 windows 和 linux 上使用相同的 3rd 方软件时需要遵循的一些一般准则.我们是否需要更改代码或添加对 .so 而不是 .dll 文件的引用

Can someone please specify a way to use Xpress solver in linux or some general guidelines that need to be followed when using the same 3rd party software on windows and then on linux. Do we need to change the code or add reference to .so rather than the .dll files

DllImport 是唯一的方法(在不同的博客上建议)

Is DllImport the only way to do this (suggested on different blogs)

推荐答案

我们终于找到了一种方法来做到这一点,但我们不确定它是否适用于所有人,并且它可能无法解决某些人别人的问题.这是我们的方法 -

We finally figured out a way to do so but we are not sure it will be applicable to everybody and it might not solve somebody else's problem. Here goes our approach -

如问题中所述,xprb.dll 无法加载,因为 libxprb.dll 是它在 Xpress lib 目录 (/opt/xpress/lib/).但在 Linux 中安装 Xpress 后,安装内容只有 .so 文件,没有 .dll 文件.

As mentioned in the question, the xprb.dll could not be loaded because libxprb.dll was what it was searching for in the Xpress lib directory (/opt/xpress/lib/). But after installing the Xpress in Linux, the installation contained only .so files and no .dll files.

有一些博客建议使用 DllImport 方法加载 .so 文件,然后调用这些方法.我们没有尝试这些方法,因为我们正在寻找比这更简单的方法.

There were some blogs which suggested using DllImport method to load the .so files and then call the methods. We didn't try those methods as we were looking for something simpler than that.

在对问题进行研究后,我们发现只有将共享库的加载指向已安装的 .so 文件,它才可能起作用.所以在我们的具体案例中情况就是这样 -

After investing the problem we found that only if we point the loading of shared libraries to somehow the installed .so files, it might work. So the situation was as such in our specific case -

  • 我们在/opt/xpressmp/lib/文件夹中没有 libxprb.dll
  • 我们在/opt/xpressmp/lib/文件夹中有 libxprb.so 文件而不是 libxprb.dll(如果我们没有这个文件,我们可能不知道还有哪些 .要使用的文件)
  • We did not have a libxprb.dll in the /opt/xpressmp/lib/ folder
  • We had libxprb.so file instead of libxprb.dll in the /opt/xpressmp/lib/ folder (if we didn't have this file we probably wouldn't have known which other .so file to use)

所以我们在/opt/xpressmp/lib/文件夹中创建了一个符号链接文件 libxprb.dll(我们可能可以将它放在任何地方,只要它在 LD_LIBRARY_PATH 中的路径之一)使用命令指向/opt/xpressmp/lib/文件夹中的libxprb.so文件-

So we created a symlink file libxprb.dll in /opt/xpressmp/lib/ folder (which we probably could have placed anywhere as long as it was in one of the path in LD_LIBRARY_PATH) which pointed to the libxprb.so file in /opt/xpressmp/lib/ folder using the command -

ln -s/opt/xpressmp/lib/libxprb.dll/opt/xpressmp/lib/libsprb.so

ln -s /opt/xpressmp/lib/libxprb.dll /opt/xpressmp/lib/libsprb.so

所以现在当加载 xprb.dll 时,它会查找 libxprb.dll,后者又指向 libxprb.so 文件(因为 libsprb.dll 是 libxprb.so 的符号链接),因此 xprb.dll 加载成功.

So now when xprb.dll was loaded, it looked for libxprb.dll which in turn pointed to libxprb.so file (as libsprb.dll was a symlink to libxprb.so) and hence xprb.dll was loaded successfully.

这篇关于C#:在 Linux 上的 .NET Core 中引用或使用 .so 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:13
查看更多