Compiling GTK+ apps on Snow Leopard x86-64

So for a while now I have been wanting to write GUIs in C using the GTK+ toolkit, but I have been unable to do so on my Macbook Pro. I googled the matter and saw something about GTK-OSX, which I tried to install but failed to do so, due to dependency issues or the like. I got Qt working with C++, but I didn’t want to write C++. So I gave up on GTK+ and put it off for the future.

Just today I got GTK+ working, with a catch: at the moment I can only compile for 32-bit applications. This is annoying, as I have a 64-bit architecture and I want to write 64-bit applications. However, 32-bit apps can run on a 64-bit architecture, so I’ll take what I can get.

Here’s what I did:

First, I installed GTK+ and GTK+2 using fink. You can find out the GTK packages on fink by typing:

fink list gtk | less

You’ll see that gtk+ and gtk+2 are listed. Install them with

fink install gtk+

fink install gtk+2

Now that GTK is installed (it will show up in /sw/include and /sw/lib like almost all other fink installs) we can write a quick hello world application. I stole the code from here.

// hello.c

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
	GtkWidget *window;
    
	gtk_init(&argc, &argv);
	
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 	gtk_widget_show(window);
    
    	gtk_main();
	
	return 0;
}

Now it’s time to compile. Since GCC doesn’t know GTK’s whereabouts, we have to specify it with some include paths and library paths. We can get these paths by typing

pkg-config --cflags --libs gtk+

We’ll now set up an environment variable to contain the output of that command. Edit ~/.profile so that you have in it

export GTK="`pkg-config --cflags --libs gtk+`"

We should now be able to compile (theoretically). Type

gcc hello.c -o hello $GTK

You’ll probably get the error messages

ld: warning: in /sw/lib/libintl.dylib, file is not of required architecture
ld: warning: in /sw/lib/libglib.dylib, file is not of required architecture
ld: warning: in /sw/lib/libgmodule.dylib, file is not of required architecture
ld: warning: in /sw/lib/libgdk.dylib, file is not of required architecture
ld: warning: in /sw/lib/libgtk.dylib, file is not of required architecture
Undefined symbols:
"_gtk_main", referenced from:
_main in cc80Plip.o
"_gtk_widget_show", referenced from:
_main in cc80Plip.o
"_gtk_init", referenced from:
_main in cc80Plip.o
"_gtk_window_new", referenced from:
_main in cc80Plip.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

That’s because the GTK files that pkg-config gives us are made for a 32-bit architecture. For some reason, pkg-config doesn’t give us the gtk-2.0 files which I suspect would be for x86-64. You’ll see if you echo $GTK that we instead include glib-1.2 and gtk-1.2. So we have to compile under a 32 bit architecture.

The following should compile and run without problem:

gcc -m32 hello.c -o hello $GTK

./hello

Well that’s it, and I’m very happy to finally be able to compile GTK apps on OS X. (Even if I’m stuck writing 32-bit apps…)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s


%d bloggers like this: