在MonoMac应用程序中包含自定义dll和dylib

bri*_*ght 7 mono bundle dylib monomac

背景:我的MonoMac应用程序使用sqlite3.0.8.6.dylib的自定义构建.

我需要确切的步骤让MyApp.app使用这个dylib.

以下是我采取的一些步骤:

  1. 将dylib复制到MyApp.app/Contents/SharedSupport.(相关问题:这是第三方dylibs的首选位置还是MyApp.app/Contents/Frameworks首选?)

  2. 更改了库的已安装名称,以使其与新位置匹配.

    MyApp.app/Contents/SharedSupport> otool -L libsqlite3.0.8.6.dylib 
    libsqlite3.0.8.6.dylib:
        @executable_path/../SharedSupport/libsqlite3.0.8.6.dylib (compatibility version 9.0.0, current version 9.6.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    
    Run Code Online (Sandbox Code Playgroud)

问题

  1. MyApp.app/Contents/MacOS/MyApp不直接引用dylib,因此我无法使用install_name_tool指向新的库位置.我相信该库是由System.Data.Sqlite.dll引用的.
  2. 我想在启动器脚本中覆盖DYLD_FALLBACK_LIBRARY_PATH, MonoMac现在使用二进制启动器(MyApp.app/Contents/MacOS/MyApp),而不是脚本,所以我在那里运气不好.

MonoMac众神会帮助解决一个简单的解决方案吗?我花了几个月的时间,一直在努力让它发挥作用.

请提供确切的步骤 - 这个问题与细节有关.

Mar*_*lig 4

看看我对这个问题的回答: Setting path of the Native Library for DllImport on Mono for Mac

二进制启动器来自monodevelop/main/build/MacOSX/monostub.m

您可以使用任一MyApp.app/Contents/Frameworks路径或其他路径,重要的是不要在您的路径中使用任何路径名[DllImport],而是将<dllmap>using添加@executable_path到您的路径中app.config,就像我在其他答案中解释的那样。

那里还有一个指向 github 上的测试应用程序的链接。

详细说明

  1. 在 中选择一个路径MyApp.app来安装您的本机 dll,例如Contents/SharedSupport/sqlite3.0.8.6.dylib

  2. 计算从托管程序集所在目录到本机程序集的相对路径.dll,并将其添加@executable_path到其前面。

    例如,如果您的托管程序集位于 中 Contents/MonoBundle/MyApp.exe,本机 dll 位于 中 Contents/SharedSupport/sqlite3.0.8.6.dylib,那么它就是 @executable_path/../SharedSupport/sqlite3.0.8.6.dylib.

  3. 使用 . 将库的安装名称更改为此相对路径install_name_tool

  4. 将新MyApp.exe.config文件添加到您的项目中,其中包含

    <configuration>
      <dllmap dll="sqlite" target="@executable_path/../SharedSupport/sqlite3.0.8.6.dylib" />
    </configuration>
    
    Run Code Online (Sandbox Code Playgroud)

    使用您在步骤 2 中计算的路径作为该target字段。在 MonoDevelop 中右键单击该文件,从上下文菜单中选择“快速属性”并启用“复制到输出目录”。这会将文件复制到Contents/MonoBundle目录中,因此它位于您的MyApp.exe.

  5. 用于[DllImport ("sqlite")]在代码中引用它。

当另一个库引用它时

例如,当另一个库Mono.Data.Sqlite.dll引用它时,它会变得有点复杂。

使用与上面相同的步骤,但是您需要弄清楚其他库在其中使用哪个名称[DllImport]来引用本机库并将其放入<dllimport dll="..." />. [DllImport]您可以在源代码中查找语句,也monodis可以在程序集上运行并搜索pinvokeimpl,例如:

// method line 679
.method assembly static hidebysig pinvokeimpl ("sqlite3" as "sqlite3_create_function" cdecl )
       default int32 sqlite3_create_function (native int db, unsigned int8[] strName, int32 nArgs, int32 nType, native int pvUser, class Mono.Data.Sqlite.SQLiteCallback func, class Mono.Data.Sqlite.SQLiteCallback fstep, class Mono.Data.Sqlite.SQLiteFinalCallback ffinal)  cil managed preservesig 
{
    // Method begins at RVA 0x0
} // end of method UnsafeNativeMethods::sqlite3_create_function
Run Code Online (Sandbox Code Playgroud)

Mono.Data.Sqlite.dll使用“sqlite3”来引用本机 dll 也是如此,因此您的MyApp.exe.config文件将如下所示:

    <configuration>
      <dllmap dll="sqlite3" target="@executable_path/../SharedSupport/sqlite3.0.8.6.dylib" />
    </configuration>
Run Code Online (Sandbox Code Playgroud)