I've been trying to figure this out lately. It is working on my Windows machine, where I got SQLite from NuGet, but...
When I put System.Data.SQLite.dll
and SQLite.Interop.dll
straight from my Windows machine into Linux server it says that SQLite.Interop.dll
is not found, but I am sure I see it next right to executable.
Then I tried to compile System.Data.SQLite.dll
with /p:UseInteropDll=false
, but with no luck. This time it says that System.Data.SQLite.dll
is not found.
What is this "not found" mystery?
Starting with System.Data.SQLite.Core 1.0.109 you don't need to compile anything yourself since the native SQLite.Interop.dll
files are included in the NuGet package for all platforms (Linux, macOS and Windows). Note that although the dll
extension is used for all platforms, the files are actually native dynamic libraries (usually suffixed dylib on macOS and so on Linux).
$ find ~/.nuget/packages/system.data.sqlite.core/1.0.111/runtimes -name SQLite.Interop.dll -print0 | xargs -0 file
…/runtimes/linux-x64/native/netstandard2.0/SQLite.Interop.dll: ELF 64-bit LSB pie executable x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=96ce4120b31bad7d95f7b9ccf7c4bbb7717ae0b1, with debug_info, not stripped
…/runtimes/osx-x64/native/netstandard2.0/SQLite.Interop.dll: Mach-O 64-bit dynamically linked shared library x86_64
…/runtimes/win-x86/native/netstandard2.0/SQLite.Interop.dll: PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
…/runtimes/win-x64/native/netstandard2.0/SQLite.Interop.dll: PE32+ executable (DLL) (GUI) x86-64, for MS Windows
Unfortunately, the MSBuild target responsible for copying the native dynamic libraries into the output directory only works on Windows. This is probably because the authors of the package assumed that .NET Framework only runs on Windows, which is not true thanks to Mono. Also, if you wanto to have a look, the CopySQLiteInteropFiles
target can be found in ~/.nuget/packages/system.data.sqlite.core/{version}/build/net4*/System.Data.SQLite.Core.targets
.
But it's possible to automatically copy the SQLite.Interop.dll
file into the output directory on Linux and macOS. Add the FixSQLiteInteropFilesOnLinuxAndOSX
target (described below) in your csproj file and you'll be able to use System.Data.SQLite.Core on Linux and macOS running mono without the DllNotFoundException
. Here's how your project should look like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
</PropertyGroup>
<Target Name="FixSQLiteInteropFilesOnLinuxAndOSX" BeforeTargets="CopySQLiteInteropFiles">
<ItemGroup>
<SQLiteInteropFiles Condition="$([MSBuild]::IsOsPlatform(Linux)) OR $([MSBuild]::IsOsPlatform(OSX))" Remove="@(SQLiteInteropFiles)" />
<SQLiteInteropFiles Condition="$([MSBuild]::IsOsPlatform(Linux))" Include="$(PkgSystem_Data_SQLite_Core)/runtimes/linux-x64/native/netstandard2.0/SQLite.Interop.*" />
<SQLiteInteropFiles Condition="$([MSBuild]::IsOsPlatform(OSX))" Include="$(PkgSystem_Data_SQLite_Core)/runtimes/osx-x64/native/netstandard2.0/SQLite.Interop.*" />
</ItemGroup>
</Target>
<ItemGroup>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.111" GeneratePathProperty="true" />
</ItemGroup>
</Project>
Make sure to add GeneratePathProperty="true"
in the package reference. This is required for the PkgSystem_Data_SQLite_Core
property to be defined.
No code changes necessary. You can build it yourself.
apt-get install build-essentials unzip
unzip
and cd Source
, chmod +x
the compile-interop-assembly-release.sh
build shell script, then run it ./compile-interop-assembly-release.sh
. - It'll build an .so
file in the ../bin
directory. .so
file to the directory that has your application inUse Mono.Data.SQLite.dll
on Linux. Take a look at the Mono manual to using SQLite on Linux or build the System.Data.SQLite.dll on Mono.
You can also map the DLL:
<configuration>
<dllmap dll="sqlite" target="libsqlite.so.0" os="linux"/>
<dllmap dll="sqlite" target="libsqlite.0.dylib" os="osx"/>
<dllmap dll="sqlite3" target="libsqlite3.so.0" os="linux"/>
<dllmap dll="sqlite3" target="libsqlite3.0.dylib" os="osx"/>
</configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With