如何在 Mono/MacOsX 上使用 Libgit2Sharp?

How to use Libgit2Sharp on Mono/MacOsX?

我在 OSX 10.9.5 上使用 Xamarin Studio 5.7 和 Mono 3.12。我使用 NuGet 下载了 libgit2sharp 库。我可以在编写代码时使用 类,没有编译器错误。但是在 运行 时我收到以下错误:

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2-91fa31f at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2-91fa31f at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0

它似乎在抱怨它找不到 git2-91fa31f dll,但是当我查看包文件夹时,我可以看到该 dll 确实存在。

有没有人以前见过这个或者知道我能做些什么来解决它?

更新

我按照@nulltoken 的说明下载了 libgit2 源代码。使用 cmake -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DBUILD_CLAR:BOOL=OFF -DUSE_SSH=OFF -DENABLE_TRACE=ON -DLIBGIT2_FILENAME=git2-91fa31f -DCMAKE_OSX_ARCHITECTURES="i386;x86_64" .. 然后 cmake --build .

构建它

这产生了一个名为 libgit2-91fa31f.dylib 的文件。我把这个文件放在我项目的exe所在的bin/Debug目录下,LibGit2Sharp.dll所在的目录。但是现在当我 运行 程序时,我得到以下信息:

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.EntryPointNotFoundException: git_threads_init at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.EntryPointNotFoundException: git_threads_init at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0

我觉得我搞砸了 cmake 部分。对吗?

libgit2sharp 需要安装 libgit2。你安装了吗?

https://libgit2.github.com

LibGit2Sharp 是一个绑定到原生 libgit2 库的 .Net。

该项目通过使用持续集成服务器(Travis and AppVeyor[=确保在Win 32/64、Linux和MacOsX上的所有测试运行 47=]).

但是,目前 LibGit2Sharp NuGet 包仅包含 Windows 本机编译的二进制文件,因此不能在 Linux/MacOsX 上按原样使用。为了在这些环境中使用 LibGit2Sharp,您必须自己构建 libgit2。

为了实现这一点,您需要 CMake

您可以从 CI 服务器使用的 build.libgit2sharp.sh script 中获得一些灵感。

在此 Issue 中,Xamariner(和 LibGit2sharp 核心贡献者)Therzork 分享了一些关于在 MacOsX 上构建的指南。

更新

I have a feeling I messed up the cmake part. Is that correct?

我认为得到 CMake 部分就好了。引发的异常是 System.EntryPointNotFoundException。这意味着 LibGit2Sharp 尝试从 libgit2 调用一个方法,该方法要么不存在,要么其签名(参数数量、类型等)与公开的内容不匹配。

事实上,每个版本的 LibGit2Sharp 都适用于特定版本的 libgit2。 libgit2 缩写的 commit sha 用于准确指定预期的 libgit2 版本。

Version v0.20.1 预计将与 libgit2 的编译版本一起工作,因为它在提交 91fa3af 中(对于每个 LibGit2sharp 提交,您可以通过以下方式检查预期的 libgit2 版本查看 libgit2 子模块(例如,v0.20.1)。

我的猜测是您实际上正在构建一个不同的 libigt2 提交(可能是最新的)。为了构建提交 91fa31f,运行 以下 git 命令行 shell:

$ git clone https://github.com/libgit2/libgit2
$ cd libgit2
$ git checkout 91fa31f

然后 运行 你的 CMake 魔法。生成的二进制文件现在应该符合 LibGit2Sharp 的预期。