Thursday, September 10, 2009

(Tech) First Splash: Compiling Your First Program On Linux

I know kung fu... -- Neo, The Matrix

Welcome to the first entry on my blog. We get things rolling with something rather basic and not so special. For the total Linux noobs, the courageous ones who dare to be different from the rest of the typical computing crowd, this one's for you. Compiling your first source code.



Okay, You've gotten fed up with Windoze and switch to linux. A typical noob choice would be Ubuntu. Simply because it works nicely with little fuss. Other distros can be just as good, so whatever makes your day... But for our purpose we'll have Ubuntu 8.10 as an example. After being spoiled by using the ever-friendly Add/Remove Software option and Synaptic Package manager, you found your favorite software which, unfortunately, is not present on the repository and has no binaries you can directly download... What do you do? Download the source code, of course.

There are many ways to do this, but for now we'll be doing a simple approach. You begin with downloading all the packages and extracting them to a directory. In the GUI you can just right-click and select "Extract Here" from the context menu. After doing so, this is when you get intimate with your ^other^ best friend when the GUI is absent-- The command line.

Open it and the program will initialize, and you'll see a prompt with your home directory as the present working directory. You can confirm this by typing in the prompt:

pwd

This should return something like:

/home/yourusername

That indicates your present working directory. If the extracted directory's not already at that location, you may want to navigate to it with the cd command.

cd the/location/of/the/extracted/directory

If done correctly you should now be at the directory containing the source code. Type ls to check. Here's the flow of what you'll do: Configure the settings, build the binary, install the binary. It may sound daunting to some but it's actually pretty straightforward. There are three simple commands to remember: Configure. Make. Make Install.


CONFIGURE


Among the three commands this is the tedious part. This is where you, as the name suggests, configure your app. To configure the settings type in the prompt

sudo ./configure

It should ask for a password. The password is the one you used to login. This will return a lot of moving texts and miscellaneous verbiage so you can just sit tight and wait for it to stop. Inevitably you'll encounter an error message. The text stops moving and that's when you read what went wrong. Typically you have missing library files. Use your package manager (e.g Synaptic) to download and install the missing libraries (you do know how to use synaptic, right?) and retype the sudo ./configure command again. Repeat as needed. Alternatively, you can follow the suggestion provided by disabling the feature if you don't want it. No need to download the package for that.

The more patient ones can avoid this rather tedious task by reading the documentation and downloading the libraries beforehand. If you're just plain bored, this is one way of killing time.

How do you know it's the correct package? It usually contains the name of the app you need and it begins with lib and ends with -dev. Those are the ones you need. The development files. When you try to install them they may also include other packages. Package names may also be very different from the name of the apps that you know you need so be sure to watch out for that occasional pitfall as well.



MAKE


After the configuration is completed, it's time to build it. This task is relatively simple. Just type at the prompt:

./sudo make

... And wait for it to finish. You can use the time to go fix yourself a cup of coffee or switch to another task. This usually takes a while... a bit.



MAKE INSTALL


After waiting for some time the build comes out successfully... It usually does, haven't encountered problems with it myself... The tasks culminate with installing it to your system. Just type in:

./sudo make install

... And wait some more!!! After the installation you should be able to run your new self-built application. That's about it at the minimum. If you fancy it, there are other options with the make command. you can check them out by typing in:

make -h

... And that gives you all the options that you can use with the make command. For more information please be sure to consult your man pages. You can access them by typing at your terminal:

man nameofcommandyouwanttolearnabout

or with

man -k keywordofsubjectyouwanttolearnabout

Now that's all there is to it. That wasn't too bad now, wasn't it? Be sure to experiment a lot... There's a lot you can learn along the way. Don't worry you didn't turn into a nerd or geek... yet. Happy computing! :)

No comments:

Post a Comment