Compiling Programs from Source

For the beginner, just the mention of compiling a program from source seems off-putting enough. The words compile and source seem to instill a sense of foreboding in the heart of the new user. That certainly was the case for me, anyway.

However, this method of installing programs is a lot easier than it sounds. In fact, now that you have some experience using the Terminal and command line, it is just plain easy—a sort of one, two, three, and you’re done process.

Of course, you can live long and prosper without ever bothering to compile anything on your system. You can move along quite happily with your system as is, or you can just install programs by means of the very convenient Synaptic application.

So why would you want to bother compiling programs from source? Well, although there are a lot of DEB packages that Synaptic can ease into your system, not every bit of software in Linuxdom is available in that file format. Or perhaps you want the newest version of the software you have your eyes on, but the version available via Synaptic is a slightly older version. Of course, there is that good old human nature factor at play as well—there may well come a time when curiosity gets the better of you, and you will want to move just one step beyond the way you’ve grown accustomed to doing things. After working through this short project, you will be able to do just that, and I am sure you will find that your initial worries will have been for naught.

What Is Source?

In order to understand what source is, you should understand a bit about how a program actually gets from its primitive state on the programmer’s computer to an up-and-running application on your machine. First the programmer writes a program in a programming language. You have probably heard of programming languages such as BASIC or C, and there are many others. What the programmer actually writes with such a language is a set of instructions called the source code, or source. Your computer, however, cannot actually understand any of that source on its own. It is as if the computer speaks ancient Greek, and the source code is all written in French. The computer therefore needs some sort of interpreter to help it out.

The various languages that programmers use are called high-level languages—they are relatively easy for programmers to read. The computer, on the other hand, only understands low-level languages, which are quite difficult for most mere mortal programmers to deal with. To convert the high-level language instructions to a low-level language, the computer needs some other program to translate.

This can be done while a program is running, in which case the translator program is called an interpreter. Applications that run using an interpreter are usually scripts.

The problem with such scripts is that they can be slower than most of the applications you’re familiar with because the computer must run an interpreter, interpret the source code, and run the actual application all at the same time. This is like having a French book translated into Greek by a live interpreter—very slow indeed.

As an alternative, most programs use a compiler instead of an interpreter. A compiler translates the high-level source code into low-level machine code, or object code, that the computer can understand before the application is actually run. Once this translation is done, the computer never has to bother with the high-level instructions again; it can merely read the translated version each and every time it runs the program. This is like having a translated version of a foreign book that you can read any time you want to. Because computers can run compiled programs without simultaneously using an interpreter, compiled programs run faster than scripts. Most applications for all operating systems are, therefore, compiled.

Tarballs: The Containers of Source

Almost all source packages come in the form of tarballs (tarballs, DEBs, and even the RPMs, such as those used in Red Hat–based systems, are all referred to as packages, which is why the icon for such files looks like a little parcel-post box). Tarballs consist of a group of files that have been archived into a single file, which is most often compressed to save disk space, much like Zip files on Windows systems or SIT (StuffIt) files on Macs. In Linux, the most common method of creating such archives is through the tar program, from which the tarball gets its name, while the compression of that archive is usually accomplished by means of the gzip program. Compressed tar files, or tarballs, can thus be recognized by their file endings, which are .tar.gz, or when compressed with the bzip program, tar.bz2.

The files archived in tarballs can be extracted by using the command line, but to keep things easy in this chapter, you can just use the simple double-click method in the GUI, if you prefer.

The Basics

As I mentioned before, the process of compiling an application from source and then installing it is actually simple. Basically, after untarring the source tarball, you would use the following commands to accomplish the task:

./configure To configure a makefile, which provides instructions for the make command

make To translate the source code into object code that the computer can understand

sudo make install To give yourself write privileges in protected folders and then install the application

make clean To clean up the leftovers once the process is complete (to clean up the mess)

Installing the Tools You Need

Because Ubuntu is designed with the average computer user in mind, it does not come with the various applications and libraries you need to compile applications from source. Fortunately, however, just about everything you need to get the job done is available via Synaptic. To get ready for the work at hand (and many other jobs you are likely to do on your own in the future) perform searches for and install the following packages:

·  build-essential

·  checkinstall

·  libgtk2.0-dev

I should mention that although there are only three packages you need to mark for installation, Synaptic will download and install quite a few more necessary packages in addition to these—a total of 29 packages, to be exact.

Project 1A: Compiling and Installing Xmahjongg

To get some hands-on experience with compiling a program from source, you will be working with a game called Xmahjongg, which you can see in Figure 1-1. If you’ve tried out the version of Mahjongg that comes with your Ubuntu distribution, you will notice that this one is much easier on your eyes and is a bit more colorful (check out the project site at www.lcdf.org/xmahjongg to catch a glimpse of it in its full four-color glory).

Figure 1-1: The Xmahjongg game

In case you are not already familiar with this genre of Mahjongg game, the idea is simple enough. Each tile has an image and there are four of each tile in the pile. You must match pairs of like tiles that are open on at least one side. When you click the two matching open tiles, they will disappear. The object of the game is, thus, to remove all the tiles from the board: A very simple solitaire game.

