Archive for the ‘Programming’ Category

Compiling GTK+ apps on Snow Leopard x86-64

March 27, 2010

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);
	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


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 bot

March 25, 2010

I am writing a bot in C. As I said before, I wrote one in Java two summers ago, and I wrote a telnet bot in Java (when I was first learning programming) in high school. I never learned VB so I didn’t write any Clean Slate bots 😛

I now want to write a nice command-line bot in C. Maybe I’ll make a GTK+ GUI at the computer labs. I have already worked out the design and I am writing the implementation as I speak.

irc client version 1.1

March 24, 2010

I’ve abandoned my intentions of learning C++ and Qt, because frankly I don’t want to learn C++ at the moment — it is too unwieldy. I am very fond of the C language and comfortable writing code in it, so I am going to continue to write in C primarily. I plan to write a bot in C in the near future, but right now I’m more inclined to learn GTK+ so I can write C GUIs. Unfortunately my laptop runs OS X, and it’s not easy to develop GTK applications in OS X… I will probably end up learning it on the Linux computers in my school’s computer lab.

After returning to C I revisited my irc client, and wrote a second version: version 1.1. (Version 1.0 is the first.) The major difference is that 1.1 is single-threaded via the use of select(). Multithreading is quite simply bad practice in C since the language was not designed for parallelism. I am quite glad that select() went so smoothly, and that my program is now single-threaded. I am also quite pleased with my ring buffer — both select() and the ring buffer were suggestions I received in ##C.

Here is the code to my irc client 1.1: ircc1.1.

Tomorrow I’m going to the computer lab to learn GTK+.

New Project

March 11, 2010 bot in C++. The first one I did was in Java, and only connected over telnet. This one had a GUI. The second one I did was in Java, and connected as a Starcraft client. Unfortunately, I never got around to making a GUI for it, and I also didn’t bother to implement Warden.

So this time I plan to make a complete one, in C++ with a GUI, using the Qt framework as my widget toolkit. Maybe I’ll implement Warden… but since it doesn’t hamper War 2 logons, and I only have a War 2 key, I don’t really see the point. Perhaps for learning. But first I’ll just make the bot with its GUI, and learn C++ and Qt in the process.

I just learned today that a lot of glorious applications were written in C++/Qt. These include: Google Earth, Adobe Photoshop Album, Player, Mathematica, Opera, Skype, TeamSpeak, VirtualBox, and VLC Media Player. This really piques my enthusiasm for learning Qt 😛

Steps 2 & 3: Threading and Packet Handling

March 11, 2010

Multithreading didn’t turn out too bad. Actually, it was painless. But that’s because I did it the lazy way — I haven’t gotten around to making it thread-safe. So it sometimes happens that when you’re sending text you all of a sudden get a message received print out in the middle of your text, messing up the display of text on terminal. For personal use I can certainly live with it, but for the sake of learning threading I should learn how to use mutexes and semaphores.

Packet handling was all about string processing, which isn’t exactly C’s forte. I ended up using a mixture of strcat, strtok, and ptr++ to parse the packets I received. I also created a map that maps the string identifiers of messages in RFC to the integer constants I define for each RFC message, so this way I can switch the packet with ease.

Here is the working code. I plan to improve it later on by adding more functionality as well as thread synchronization.

Multithreading in C

March 5, 2010

This is going to be a nightmare.

Step 1: Joining a Channel

March 5, 2010

Well tonight I finished the first stepping stone: getting the program to connect to IRC and join a channel. Here’s the code and here’s some screenshots: 1 2 3. You can see the bot listed as enderx86b in the second screenshot.

Current Project

March 5, 2010

IRC Bot, written in C. I just wrote this echo server to help myself learn C socket programming. I’ll be implementing the IRC protocol documented here. Will write updates.

First I’ll do a single-channel command-line program, and then maybe I’ll write a GUI in Cocoa. Unfortunately, I feel very disinclined towards Objective-C, it just strikes me as ugly =/