C# : Referencing or using .so files in .NET Core on Linux

一世执手 提交于 2020-06-29 03:44:30

问题


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

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.

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).

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 –

Unable to load shared library 'xprb.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libxprb.dll: cannot open shared object file: No such file or directory

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.

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

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


回答1:


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 -

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.

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.

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 -

  • 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)

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

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.



来源:https://stackoverflow.com/questions/51166866/c-sharp-referencing-or-using-so-files-in-net-core-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!