Build project using LibGit2Sharp, have it run on .NET & Mono (Windows, Linux, Mac)

1k views Asked by At

Using LibGit2Sharp 0.22.0-pre20150606092322 (with NuGet), when I build my project on Windows, the NativeBinaries folder only includes .dll files, which I assume don't work on Linux (unless they're .NET libraries). Attempting to run the project on Linux causes an exception:

System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2-ff8d635

I assume if I tried to build my project on Linux, it would contain the correct libraries, though no Windows ones. Is there a way I can build the project on one system and provide a download that works on all systems?

edit: Actually, I just built the project on Linux using MonoDevelop. The output does include a "packages" folder which contains a .so file in some sub-folder, but I still get the same exception if I just run it like that.

1

There are 1 answers

7
Brandon Ording On BEST ANSWER

Since you're using the latest prerelease LibGit2Sharp package, then you should also have a LibGit2Sharp.NativeBinaries package, which includes the native git2 libraries that LibGit2Sharp needs. In that package we include the following versions:

  • 32-bit Windows (dll)
  • 64-bit Windows (dll)
  • 32 & 64-bit Mac (dylib), one file supports both architectures
  • 64-bit Linux (so)

If you're trying to run on 32-bit linux, then there isn't a pre-built library included for that. What distro/architecture are you running?

When you install the LibGit2Sharp package, an MSBuild properties file is added to your project, and that is what handles copying the native binaries to the correct location as part of the build process.

If you're building on linux, then the build output should contain libgit2-ff8d635.so in the same directory as LibGit2Sharp.dll.

As long as your project isn't doing something that requires shadow copying your assemblies, that should be enough for mono to find and load the linux library.

If you are, for example if it's an asp.net project, then you have to make sure that the native linux library loader can find .so file, either by setting the LD_LIBRARY_PATH, or actually installing it.

As far as building it once goes, since all of the pre-build native images are already sitting in the NativeBinaries package, your release process could include them directly from there.