HowTo use Cygwin to compile GTK+ v2.8+ and Cairo v1.4+ GUI crossplatform applications

[italian version (older - meno aggiornata)]

The method described here is particularly well suited for Unix aware users, as you have all the tools you are accustomed to use in developing: state of the art editors, gcc, grep, nm, file, ecc... If you are a Win32 only user, probably is best for you the Dev-C++ method as is an IDE that is so common on this simple platform.
The applications builded with this method do not depend on the Xserver, so run natively on Win32 GDI. Dependence on 'cygwin1.dll' is regulated with the presence of  '-mno-cygwin' compile option.

Some Win32<->Posix differences:
- PATH for shared object (library .dll files) is the same of executable (not as with LD_LIBRARY_PATH on Unix).
- Shared object and header files aren't in a well know standard position.


1 - Download Cygwin:
http://cygwin.com/ (tested until version 1.5.25-11 del 2008/03/09)

2 - Install Cygwin. Select at least the following libraries:
Freetype2
Fontconfig
libpng12
Install the devel tools 'pkg-config'
Surely install compiler and make.
If you want to compile 'also' for X11 backend install the
following libraries:
GTK-2.0
ATK-1.0
PANGO-1.0
GLIB-2.0
CAIRO

3 - Download and install GTK+/Win32 Development (sources, include, Glade e pkg-config files):
http://gladewin32.sourceforge.net/  (last is 2.12.9 Revision 1 packaged with Glade2.x or Revision 2 packaged with Glade3.x)
or from my web site: Gtk+/Win32 Developement 2.12.9-1 (.exe, 17M)

4 - From Windows Control Panel, System, Advanced, System Variables,
check the following variables are correct:

DISPLAY=localhost:0

GTK_BASEPATH=C:\Program Files\COMMON~1\GTK\2.0   (best)
GTK_BASEPATH=C:\PROGRA~1\COMMON~1\GTK\2.0  (backup)
GTK_BASEPATH=%CommonProgramFiles%\GTK\2.0  (spaces may create problems)
GTK_BASEPATH=C:\Program Files\Common Files\GTK\2.0   (spaces may create problems)
LIB=%GTK_BASEPATH%\LIB;   (best)
LIB=C:\Program Files\Common Files\GTK\2.0\LIB;     (spaces may create problems)

INCLUDE=
C:\Program Files\Common Files\GTK\2.0\INCLUDE;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\GTK-2.0;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\ATK-1.0;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\PANGO-1.0;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\GLIB-2.0;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\CAIRO;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\LIBGLADE-2.0;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\LIBXML2;
C:\Program Files\Common Files\GTK\2.0\INCLUDE\GTKGLEXT-1.0;
C:\Program Files\Common Files\GTK\2.0\LIB\GTK-2.0\INCLUDE;
C:\Program Files\Common Files\GTK\2.0\LIB\GLIB-2.0\INCLUDE;
C:\Program Files\Common Files\GTK\2.0\LIB\GTKGLEXT-1.0\INCLUDE;

Path=
%SystemRoot%\system32;       (C:\WINDOWS\system32)
%SystemRoot%;                (C:\WINDOWS)
C:\Program Files\cygwin\bin;
%GTK_BASEPATH%\bin;          (C:\Program Files\Common Files\GTK\2.0\bin)
...   (other path)

LANG=it

5 - Start Cygwin Bash e check:
pkg-config --version
report 0.21 minimum.
This is why Cygwin install in:
/usr/bin
C:\Program Files\cygwin\usr\bin
the 0.21, while GTK+/Win32 Devel install in:
/cygdrive/c/Program Files/COMMON~1/GTK/2.0/bin/pkg-config
C:\Program Files\Common Files\GTK\2.0/bin/pkg-config
the 0.20, and with it do not work the option --list-all
Possibly swap in path:
/usr/bin/   and
/cygdrive/c/Program Files/COMMON~1/GTK/2.0/bin/

