C++

Cross Compiling Gtkmm Applications to Windows

There’s a readily available Gtkmm development package for Windows, but the following could come in handy for those who don’t own Windows, or want to escape the inconvenience of setting up and booting into another environment. A basic understanding of using the console is required and though I’m using Ubuntu, the steps are easily transferable.

First you’ll need to install the following packages and their dependencies:

mingw32
wine

Wine is only required if you don’t have access to Windows and want to check basic functionality. For a simple example I’m going to cross-compile the Hello World program in the Gtkmm documentation:

g++ helloworld.cc main.cc -o helloworld `pkg-config gtkmm-2.4 –cflags –libs`

This will compile and link “Hello World” for regular use under the Ubuntu environment. To make an .exe we need to alter this in two ways:

  1. Substitute g++ with the equivalent Windows build.
  2. Tell pkg-config to use Windows development Gtkmm dlls instead of the Ubuntu development shared objects.

The first part should be provided by the mingw32 suite you installed earlier. On my system it installed:

i586-mingw32msvc-g++

Conveniently the Inkscape project supplies the Gtk/Gtkmm development dlls all in one place and provides the basis for the following instructions. What they’ve done is set the pkg-config paths to work from /target, so you can put the unzipped directory wherever you want, as long as you create a symbolic link to it from /target as root:

ln -sf /home/alex/gtk28 /target

The next instruction gives precedence to the libraries in /target, rather than on your system, by temporarily changing the first location pkg-config searches:

export PKG_CONFIG_PATH=/target/lib/pkgconfig

You can check if this worked by examining that /target is the prefix for the relevant flags:

pkg-config gtkmm-2.4 –cflags –libs

Once it’s all working you can create the .exe with the following:

i586-mingw32msvc-g++ helloworld.cc main.cc -o helloworld `pkg-config gtkmm-2.4 –cflags –libs`

You can add the mwindows flag is to stop the console popping up, but I left it out to demonstrate the complete program on the right, running under Windows. If you don’t have Windows you can use Wine to test it loads, but be aware I had some problems regarding missing fonts that would otherwise be available–but essentially it will work fine given a proper Windows installation.

When distributing your application, you either need to direct your users to install the Gtk and Gtkmm runtime environments, or download them yourself and lump the dlls in the same directory as your binary (have a look at the Inkscape Windows zip file). I’ve managed to get the total build size down to about 8MB by deleting some locales and using UPX, and that example is running off my USB key.