Home > Net >  Eliminate "e_sqlite3.dll" during single file compilation
Eliminate "e_sqlite3.dll" during single file compilation

Time:12-03

In my attempts to compile a single-file binary that leverages Microsoft.Data.Sqlite, I am consistently left with two files that are both required for the application to work.

  1. {ProjectName}.exe
  2. e_sqlite3.dll

Is it possible to include the e_sqlite3.dll into the exe?

It appears that System.Data.Sqlite exhibits the same behaviour, but instead a file called SQLite.Interop.dll.

Sample code

Note: I realize there is no actual interop with SQLite happening, this code is purely meant to demonstrate the compilation.

ProjectName.fsproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Data.Sqlite" version="7.*" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>

Program.fs

module ProjectName.Program

open System

[<EntryPoint>]
let main (argv : string[]) =
    printfn "Hello world"
    0

Compiling the project as follows:

dotnet publish .\ProjectName.fsproj -c Release

CodePudding user response:

Unfortunately, it does not appear to be possible to include the e_sqlite3.dll into the exe file. The only way to have a single-file executable is to use the .NET Native toolchain, which can be enabled by setting the property to true.

CodePudding user response:

Solution

It turns out that it's rather easy to do this in net6, net7 and (presumably) beyond, by setting IncludeNativeLibrariesForSelfExtract to true.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishTrimmed>true</PublishTrimmed>

    <!-- Must include this line -->
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    <DebuggerSupport>false</DebuggerSupport>
    <EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
    <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
    <InvariantGlobalization>true</InvariantGlobalization>
    <UseNativeHttpHandler>true</UseNativeHttpHandler>
    <UseSystemResourceKeys>true</UseSystemResourceKeys>
    <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Data.Sqlite" version="7.*" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>
  • Related