Is it possible to call a shared library (.so) on linux platform from .NET core?

不问归期 提交于 2020-01-29 09:29:20

问题


I understand that aim of the .NET core is for cross platform development, but I am looking for backward compatibility. If I have a linux library available (maybe, legacy) and I want its functions to be called from .NET core application for linux platform. Is it possible?

I am not talking about ASP.NET core, I need it for a desktop application.


回答1:


Yes: https://github.com/dotnet/corefx/issues/343

That being said, I'll admit to not having actually tried it yet. I have used this a far bit on Mono, for what it's worth, and it's definitely worth reading their documentation on this: http://www.mono-project.com/docs/advanced/pinvoke/ ... because .net core seems to take much the same approach (eg: leave off the file extension for the p-invoked library, so the system can take care of linking to the right version etc...)

Eg how bits of .net core itself do this:

const string LIBC = "libc";

[DllImport(LIBC, EntryPoint = "getenv")]
private static extern IntPtr getenv_core(string name);

https://github.com/dotnet/corefx/blob/79d44f202e4b6d000ca6c6885a24549a55db38f1/src/System.Console/src/Interop/Interop.manual.Unix.cs




回答2:


Look at SWIG. It is a simple way to wrap a shared library and it generates all of the P/Invoke code for you as well as maps any header files (think structs) between your .so library and your C# code. We created a simple cpp wrapper around the .so library that we wanted to use and then let SWIG do its magic. We now have a C# library that can call from our .Net code (a simple console app) that invokes our linux library functions. We have done this on a PI, on unix hosts running Ubuntu, CentOS, and Debian with great success. http://www.swig.org/



来源:https://stackoverflow.com/questions/39240422/is-it-possible-to-call-a-shared-library-so-on-linux-platform-from-net-core

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