Wednesday, October 29, 2008

Getting a "Hello World" X Client to Compile and Run on Ubuntu 8.04 ("Hardy Heron")

Here's how I barely managed it, being a total n00b.

First a little background. Most Linux variants use X Window for GUI, so those who want to try out X for fun (ie, "Aaargghh, it's in the effing syllabus!") can use the nearest Linux box, which might well be Ubuntu these days, as it is for me. The GNU C Compiler is called gcc (of course), and to compile a c file, say hello.c, you can start a Terminal (command prompt) and type:

gcc -o outputfile hello.c

if you type "gcc hello.c", the output goes to "a.out" by default.

So I start with a rather fresh install of Ubuntu 8.04 LTS and the tutorial from (http://tronche.com/gui/x/xlib-tutorial/). I am pleased to find that gcc was installed by default, because the last version I tried didn't have gcc, and I just happened to be stuck without an internet connection then. Great. But this time, when I try to compile anything c or c++, I still end up with errors. Turns out that the one-cd install of Ubuntu installs gcc, but doesn't install the library headers for anything. So unless you have internet access good for a few MBs download, you may be out of luck if you have my set up.

The first step is to install the package "libc6-dev". Go to System > Administration > Synaptic Package Manager. Search and download-install libc6-dev. Now you can compile simple c applications.

For c++, you will need to install the package "g++" (the source file extension .cc is for c++ files--renaming to .c might be useful for the aforementioned tutorial).

You'll also need to find the package for X11 development. Search in Synaptic for "libx11-dev" and install it. (I also added libxcb1-dev, but that is probably unnecessary--I haven't checked :P)

Still the program doesn't compile because, as I find out, you need to reference the X library for the linker:

gcc -lX11 prog2.c

The "-lX11" option links to the X11 library. Finally, the program compiles and shows a window. Whew!

***

Hello World!

What the post title says. I expect to post random, vaguely technological stuff here, so please bear with me.

Here's hoping that someone may find something useful here sometime!

***