Mono on Mac: DllNotFoundException despite SQLite.Interop.dll being in dllmap

随声附和 提交于 2019-11-28 01:35:00

问题


I have a C# application that uses SQLite and works fine on Windows.

The same Visual Studio project compiles fine in Xamarin Studio, but when running I get:

DllNotFoundException: SQLite.Interop.dll

Despite:

  • libsqlite3.0.dylib is in /usr/lib and also in the same folder as the executable and other DLLs
  • . is part of the $DYLD_LIBRARY_PATH
  • The executable and all SQLite-using DLLs have a matching <the_exe_or_dll_including_filename_extension>.config file containing:

<configuration> <dllmap dll="sqlite" target="libsqlite.0.dylib" os="osx"/> <dllmap dll="sqlite3" target="libsqlite3.0.dylib" os="osx"/> </configuration>

I also tried adding <dllmap dll="SQLite.Interop.dll" target="libsqlite3.0.dylib" os="osx"/>, not better.

What is the problem?


回答1:


You can easily find where mono is looking for that native library by setting the MONO_LOG_LEVEL to debug and MONO_LOG_MASK filtering to only DLL related messages.

export MONO_LOG_LEVEL=debug
export MONO_LOG_MASK=dll
mono yourprogram.exe

or as a one liner so you do not have to unset env vars:

MONO_LOG_LEVEL=debug MONO_LOG_MASK=dll mono yourprogram.exe

Mono and the OS-X dynamic link editor ('man dyld' for details) does not require DYLD_LIBRARY_PATH to be set to the current directory ('.'). Note: Linux does require LD_LIBRARY_PATH to include the current directory, if that is your intention.

  • Move those dll map files out of the way to remove them from the equation.
  • Unset DYLD_LIBRARY_PATH
  • cd in the directory that contains your CIL based exe, dlls and native dylib(s)
  • MONO_LOG_LEVEL=debug MONO_LOG_MASK=dll mono yourprogram.exe

Using the native dll/shared library trace output you can track which library is not being found (or one of its dependancies) or if it is the wrong ARCH for your mono version.

If you are still having problems, we would need to know which SQLite library you are using the options that you are using to compile it (or the arch version if getting it via a Nuget). A posting your dll trace output would quickly solve things also.

Notes:

I am assuming you are using the System.Data.SQLite library and are compiling the the options "/p:UseInteropDll=true /p:UseSqliteStandard=false".

Mono includes a SQLite in it's default install, it is 32-bit on OS-X:

file /Library/Frameworks/Mono.framework/Versions/4.0.2/lib/libsqlite3.dylib
/Library/Frameworks/Mono.framework/Versions/4.0.2/lib/libsqlite3.dylib: Mach-O dynamically linked shared library i386

Assuming you are using the OS-X package installer from Mono, thus are getting the 32-bit version of Mono and thus need 32-bit versions of the native libraries.

>>file `which mono`
/usr/bin/mono: Mach-O executable i386

The /usr/lib/libsqlite3.0.dylib is a multi ARCH fat binary, so that library is not a problem, but your debug output might show another one that is a problem,

>>file /usr/lib/libsqlite3.0.dylib
libsqlite3.0.dylib: Mach-O universal binary with 3 architectures
libsqlite3.0.dylib (for architecture x86_64):   Mach-O 64-bit dynamically linked shared library x86_64
libsqlite3.0.dylib (for architecture i386): Mach-O dynamically linked shared library i386
libsqlite3.0.dylib (for architecture x86_64h):  Mach-O 64-bit dynamically linked shared library x86_64



回答2:


You need to build and supply SQLite.Interop.dll (or more precisely libSQLite.Interop.dylib). The Mono distribution packages don't include it, probably because it's native code and really needs to be built on the target platform.

System.Data.SQLite on Windows uses a mixed mode approach (Managed data adapter + sqlite native code in one assembly). Mono however doesn't really support mixed mode assemblies.

So on MacOS there are two alternatives when it comes to building System.Data.SQLite on Windows:

  1. Use interop dll.
  2. Use libsqlite.x.x.dylib.

Both of these are native code and need to be built on the Mac.

Interop is Windows com speak so it's a bit disconcerting to see it used in a MacOS context. What this native dll is is the sqlite source code compiled up with some additional native code that can be P\Invoked by System.Data.SQLite. There are some benefits to using the interop dll as opposed to the sqlite dylib.

System.Data.SQLite ships with a copy of the relevant SQLite native source code in ./SQLite.Interop/src.core. You can build the interop library by running compile-interop-assembly-release.sh on the Mac. This will build libSQLite.Interop.dylib. Drop that in beside System.Data.SQLite and you should be good to go.

If you turn on Mono dll tracing you can watch the loader (see mono 4.8.0 loader.c) searching for the dll in various locations and with various name substitutions. Eventually it finds our dylib. It is also possible to use a dllmap entry in the System.Data.SQLite.dll.config file to direct the runtime to the dll. In my case Mono is on my app bundle so I have:

<dllmap dll="SQLite.Interop.dll" target="@executable_path/../Mono/libSQLite.Interop.dylib" os="!windows"/>

The dllmap target argument is passed to dlopen() so @executable_path et al are all usable.

I prefer this approach as it goes into the repo and provides some insight into what is going on when there's a foul up.



来源:https://stackoverflow.com/questions/31152451/mono-on-mac-dllnotfoundexception-despite-sqlite-interop-dll-being-in-dllmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!