0

I am trying to use the great library libgit2sharp on a Mac. Therefore I'm using Xamarin Studio to create a Mac app to read information from a Git repository.

When I use the library within a normal mono executable (target Mono / .NET4.5, everything just works fine. But when I create a Xamarin.Mac project, the following error occurs and I don't see a way round:

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: git2-a5cf255
  at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_libgit2_init ()
  at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00006] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
  at LibGit2Sharp.Core.NativeMethods..cctor () [0x00058] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
   --- End of inner exception stack trace ---
  at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00008] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
  at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options, LibGit2Sharp.Repository+RepositoryRequiredParameter requiredParameter) [0x0007e] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: git2-a5cf255
  at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_libgit2_init ()
  at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00006] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
  at LibGit2Sharp.Core.NativeMethods..cctor () [0x00058] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
   --- End of inner exception stack trace ---
  at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00008] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 
  at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options, LibGit2Sharp.Repository+RepositoryRequiredParameter requiredParameter) [0x0007e] in <9f09b2b30ef34f0b8a3a0a6bdde2be90>:0 

When googling around, I found a lot of topics closing with "build libgit2sharp on your machine to get the correct .dylib files". This may have been a problem in the past, but not anymore so this did not help me. As said, I can easily use libgit2sharp with a mono executable.

By the way, I cannot use the Nuget package "libgit2sharp" because it says that the Xamarin.Mac-target is not compatible. I'm using the package "libgit2sharp.portable" which throws no error on install- or compile-time. It's just that error message from above in runtime.

My sample application is available to download here: https://files.fm/u/7eq55jjq

Any suggestions to get around this? Thanks!

Update The error might hint that the path to the dylib is wrong ("DLL not found"), but that's not true. The file is exactly where it should be.

Waescher
  • 5,361
  • 3
  • 34
  • 51

2 Answers2

1

I'm pretty sure that the problem come from the fact that the native libraries that come in a separate nuget package 'Libgit2sharp.NativeBinaries' are not copied in the output folder (should be in a sub directory 'lib').

The copy is normally done by a target during the build of the project.

Philippe
  • 28,207
  • 6
  • 54
  • 78
1

Oh my god, I was so close. and Philippe was there as well: It is not correct to make sure to place the dylib file to the folders in the output directory (like the Nuget package does automatically) which would be /lib/osx/libgit2-1196807.dylib.

Instead, I found out that the dylib-file has to be placed inside the *.app file. But instead of doing this manually, Xamarin Studio provides a thing called "Native References" for Xamarin.Mac projects.

enter image description here

I should have seen this "Native References" before. Sorry guys but maybe that helps someone.

Community
  • 1
  • 1
Waescher
  • 5,361
  • 3
  • 34
  • 51