How to Compile Software from Source Code

Maximum PC Staff

As a Windows user, you’re probably used to software that comes in a user-friendly package. If you want to install a new program, it’s usually a matter of running setup.exe, or occasionally a .msi installer. Because of this, a lot of Windows users panic when faced with the prospect of installing a file from source code. Well fear not, because it’s not that difficult. We’ll walk you through a source code install, step by step.

1. Install Cygwin

There’s more than one way to compile source code on a Windows machine, but Cygwin is our go-to application for getting Unix-like control on a PC. For a complete look at how to get started with Cygwin, check out our introductory how-to from the October 2010 issue, now available online at http://bit.ly/9ynEBl . For now, here are the basics you’ll need to know.

You can find the Cygwin installer online at www.cygwin.com —download and run it. During the install, you’ll be asked to select which packages to install; make sure the Devel package is selected (above). Finally, when you install Cygwin, remember what you select for a home directory, because that’s where you’ll be doing the following steps.

2. (Optional) Check the md5 Hash

One of the great things about open-source software is that you can verify that what you’re compiling is exactly what it’s meant to be and hasn’t been tampered with or damaged on its way to your computer. The most common method of verification is with an MD5 hash, which takes a large document (a bunch of source code, for instance) and creates a nearly unique 256-bit string of characters. By making sure that your code produces the same MD5 hash as the original, you know that the two are identical.

Most sites that provide source code will also provide the MD5 code right alongside it (above), so you can verify the code if you download it from a mirror. To generate an MD5 code from the file that you’ve downloaded, just use the md5sum command in Cygwin, like this:

md5sum gimp-2.6.11.tar.b2

Where in this case, “gimp-2.6.11.tar.b2” is the name of the file we want to verify. It will output a hash (below), and you can quickly check to make sure that it matches the one posted on the source website.

3. Install and Decompress the Installer

Source code usually comes in a file with a name like “gimp-2.6.11.tar.bz2,” which can seem like quite a mouthful, but it’s actually not all that complicated. The numbers after the name of the file are version numbers, so you’ll want to grab the latest (stable) source code. The “.tar” part of the file indicates that this is a “tarball” archive file, which is opened with the tar command-line program. Unlike common Windows archive formats like .zip and .rar, .tar is uncompressed. For transferring large files (like source code) over the Internet, a compression scheme is used—hence the final part of the filename, “.bz2,” which indicates the compression algorithm used on the tarball.

Once you’ve got the file downloaded to your Cygwin home directory, you can unzip it using the tar command. Here’s how you would decompress the example GIMP source code bundle we downloaded:

tar xjvf gimp-2.6.11.tar.bz2


The “xjvf” tells the tar program how to decompress the file we downloaded. You should be able to use the same command for any source code you download, unless it ends with “.gz,” which is a different compression scheme and has to be unzipped with the command:

tar xzvf

Once you’ve executed the command, tar will extract all files in the tarball to a new directory created inside of the one you’re currently in, named after the file. Switch to that directory (above) with the cd command:

cd gimp-2.6.11

4. Read!

As much as we would like there to be a one-size-fits-all guide to compiling source code, there are actually minor differences in how many programs compile. Because of that, most every program comes with an INSTALL file, which is a text document that details what you’ll need to do to install the software (above). Frequently, this will include a list of package dependencies. The Devel package that you installed with Cygwin includes the most common packages used in building from source code.

5. Configure, Make, Make Install

If the install file doesn’t specify differently, the basic steps that make up most source code builds are as follows. Execute all commands from within the directory that you extracted the tarball’s contents to:

./configure

This command runs the configure script (below), which gets your computer ready to build the software. It will tell you if you’re missing anything. For instance, running the configure script for GIMP revealed that we were missing the necessary babl and gegl software—both of which can be easily downloaded and installed from their own source code—all within Cygwin.

Once the configure script has completed, and not returned any major error, you can actually compile the program by running the following command:

make

That one’s pretty simple, right? This command simply tells the make program to begin compiling the source code in the directory you’re currently in. This step may take a while, depending on the complexity of the program you’re compiling and the speed of the computer. In the process it’ll output a whole lot of intimidating-looking text to the shell, but it’s not anything you have to understand.

Finally, once make has completed successfully, you can run the final command:

make install

This simply takes the compiled program and actually installs it, putting files where they belong in your filesystem.

And, like that, you’re done. Of course, it’s very likely that you’ll run into an error or two along the way (open source isn’t exactly synonymous with user friendly) but there are a lot of communities online dedicated to helping you find out what went wrong, and a simple Google search can frequently turn up an answer to any problems you might encounter.

Around the web

by CPMStar (Sponsored) Free to play

Comments