Compiling GTK+ programs from the terminal: Part 1

I installed Ubuntu onto the new hard drive over lunch. Now I’m trying to get my development environment up and running again. Much of my Linux-specific programming was done to learn GTK+, a very popular GUI toolkit. The IDE I use, CodeBlocks, really likes GTK+ 2, but the world has moved on to GTK+ 3, and so must I.

Unfortunately, CodeBlocks doesn’t quite know what to do with GTK+ 3 right out of the box. I spent a great deal of time figuring out how to make it work last fall. This (error filled) post on my old journal details some of that struggle: http://chrislearnsc.blogspot.com/2011/10/compiling-gtk-30-programs-in-codeblocks.html

This year I’ve spent a lot of time learning Git and getting familiar with the command line. My current work-flow isn’t dependent on an IDE anymore for doing the compiling. So, here’s the punchline:


gcc `pkg-config --cflags gtk+-3.0` program.c -o program `pkg-config --libs gtk+-3.0`

This took a long time to get right because I didn’t know that the order actually mattered. I can swap the place of some stuff, like it will accept “-o program program.c” instead of “program.c -o program”, but it won’t accept rearranging the pkg-config parts. This is what I got terribly wrong last fall, and why I had to use the output of pkg-config as a whole in the build options.

A quick note – Remember, if you go to a terminal any type:

>>pkg-config –libs gtk+-3.0

you get…

-lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0

…and putting that command in the backticks in the gcc command line is a substitute for all of those -lxyz parts.

So I now know how to structure the gcc command to get what I want. I’m mad that I ran into two conflicting sources of info in the GTK documentation. The documentation I thankfully stumbled on that was correct is here: http://developer.gnome.org/gtk3/3.0/gtk-getting-started.html and the bad info (which is just a slightly rearranged way of arranging the compile command) is here: http://developer.gnome.org/gtk/2.24/gtk-compiling.html

In hindsight I realize that maybe it’s not a fair comparison. The “incorrect” example is for GTK+ 2, and uses cc instead of gcc, so maybe one or both of those are relevant. I’m going to make a mental note to see what the rules are for the order of compile flags and library lists in gcc.

I called this post “Part 1” because eventually there will be a “Part 2” (hopefully tomorrow) where I use Make to do the dirty work. Up until this point none of my projects have been involved enough to warrant Make, but my recent experience with my Project Euler solutions has somewhat shown me the light.

This entry was posted in Hobbies, Programming. Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s