Red Player
Red Player

Reputation: 1

Can you link a .so in dotnet?

I have a .NET core project that uses a .so file. I use [DllImport()] to import it. My problem is that this .so has a dependency libtomcrypt.so so it can not locate some symbols(undefined symbol: cipher_descriptor).

I tried importing my .so in C and it works fine if I specify the linker variable -ltomcrypt.

Adding a reference to libtomcrypt.so in the .NET core project did not help because it is a native .so.

Is there any way to link libtomcrypt.so to dotnet?

Upvotes: 0

Views: 1828

Answers (1)

Aleksey
Aleksey

Reputation: 803

Try to load your library with NativeLibrary first.

static class Library
{
const string MyLibrary = "mylibrary";

static Library()
{
    NativeLibrary.SetDllImportResolver(typeof(Library).Assembly, ImportResolver);
}

private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
    IntPtr libHandle = IntPtr.Zero;
    if (libraryName == MyLibrary)
    {
        // Try using the system library 'libmylibrary.so.5'
        NativeLibrary.TryLoad("libmylibrary.so.5", assembly, DllImportSearchPath.System32, out libHandle);
    }
    return libHandle;
}

[DllImport(MyLibrary)]
public static extern int foo();
}

Interacting with native libraries in .NET Core 3.0

Upvotes: 1

Related Questions