Skip to content

Draft: Enable full-static build on Windows

Loïc Le Page requested to merge neodesys/glib:windows-static-build into main

Closes #692 (closed)

Glib cannot be built statically on Windows because glib, gobject and gio modules need to perform specific initialization when DLL are loaded and cleanup when unloaded. Those initializations and cleanups are performed using the DllMain function which is not called with static builds.

Issue is known for a while and solutions were already proposed but never merged (see: #692 (closed)). Last patch is from version 2.36.x and since then the "constructor/destructor" mechanism has been implemented and used in other part of the system.

This patch takes back the old idea and updates it to the last version of glib to allow static compilation on Windows.

WARNING: because DllMain doesn't exist anymore in static compilation mode, there is no way of knowing when a Windows thread finishes. This patch implements a workaround for glib threads created by calling g_thread_new(), so all glib threads created through glib API will behave exactly the same way in static and dynamic compilation modes. Unfortunately, Windows threads created by using CreateThread() or _beginthread/ex() will not work out of the box. In this latter case, user will have to call g_system_thread_exit() explicitely at the end of the thread procedure (see glib/tests/private.c test /thread/private3). This should not be an issue as user should use exclusively the glib API to manipulate threads in order to be cross-platform compatible and this would be very unlikely and cumbersome that he may mix up Windows native threads API with glib one.

Edited by Loïc Le Page

Merge request reports