6 - To make 'pkg-config' work it is necessary configure the system variable used to find packages *.pc files.
echo $PKG_CONFIG_PATH
If empty, use 'export' to add:
/cygdrive/c/Program Files/COMMON~1/GTK/2.0/lib/pkgconfig/;
/cygdrive/c/Program Files/cygwin/lib/pkgconfig/
that are the Cygwin equivalence of:
C:\Program Files\Common Files\GTK\2.0\lib\pkgconfig\
C:\Program Files\cygwin\lib\pkgconfig\

7 - Change as follow all the *.pc files located at:
$GTK_BASEPATH/lib/pkgconfig
/cygdrive/c/Program Files/Common Files/GTK/2.0/lib/pkgconfig
In each the first line is:
prefix=/target
substitute with:
prefix=/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0
prefix=$GTK_BASEPATH   (use the variable value and not the variable itself).
To speed up this process, you can use this simple bash script 'gtkPkgFix'.

7b - If this file miss:
        C:\Program Files\Common Files\GTK\2.0\lib\pkgconfig\pixman-1.pc
   copy the following empty fake pixman-1.pc in:
        /cygdrive/c/Program Files/Common Files/GTK/2.0/lib/pkgconfig
   edit 'pixman-1.pc', change first line to:
      prefix=/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0

8 - Verify to have the following libreries minimum version:
PackageName _________________ LastVers Install_ Description____________________

pkg-config __________________ 0.22     0.21

