How to include native GTK libraries in single-file dotnet executable?

Ivo Rogina 0 Reputation points
2023-09-23T06:55:41.2966667+00:00

I am trying to bundle a GT3 application into a single file dotnet executable.

A minimal not working example is this:

mkdir x
cd x
dotnet new gtkapp
dotnet build
dotnet publish -r win-x64 -c Release -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:IncludeAllContentsForSelfExtract=true -p:PublishReadyToRun=true --self-contained true

This produces an executable in bin\Release\net6.0\win-x64\publish\x.exe that works just fine on any machine, that has GTK installed. On Windows without GTK nothing happens, not even an error message is produced when running this x.exe that should -to my understanding- have everything included.

With a bigger example (too big to post here), I get an error message:

Unhandled exception. System.TypeInitializationException: The type initializer for 'Gtk.Application' threw an exception. ---> System.DllNotFoundException: Gtk: libgtk-3-0.dll, libgtk-3.so.0, libgtk-3.0.dylib, gtk-3.dll at GLibrary.Load(Library library) at Gtk.Application..cctor() --- End of inner exception stack trace --- at Gtk.Application.Init()

I have also tried skipping some cli arguments of the publish command, but nothing helped.

What could be the problem?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,787 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Krew Noah 500 Reputation points
    2023-09-23T09:46:04.24+00:00

    The issue you are encountering arises because the .NET single-file publish does not natively support bundling of non-.NET dynamic libraries (like GTK's native libraries). While the -p:IncludeNativeLibrariesForSelfExtract=true option attempts to include native libraries, it doesn't always handle third-party native libraries correctly, especially those not in the NuGet package format or not explicitly referenced by the project.

    To resolve this, you can manually copy the required GTK native libraries (like libgtk-3-0.dll and other dependencies) to the output directory where your single-file executable is located. Ensure that these libraries are in the same directory as the executable or in a location that is in the system's library search path.

    Additionally, make sure that all the GTK dependencies are installed on the target machine, as some libraries may have dependencies on other system libraries that are not bundled.

    Please note that this is a simplified workaround and might not cover more complex scenarios with additional dependencies.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.