Published on MacDevCenter (
 See this if you're having trouble printing code examples

Graphical Toolkits for Apple's OS X: GTK+

by Jeremiah Foster

This series of articles presents an overview of graphical toolkits for Apple's OS X, and this article deals with GTK+ (GIMP ToolKit). OS X is a modern POSIX-compatible operating system with arguably the best human-computer interface ever created. There are other GUI toolkits available aside from Apple's set of tools, and a number of good reasons to use them together with the famed Apple interface.

For example, you may want to bring a Unix or Linux application to OS X. Perhaps you have a favorite application you cannot find with fink or Darwin ports or one of the other porting tools. With the graphical toolkits, I hope to show that you can port the code from that application quite easily and bring it to the Mac. Or perhaps you know a programming language like Python, Perl, or even PHP from your web development experience and now you want to develop a desktop application. These tools will allow you to use your favorite programming language and leverage the time and effort you have already invested in learning a scripting language. Lastly, it is fun, and not too hard, so this might be a great way to teach yourself how to program for your favorite operating system.

The official GTK+ site describes GTK+ this way:

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+ 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.

Before we get too deep into building our sample application, we should clarify our terms and tools. Firstly, when I say "GTK+" I mean the latest, maintained version of GTK+, called GTK+2, for Mac OS X Tiger (i.e., 10.4.9). It is important to note up front that the build script for GTK+ currently only works with Mac OS X Tiger. I have tried it on earlier versions of OS X and it does not work. If you have tried, or want to try, to get it to work with an earlier version of the OS please post your solutions to the forum at Imendio. Imendio is hosting this project and has provided us with a shell script we can use to build GTK+ for testing and trying out at

You'll note that the above link does not go to a .dmg file. This is because GTK+ on the Mac is in its early stages, it is bleeding-edge technology. If you are a developer, or a Linux hacker, you know what bleeding-edge means; it means it might not work and you might make a mess of your computer, which might take a while to clean up. It also means there might be someone to help you if you get stuck or there might not. There are no guarantees. So if you are feeling brave and want to get your hands dirty, then GTK+ is for you, otherwise you might wait until the project stabilizes a little and a disk image is created. But since we are hackers, lets hack a bit.

Since we are going into some pretty deep, uncharted waters, we need some heavy-duty tools. For example, the build script provided by Imendio requires git, the tool developed by Linus Torvalds, to get some source code. We need to have git ready to run when the build script calls it. There are pre-built binaries for Mac OS X which are available from macports. If you have macports installed, getting git is as easy as:

sudo port install git-core

If you prefer to build from source, you can select your preferred version and get git at . I chose to use 1.5.0, but you can use a more recent version if you want to, it's up to you. Once we have downloaded the tarball (git-1.5.0.tar.bz2 in our case), we have to unpack it. To do that you can use this command:

tar xjf git-1.5.0.tar.bz2

The next thing we have to do is build the source code for our computer into a binary. Do that with the command:

make prefix=/usr/local/ all

and then we use sudo to issue the next install command:

sudo make prefix=/usr/local install

You can use a tool called asciidoc to make the documentation, but I chose not to do that as it would distract us from our mission. Once you have built the source code, just test to see if it is working by calling git:


You should see output showing various git commands, like this:

usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add           Add file contents to the changeset to be committed next
   apply         Apply a patch on a git index file and a working tree
   archive       Creates an archive of files from a named tree
   bisect        Find the change that introduced a bug by binary search
   branch        List, create, or delete branches

If you see git information dumped to your terminal screen, you know your installation of git has been successful. You can use git to manage source code you have, but that is not the subject of this article so I will leave it to you to research if you're interested. The build script also requires Subversion, despite that it does exactly what git does, but since in the world of Free Software wheel re-invention is endemic, one has to learn to love it. Fortunately, Subversion is quite useful and available through fink:

fink install svn-client

You can test your Subversion installation like this:

svn help

This should dump out a bunch of help commands you can use with Subversion. Okay, now on to building GTK+. Since we have all the tools we need (i.e., git and Subversion), we can download and build GTK+ from the Imendio build script. Of course, since this is a shell script, we can modify it if need be, and as good hackers we cannot leave well enough alone. So we'll edit the source code so that it installs software in /sw/gtk instead of /opt/gtk, which seems unnecessary to me. Even if you do not decide to do this, check the write permissions on the default directory the build script uses, /opt. This is because you are going to run the build script as yourself and you need to have write permission on the /opt directory and below. If you do not have /opt simply make it with:

sudo mkdir /opt

and set the ownership with:

sudo chown -R $USER /opt/

Once we have done that, we run the script:

./gtk-osx-build bootstrap

This installs yet another set of tools needed, like autoconf and automake and dependencies (essentially libraries of software which will be needed by GTK+ that are not yet present on our system). This will output lots of make and configure information since you are compiling lots of software. You will also see output from curl as it gets various code archives. Bootstrapping actually takes awhile, even on my Intel Core Duo, so one has to be patient. You can read the code of the shell script to determine what exactly is happening. For example, this function simply checks the basename of the application passed to it and then goes and downloads it:

Function download
    BASENAME=`basename $1`

    if [ -s $BASENAME ]; then
    echo "Already downloaded"
    return 0

    echo $1
    curl $1 > $BASENAME || return 1

    return 0

Once we have bootstrapped and downloaded all the needed software, we can go forward and build it. Call the same build script, but this time pass the argument build:

./gtk-osx-build build core

We are interested here in just the core tools to build GTK+ on the Mac, any other software can be added later if necessary. That is why we pass the argument core along with build to the build script. The script then goes out and uses git to clone a Cairo repository and builds it. This can also take quite awhile and you can watch as the script checks out source code for GNOME-common, GLib, ATK, Cairo, Pango, and GTK+, then compiles and installs these. This take a long time because you are building very large software packages--in essence, you are building a mini version the GNOME desktop on the Mac, which should give you some idea of how large this package of software is.

Once we have compiled everything, that is once the build script has stopped and there are no errors, then we can go onward and build our GTK+ program. (If you have errors, which is not at all unlikely given the nature of the software, first run the build script again. If you still have problems, you may want to post them to the Imendio forum to see if anyone else has had a similar problem or if someone can help you. You'll see many posts from me as I tried to get GTK+ to work.)

We'll also need to make sure that the Perl module Gtk2 is installed since we are going to be using Perl to create our application. If you have CPAN installed, this is child's play. Simply drop into CPAN and say:

cpan> install Gtk2

CPAN provides the connection between the libraries we install and Perl. This means we can now call the libraries directly from Perl making them do what we want in Perl itself.

You may have to make sure your PATH information is correct since CPAN will look for tools like pkg-config, and we need to make sure that all of the Perl modules we need will be available as well. This means that we have to install Cairo, GLib, and Gtk2--as well as their dependencies--so that we can call these modules from within our Perl program. These modules serve as the glue between the software libraries we installed and the Perl programming language. Therefore, if you are interested in using Gtk2 with another programming language, you can skip this step.

However since we are using Perl, we'll drop into CPAN to get the necessary modules: ExtUtils::Depends, ExUtil::PkgConfig, GLib, and Gtk2. There were some other dependencies CPAN asked me about that I told CPAN to download as well. Finally, I needed to force Gtk2 to install while in CPAN and as root. This is sub-optimal. Firstly, dropping down into root should not be required and there are settings in CPAN to set make to sudo make to avoid dropping to root. I did this to no effect on my Mac. Secondly, all the tests for Gtk2 failed, so I had to force the installation. Hmm, maybe a bug report is required here? Yes, I think it is, and I have gone off and filed GNOME bug #438440. It is always good to file bugs since it really helps developers, and since a lot of users are anonymous, it shows developers that people are using their software. The cliche that says your only audience is your critics is really true, at least in the software world.

Now, after all the bug reporting, software installation, tool installation, and Perl module downloading, we should have a complete kit ready to go. Lets grab some code and run it to see if the whole thing works. Here is a hello world snippet:


use Gtk2;


$window = Gtk2::Window->new('toplevel');



Save this code to a file, call the file whatever you want. Make it executable and run it:

chmod 755 file_name

Hello World screenshot
Figure 1. Hello World screenshot

You can run it from the command line by typing:


This says "run this program right here." You should see a small window in the left hand corner of your screen that has the name of your program in the title bar. This is obviously a very simple program but with a few code modifications it can turn into a much larger application. The best way to develop the application further is to follow along with the GTK+ tutorial, which also focuses on Perl. There is also a C tutorial if you prefer to use C to program your GTK applications.

I hope you enjoyed this article and I hope it gives you inspiration to build you own programs. Have fun, and stay tuned for the next article in this series on Qt.

Jeremiah Foster fell in love with the Mac years ago. One day, when his new computer had an application called "Terminal," his life was changed. Some years and UNIX certification later, he is still in love with his Mac.

Return to Mac DevCenter.

Copyright © 2009 O'Reilly Media, Inc.