25 August 2012

Static library attempt for GTK

In my previous post when I had to make rsvg-view a static build, a complete set of static GTK libraries needed. GTK known to refuse being build as static library on Windows. Obviously there is lot good reason (be it Windows or not), GTK is a library with lots of addons (theoretically a GLib based one) so a modular/extensible approach chosen, then if I recall internationalization won't work unless in dll mode, then GTK is known using lot of settings files and modules scattered in etc-lib-share folder, then in version 3 there is DBus stuff which all of these make it almost a deal breaker for static build mode. Although some modules (found in pango, gdk-pixbuf, gtk) can be statically linked to main library, others are not (need modification) such as wimp theme engine.

For this minimalistic use case (rsvg-view) where basically a window with just 2 zoom buttons, a static build would be nice eh?

Oh well let's just get it on...
Firstly this is GTK-3 that I tried which should prompt a serious warning message about stability/maturity. Historically in version 2.x IMO there is only 3 stable, good or sane version for Windows: the last 2.12.x, 2.16.x and 2.24.x. I did try make static GTK-2 (2.24.10) however almost all but three tests mysteriously crashed during startup, here is one of that survived: testtreecolumns (unfortunately not LTO optimized so it's a bit too big)

The main thing that prevented static linking is multiple definition of dllmain. In case you're not aware, GLib starting with 2.30 support static build but broken in 2.32 since glib-init introduced but is trivial to fix. So based on how this static glib work we could also do the same to gdk-pixbuf, pango and lastly to gtk itself:

- override the Windows guard in configure script which prevent static build. hint: "DLL on Windows"
- make sure all possible and desired modules being "bundled" via configure
- initialize hmodule instances as null instead of uninitialized
- prevent dllmain from being compiled by adding #ifndef DLL_EXPORT
- assign declspec instances as nothing when #ifndef DLL_EXPORT (or create your own guard) instead of dllimport/dllexport (seems GTK-3 somehow omit this already)

note: DLL_EXPORT and PIC are common flags used by libtool during compilation, they are off when configured with: --enable-static --disable-shared

This way, path finding and internationalization still works as expected. Here is example of gtk3-demo in static binary: gtk3-demo.7z

It would great to bundling it in my MinGW distro... kinda rare to see C-based GUI library, not that C is good for GUI

1 comment:

  1. you can use good meta-gui-toolkit with gtk target to make static build, like this: