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.
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
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With