´╗┐Building cross-platform GTK+ applications with MinGW and C/C++ - HOWTO

Author: Svetoslav P. Chukov <hydra@nhydra.org>

Table of contents


  1. Introduction

  2. Description of used Libraries.

  3. Install the necessary tools and libraries.

  4. Process of compiling.

    • 4.1 Compilation of source code via "./configure" and "make".

    • 4.2 Manual compilation.

  5. Copyright.

  6. License.


  • 1.Introduction.

This Howto will describe how to install, build and run GTK + applications under both Windoze and Linux.

2.Description of used Libraries and tools.

MinGW - MinGW ("Minimalistic GNU for Windows") refers to a set of runtime headers, used in building a compiler system based on the GNU GCC and binutils projects. It compiles and links code to be run on Win32 platforms... providing C, C++ and Fortran compilers plus other related tools. If you see references to "mingw32" instead of "MinGW", they are referring to the same compiler system. The project's name changed from mingw32 to MinGW is to prevent the implication that MinGW will only works on 32 bit systems (as 64 and higher bit machines become more common, MinGW will evolve to work with them). MinGW uses the Microsoft runtime libraries, distributed with the Windows operating system. Unlike other ports of GCC to Windows, the runtime libraries are not distributed using Gnu's General Public License (GPL). You, therefore, do not have to distribute your source code with your programs unless, of course, you use a GPL library in your programs..GTK+ - GTK+ is a multi-platform toolkit for creating graphical user interfaces. Offering a complete set of widgets, GTK+ is suitable for projects ranging from small one-off projects to complete application suites.GTK+ is free software and part of the GNU Project. However, the licensing terms for GTK+, the GNU LGPL, allow it to be used by all developers, including those developing proprietary software, without any license fees or royalties. GTK+ is based on three libraries developed by the GTK+ team:

GLib is the low-level core library that forms the basis of GTK+ and GNOME. It provides data structure handling for C, portability wrappers, and interfaces for such runtime functionality as an event loop, threads, dynamic loading, and an object system.

Pango is a library for layout and rendering of text, with an emphasis on internationalization. It forms the core of text and font handling for GTK+-2.0.

The ATK library provides a set of interfaces for accessibility. By supporting the ATK interfaces, an application or toolkit can be used with such tools as screen readers, magnifiers, and alternative input devices.

GTK+ has been designed from the ground up to support a range of languages, not only C/C++. Using GTK+ from languages such as Perl and Python (especially in combination with the Glade GUI builder) provides an effective method of rapid application development.

3.Install the necessary tools and libraries.

The core GTK+, Glib and Pango source and binary archives can be downloaded directly from the GTK.org ftp server. These are links about:

GTK+ -ftp://ftp.gtk.org/pub/gtk/v2.10/win32

Glib -ftp://ftp.gtk.org/pub/glib/2.12/win32

Pango -ftp://ftp.gtk.org/pub/pango/1.16/win32

There are also the all dependencies required of the above libraries.You should You have to download all the libraries and put them in one directory. Then you should extract them one by one in the same directory. So, to be easy you just use one directory for the whole source code of entire package list. This is not so good but is rather simle way to compile your GTK+.

There is very comfortable installer and packages for GTK+ windoze version than can be downloaded from www.nhydra.org. Tarball and installer available are available there. So, download the tarball or the installer. If you download the tarball just untar the archive. If you download the installer just install it and then you will have working GTK+ libraries for you. Enter in the folder where you have installed the package. There should be the following directories:

  • bin contrib include lib make man manifest share test.

Important folders are include and lib. These are the folders with headers files and libraries. Then proceed to MinGW. Available version can be found on www.mingw.org and installer from www.nhydra.org. Install or untar the archives and proceed with the real work.

4. Process of compiling.

The compilation is simple. mingw-gcc is command replacement for gcc in the MinGW world. The options and commands are exactly the same as the Linux/UNIX versions. So, to compile a program called foobar with 3 source files - foobar1.c foobar2.c foobar3.c type this. mingw-gcc foobar1.c foobar2.c foobar3.c -I /your-include-dir -L/your-lib-dir --mmbitfields -o foobar-programThis command compile your source files and produce one exe file called foobar-program.

4.1 Compilation of source code via "./configure" and "make".

There is two ways to compile an application. Via "make" and manually executed mingw-gcc ocmmand. In this example I assume you have source code generated by Glade Interface Designer.

After that open the file "Makefile.am" and add library and include paths for the GTK+ library. This can be done in that way. If the content of "Makefile.am" is this :

  • INCLUDES = \ -DPACKAGE_DATA_DIR=\""$(datadir)"\" \ -DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" \ @PACKAGE_CFLAGS@

Add additianal line with the path to GTK+ like this:

  • INCLUDES = \ -DPACKAGE_DATA_DIR=\""$(datadir)"\" \ -DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" \ @PACKAGE_CFLAGS@\ /my-dir-gtk-headers

Then go to the line with the LIB options. This is the example code for it: gtk_test_LDADD = @PACKAGE_LIBS@ $(INTLLIBS)

Change it to this:

  • gtk_test_LDADD = @PACKAGE_LIBS@ $(INTLLIBS)\ -L/my-dir-gtk-libraries\ -lgtk-2.0 -lglib-2.0 -gthread-2.0

After all these changes the compiling may begin. So, just follow this steps to compile your windoze executable:

Enter in the project directory of you program and run ./autogen.sh as usuall for any glade generated source code. This is ordinary step needed for any program. No extra stuff here.

Then you have to run ./configure command. This is usual steps in configuring of Linux/UNIX programs. When you have configured you are ready to compile source codes .... then just type the usual command

  • "make" but you have to specify your windoze compiler. In that case is MinGW GCC. So, you just run "make" like that(without the quotes): "make CC=i386-mingw32-gcc". Or if you should compile C++ program you could specify "i386-mingw32-g++" instead "i386-mingw32-gcc".

4.2 Manual compilation.

The most simple way to compile an application is just to pass source and header files and paths to the gcc via only one command. So, If you have libraries available in directory "/my-dir-gtk-lib" and you have include files in "/my-dir-gtk-include" just execute this command in the command shell.

  • i386-mingw-gcc source-file.c -o my-gtk-app.exe -I/my-dir-gtk-include -L/my-dir-gtk-lib -lglib-2.0 -lgtk-2.0 -lhtread-2.0

That's it! After compilation process done you will have executable called "my-gtk-app.exe".

5 . Copyright.

Copyright (c) 2007 by Svetoslav P. Chukov. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 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".

6. License

  • Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 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".

Building cross-platform GTK+ applications with MinGW and C/C++ (last edited 2008-02-03 14:45:53 by localhost)