Compile GTK+ code with Cygwin Tutorial

To compile code with GTK+ in Windows, the recommended toolchain is MinGW (for a variety of reasons). However, if you want to use Cygwin anyway here is some guidance, which may save you some searching time and head-scratching.

I’ll assume:

  • You have Cygwin installed and you know how to use it
  • You have gcc installed within Cygwin
  • You know how to add entries to the system PATH safely

(Note before we start that it may be possible to so some of this from Cygwin’s installer but I found that manual setup was easier)

Step 1

Download the ‘all-in-one bundle’ from the GTK+ download page and unzip it to your preferred installation directory, e.g. C:/gtk . This is the example directory I’ll use below.

Step 2

Set your system variables:

  • Add the ‘bin’ directory to your system PATH (e.g. C:/gtk/bin )
  • Add a variable called GTK_BASEPATH  with the value of the installation directory (e.g. C:/gtk )
  • Add a variable called LIB  with the value of the ‘lib’ subdirectory (e.g.   C:/gtk/lib )
  • Add a variable called PKG_CONFIG_PATH  with the value of the pkgconfig directory (e.g. C:/gtk/lib/pkconfig )

Note that these can also be done on the Cygwin command line or in your bash profile using ‘export’, e.g.

Step 3

Open a fresh Cygwin shell and ensure your path was set up correctly:

This should return a version number if you completed Step 2 correctly.

Step 4

Now we need to fix the paths in the pkgconfig directory. This is the most annoying part of the process. Take a look in the pkgconfig directory (e.g. C:/gtk/lib/pkgconfig) and you will find a number of .pc files. These contain information about where includes are for particular libraries. However, the top of each file contains a ‘prefix’ of where the GTK installation directory is, and for some reason these are set by default to some mystery values. We need to correctly set the prefix for all these .pc files.

This is a pain to do by hand and you may wish to script it, but it shouldn’t take more than two minutes or so.

The first line of each file is the prefix. E.g.

This simply needs to be changed to your GTK installation directory (using a Cygwin-style path). Thankfully, the rest of the entries use the prefix as a variable so this is all we need to change. So if your installation is in  C:/gtk

Then your prefix line simply looks like this:

Make sure to do this for all .pc files in this directory.

Step 5

Check the paths to libraries/includes are valid by typing

This should return a long line of library switches for gcc to use, and these should all have /cygdrive/… in them. If this contains the original file path prefix as shown in Step 4, you may have missed a file.

This line of code allows us to delegate the library part of the gcc call to the pkg-config tool, making compilation easier.

Step 6

Let’s compile a GTK+ hello world program. Here is one I took from the GTK+ tutorial:

This simply initialises a blank 200×200 window (which you must forcibly close since it does not handle close button events).

Compile with this gcc command:

If all goes well, the program should compile correctly and you should see the plain window, like this one:



If you have trouble executing the program (e.g. a shared library error), you may need to ensure you added the ‘bin’ directory to your system PATH correctly in Step 2.

Good luck!