Introduction
Many people might wonder why you would want to compile source code. After all, most Linux programs can be had by simply opening up their favorite package manager and clicking "install". The package manager does the rest. However, there *are* reasons why you would want to do this. For one thing, some Linux programs are only distributed in source code. Or maybe they don't distribute packages for your Linux distribution. In any case, you'll need to compile it.
Now compiling source code isn't the easiest thing. You need to have lots of libraries and other junk installed, need to know the right compilation syntax, and how to deal with errors when they come up. If this is scaring you, rightly so. I prefer installing packages with a package manager, too, so if that's an option for you, I suggest doing that instead. (In case you're wondering: here's my Linux packages tutorial.)
What you'll need
On your Linux system, you're going to need to have the command line software development tools installed. This includes gcc, g++, automake, and others. You don't actually need the Gnome and KDE development tools, as you're going to be using the command line for most of the compilation.
Getting the source code
To start off, you'll need to download the source code for the particular application you plan to use. How do you know if what you're downloading is source code? Look at the file extension. If it ends in the most common extensions, .tar.gz or .tar.bz2, you can be nearly guaranteed that the archive you're downloading contains the program's source code.
Extracting the source code
Once you've downloaded the file, it's time to extract it. Although you could use the graphical UI tools to extract the package, we're going to be needing the command line to compile it anyway, so it's just as easy to do it at the shell. So open up Konsole or whatever terminal application you use, and get ready to enter commands!
Depending on the file extension, you're going to be needing different methods of decompressing it. If it's a .tar.gz file (most common), enter the following:
tar -xfz myprogram.tar.gz
Replacing myprogram.tar.gz with the name of the file you downloaded.
And if it's tar.bz2, it's equally easy:
tar -xjf myprogram.tar.bz2
The files will be extracted into the current directory, but that's usually not a problem, since most program's source code is encased in a single directory. Now you need to change into this directory. First of all find the name of the directory:
ls
A directory usually with the name based on the archive would be here. Make this the active directory:
cd myprogram
The actual compilation process
This is where the real fun begins. Now you need to turn the source code into an actual binary which the computer can run, so here's how you do it.
Compilation for newbies is usually described as "./configure make make install". It's a series of commands which under many circumstances, will compile the code without any errors. Here it is:
# ./configure
# make
# su
[enter password]
# make install
Here is how it works:
The ./configure command is actually a script that comes with most Linux programs. It looks at your hardware, the packages you have installed, and determines what will be needed to compile it. It then generates a unique makefile, which is a file containing a list of commands to the compiler. There's various options you can send configure; for a full list run configure with the --help option.
The "make" command is actually a *nix utility that is installed on most machines. What is does is look for a makefile generated from a configure script, and it runs it. This in turn runs the compiler, which then proceeds to compile your source code.
Finally "make install" uses the make utility, but for a different purpose. It looks at the makefile and copies the files generated by the compiler to system-wide directories so that all users have access to the program. That is why you need root access to run it. However, in many cases the program does not have to be installed, and can simply be run from the directory it was compiled in. In such a case, it's your choice whether you want to install it or not.
The errors-solving process
Undoubtedly, you'll get errors when you try to compile. This is quite normal and to be expected. There's usually nothing wrong with the source code however, so don't bother contacting the project's developers for help, because it's unlikely you'll get any.
The first thing to do when you get an error is to read the README or INSTALL file that came with the source code. (Which is actually what you should read first, but I never do :P. Don't worry, it doesn't hurt anyone.) It will usually contain installation instructions, and other requirements in order to compile the code.
Most often, your system is lacking libraries. These usually result in compilation errors such as "libjpeg not found" and derivatives. Usually the documentation describes which libraries you need, so set about to installing them. Although you *can* compile these packages if you want to, you usually end up on a wild goose chase, as this source code has dependancies of its own. The best thing is usually to open up your package manager and download the files yourself.
Try again, and then see if the errors went away or changed. If the system cannot find ./configure, you may have gotten one of those odd packages which contain no configure script. Check the project's documentation for more details.
Another resource you can use is Google. If you're having errors, it's highly unlikely that someone else hasn't had these errors too, at some point or another. So try googling the compilation errors, or parts thereof, and see what turns up. You might be surprised.
Last but not least, don't forget the community support. DaniWeb is a great forum for asking for help, and it's likely I'll see your problem if you post in the *nix forums. So don't kill yourself trying to compile a program.
Conclusion
Although compiling programs isn't something that we all enjoy, many things can be learned from it. You can get a better understanding of how programs are put together; learn how to optimize programs for best performance, and how to do regular system tool maintenance.
I could only cover the basics here, so to get a better understanding of how ./configure works and make
work, follow the links below.
--Joe