atk _________________________ 1.12.3   1.12.3   Atk - Accessibility Toolkit
cairo _______________________ 1.4.10   1.2.6    cairo - Multi-platform 2D graph
fontconfig __________________ 2.4.2    2.4.2    Fontconfig - Font configuration
freetype2 ___________________ 2.3.5    9.6.3    FreeType 2 - A free, high-quali
gdk-2.0 _____________________ 2.10.11  2.10.11  GDK - GIMP Drawing Kit (win32 t
gdk-pixbuf-2.0 ______________ 2.10.11  2.10.11  GdkPixbuf - Image loading and s
gdk-pixbuf-xlib-2.0 _________ 2.6.10   2.6.10   GdkPixbuf Xlib - GdkPixbuf rend
gdk-x11-2.0 _________________ 2.6.10   2.6.10   GDK - GIMP Drawing Kit (x11 tar
glib-2.0 ____________________ 2.14.0   2.12.11  GLib - C Utility Library
gmodule-2.0 _________________ 2.12.11  2.12.11  GModule - Dynamic module loader
gobject-2.0 _________________ 2.12.11  2.12.11  GObject - GLib Type, Object, Pa
gthread-2.0 _________________ 2.12.11  2.12.11  GThread - Thread support for GL
gtk+-2.0 ____________________ 2.10.14  2.10.11  GTK+ - GIMP Tool Kit (win32 tar
gtk+-x11-2.0 ________________ 2.6.10   2.6.10   GTK+ - GIMP Tool Kit (x11 targe
gtk-doc _____________________ 1.5      1.5      gtk-doc - API documentation gen
gtk-engines-2 _______________ 2.2.0    2.2.0    gtk-engines-2 - GTK+ Theme Engi
libglade-2.0 ________________ 2.5.1    2.5.1    Libglade - a library for dynami
libpng ______________________ 1.2.12   1.2.12   libpng - Loads and saves PNG fi
libpng12 ____________________ 1.2.12   1.2.12   libpng - Loads and saves PNG fi
librsvg-2.0 _________________ 2.14.4   2.14.4   librsvg - library that renders
libxml-2.0 __________________ 2.6.27   2.6.27   libXML - libXML library version
pango _______________________ 1.16.4   1.16.1   Pango - Internationalized text
pangoft2 ____________________ 1.16.1   1.16.1   Pango FT2 - Freetype 2.0 font s
pangox ______________________ 1.8.1    1.8.1    Pango X - X Window System font
pangoxft ____________________ 1.8.1    1.8.1    Pango Xft - Xft font support fo
xcursor _____________________ 1.1.4    1.1.4    Xcursor - X Cursor Library
xft _________________________ 2.1.6    2.1.6    Xft - X FreeType library

To verify this, you can use this simple bash script 'pkgtk'.

9 - Try the package "pkgconfig" with:
     pkg-config --cflags --libs gtk+-2.0
The output should be something like this:
   -Wall -mno-cygwin -mms-bitfields \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/gtk-2.0 \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/atk-1.0 \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/pango-1.0 \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/glib-2.0 \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/cairo \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/freetype2 \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/include/libpng12 \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/lib/gtk-2.0/include \
   -I/cygdrive/c/PROGRA~1/COMMON~1/GTK/2.0/lib/glib-2.0/include \
   -user32 -Wl,-luuid -L/cygdrive/c/PROGRA~1/COMMON~1/GTK/lib \
   -lgtk-win32-2.0 -lgdk-win32-2.0 -lgdi32 -limm32 \
   -lshell32 -lole32 -latk-1.0 -lgdk_pixbuf-2.0 \
   -lpangowin32-1.0 -lpangocairo-1.0 -lcairo -lpangoft2-1.0 \
   -lpango-1.0 -lm -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 \
   -lintl -liconv -lfreetype -lz -lfontconfig -lpng12

Warning, if it report something like:
   -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 \
   -I/usr/
include/pango-1.0 -I/usr/X11R6/include -I/usr/include/freetype2 \
   -I/usr/include/g
lib-2.0 -I/usr/lib/glib-2.0/include  -Wl,--export-dynamic \
   -lgtk-x11-2.0 -lgdk-x1
1-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 \
   -lpangox-1.0 -lpango-1.0 -lg
object-2.0 -lgmodule-2.0 -lglib-2.0 -lintl -liconv
the GTK founded and used for compilation are 2.6 bundled with Cygwin and not 2.12 from GTK+/Win32 Devel.


10 - Now teorically should work compilation with:
pkg-config --cflags --libs gtk+-2.0
that generate all the compile and linking right options for GTK2.
Example:

gcc -Wall -o target `pkg-config --cflags --libs gtk+-2.0` target.h target.c main.c


11 - If pkg-config does not work, a Makefile with tested options is:

# pkg-config --cflags gtk+-2.0
cflags = -Wall -mno-cygwin -mms-bitfields \
         -I"$(GTK_BASEPATH)"/include \
         -I"$(GTK_BASEPATH)"/include/gtk-2.0 \
         -I"$(GTK_BASEPATH)"/include/atk-1.0 \
         -I"$(GTK_BASEPATH)"/include/pango-1.0 \
         -I"$(GTK_BASEPATH)"/include/glib-2.0 \
         -I"$(GTK_BASEPATH)"/include/cairo \
         -I"$(GTK_BASEPATH)"/include/freetype2 \
         -I"$(GTK_BASEPATH)"/include/libpng12 \
         -I"$(GTK_BASEPATH)"/lib/gtk-2.0/include \
         -I"$(GTK_BASEPATH)"/lib/glib-2.0/include

# pkg-config --libs gtk+-2.0
ldflags = -user32 -Wl,-luuid -L"$(GTK_BASEPATH)"/lib \
          -lgtk-win32-2.0 -lgdk-win32-2.0 -lgdi32 -limm32 \
          -lshell32 -lole32 -latk-1.0 -lgdk_pixbuf-2.0 \
          -lpangowin32-1.0 -lpangocairo-1.0 -lcairo -lpangoft2-1.0 \
          -lpango-1.0 -lm -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 \
          -lintl -liconv -lfreetype -lz -lfontconfig -lpng12

target: target.o marshallers.o
    gcc target.o marshallers.o -o target $(ldflags)

target.o: target.c
    gcc $(cflags) -c target.c

marshallers.o: marshallers.h marshallers.c
    gcc $(cflags) -c marshallers.c

marshallers.c: marshallers.list
    glib-genmarshal --prefix _marshal --body $< > $@

marshallers.h: marshallers.list
    glib-genmarshal --prefix _marshal --header $< > $@


N.B. In the example I used 'glib-genmarshal' that starting from:
marshallers.list generate marshallers.h and marshallers.c

There are two version of glib-genmarshal:
/cygdrive/c/Program Files/COMMON~1/GTK/2.0/bin/glib-genmarshal --version
glib-genmarshal version 2.12.11
and
/usr/bin/glib-genmarshal --version
glib-genmarshal version 2.10.3
use the newer.


12 - if you want to test your libraries installation and compiler configuration, you can download this minimal source, but use the Cairo and so the GTK+ 2.8+. If it compile, you can be sure that complex application compile as well. The compile line is:
gcc -Wall -O3 -lm cairo.c -o cairo `pkg-config --cflags --libs gtk+-2.0`


Additional notes:
-----------------
If you prefer a visual Widget GUI Builder use Glade (Linux and Win32)
If you prefer an editor and compiler integrated IDE environment use Anjuta (Linux only)
If you prefer an editor and compiler integrated IDE environment use Dev-C++ (Win32 only)


Note on generation using Glade 2.x:
-----------------------------------
To compile using Glade are required some other tools packaged with Cygwin:
autoconf
intltoolize
xml-i18n-toolize
libtool
glib-gettextize
automake
aclocal

To generate the GUI sources, press Build button in Glade2, then go to project root and run
'autogen.sh' to generate the Makefiles.
Then run 'make'

N.B.1 if sources use ?printf, in src/Makefile it is necessary to add in link options:
<projectname>_LDADD: -L/usr/lib/mingw -lmsvcrt
otherwise it can't resolve the symbol '__imp___iob'
You can also add it to src/Makefile.am at line: _LDADD

N.B.2 if sources use ?printf, and in link stage you got an error for:
undefined reference to '___printf__'
it is necessary to add '
-mno-cygwin' in src/Makefile:
CFLAGS = -g -O2 -Wall -mno-cygwin
clean and rebuild all.

N.B.3 If you install also MinGw/MSYS or Dev-C++ you need to have those option in src/Makefile.am:
<projectname>_LDADD = @PACKAGE_LIBS@ $(INTLLIBS) -L/usr/lib/mingw -lmingwex -lmsvcrt
AM_CFLAGS = -mno-cygwin

N.B.4 To add some other compile options, add them to:
src/Makefile.am at line AM_CFLAGS =


Note on contents of Glade generated files:
------------------------------------------
To add, at a later time, other file to the project, modify:
src/Makefile.am in section <projectname>_SOURCES


Packaging:
----------
The builded executable can be packaged with NSIS, a professional opensource installer:
http://nsis.sourceforge.net/Main_Page
so the Win32 only users can easily install your application.
You have to package:
1 - the executable
2 - the <Program main directory>\pixmaps directory
3 - if you do not used '-mno-cygwin' compile option, you have to package the file 'cygwin1.dll' and let the installer put it the <Program main directory>.
(if you want, you can put it in the \Windows\System32 directory, but, if you do that, you have ALWAYS to check before the presence of a newer .dll already there!)
4 - the GTK+ runtime. Work with both the following runtime:
http://gladewin32.sourceforge.net/
http://gtk-win.sourceforge.net/home/index.php/en/Home
You can choose to keep the GTK+ runtime separately to keep the package size smaller.
For point 3 and 4, do not forget to link the source web site, or better distribute the sources of that packages, as the GNU GPL impose.


Useful links on GTK+, Glade, Cairo, GUI crossplatform developing

Why use GTK+ v2.8+ and Cairo v1.4+ to develop GUI crossplatform applications


Go to EFA programming page
Back to main page:  EFA home page

Web site showed  times since 01/04/2000. 
From 20/10/1999 - 31/03/2000 the counter showed 770 visits, but it was resetted from the provider (so add 770 ...)

Copyright (C) 2006,2007,2008  Valerio Messina http://users.iol.it/efa
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License,
Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts
and no Back-Cover Texts.  A copy of the license is included in the section entitled "GNU Free Documentation License" or
can be downloaded at: http://www.gnu.org/licenses/fdl.html