oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Graphical Toolkits for Apple's OS X: GTK+
Pages: 1, 2

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.