1A-1: Downloading and Extracting the Xmahjongg Files

To get started, you will first have to download the Xmahjongg source code. You can get this from the Xmahjongg project page at www.lcdf.org/xmahjongg. Download the tarball xmahjongg-3.7.tar.gz or a newer version if there is one. Do not download any of the other file types available on that page.

If you prefer, you can instead download the Xmahjongg tarball by using the wget command. Just open a Terminal window, type the following command, and then press ENTER:

wget http://www.lcdf.org/xmahjongg/xmahjongg-3.7.tar.gz

Once you have the file on your hard disk, untar the xmahjongg-3.7.tar.gz file either via the command line, or via the simple double-click method. To make it easier for you to follow along with the directions I’ll be giving you, be sure to place the untarred Xmahjongg folder in your home folder. Then you will be ready to roll.

To extract the tarball, type the following command, and press ENTER:

tar –xvf xmahjongg-3.7.tar.gz

Normally at this point, you would look through the folder to find some instructions for dealing with the package. In most source code packages, this information is included in an INSTALL file like that in the xmahjongg-3.7 folder (Figure 1-2). To read the INSTALL file, just double-click it, and it will open in Gedit.

In this case, you can simply close the INSTALL file, as it prescribes the same steps as I’ve listed below. However, in the future, when you install other programs from source, you will need to follow the instructions in the INSTALL files that come with the source files for those programs. With most INSTALL files, the instructions will match those that follow, though I would suggest using the alternative method I’ll be presenting as a substitute for the make install step.

Of course, it may well occur that you take a look at the contents of the INSTALL file and start wondering what alien tongue it is written in. In such cases when you have no idea what the INSTALL file is going on about, which sometimes happens, just look for a configure file in the package folder. If you find one, then just try doing things the way you will learn in this project.

Figure 1-2: Identifying a package’s INSTALL file

1A-2: Running configure and make for Xmahjongg

Now that you have downloaded and untarred the Xmahjongg tarball, installation is pretty standard, though we will be finishing up in a slightly different way. Here’s what you need to do:

1. Open a Terminal window, and then move into the new folder by typing cd xmahjongg* and pressing ENTER.

The next step is sort of a setup phase that runs the configure script in the xmahjongg-3.7 folder. The configure script checks what files, compilers, and other things it needs, and then it searches your computer to see if those things are there, and if so, where. Based on this information, it writes a file called a makefile, which is a set of instructions that will tell the make command in the subsequent step how to set things up specifically for your system configuration.

2. Configure the program by typing ./configure and pressing ENTER.

While you are running configure, you will see lots of odd and mysterious things flowing through your Terminal window; this is essentially a running account of what is going on, each step of the way. Depending on the program you are dealing with, this could take a bit of time—a few seconds or a few minutes. Either way, you needn’t worry. As long as the mysterious text keeps flowing and you don’t get an error message at the very end of the whole process, all will be well.

When configure has done its thing, you will see your prompt again, and you can go on to the next step, which is the translation, or compilation, step. The make command reads the makefile, created by configure, to see how things need to be set up on your machine. Then it proceeds to call on the compiler to translate the high-level source code into low-level, machine-readable files that can be installed in the subsequent step.

3. To perform this translation, type make, and press ENTER.

Again, you will be treated to more mysterious text flowing through the window and a short wait, usually a tad longer than for the configure process. Once make has done its job and you see your prompt again, you are ready to install the program.

Up to this point, you have not changed your system in any way. All the changes thus far have taken place in the xmahjongg-3.7 folder only. Your system is still as pure as the day you started. Of course, all that is going to end right now when you perform the final installation step.

1A-3: Installing Xmahjongg

Normally at this point of the process, you would type sudo make install and press ENTER, after which the files that were compiled in the make step would be installed in various locations throughout your system. You would then follow this up with the make clean command to tidy things up in the Xmahjongg folder by getting rid of any unnecessary files.

This traditional approach works fine enough, but things can get a bit messy if you find yourself wanting to upgrade or remove the application later on, as you have to keep track of where everything is and remove each item piece by piece using a series of sudo rm commands. Sometimes an application package will provide a make uninstall routine, but such routines don’t always work perfectly, aren’t available for every package, and require you to keep the project folder on your hard disk in order for you to perform the procedure.

If you are a traditionalist, you can go ahead and install Xmahjongg in this traditional manner. In the same Terminal window that you performed the ./configure and make steps, just type sudo make install, and then press ENTER. You will be prompted for your password, so type it, and then press ENTER again. Xmahjongg will then be installed and ready for use.

The Alternative Installation Method

The traditional installation step is fine enough, but because of the limitations I mentioned in terms of removing the application, I recommend finishing things off in a slightly different way. What you are going to do, assuming you are willing to go this route, is to skip the make install step and instead convert the compiled, but uninstalled, application into a DEB package, and then install that. By doing this, you can remove the package quite easily using Synaptic whenever the desire strikes. Having a DEB package for your application, you don’t need to keep the project folder, and you can save on hard disk clutter.