| The basics of compiling programs in linux |
|
|
| Submitted by psYchotic | ||||||||||||||||||||||||||
| Monday, 11 December 2006 | ||||||||||||||||||||||||||
|
But sometimes there are little programs that do specific tasks that you won't find so easily, and if you do, they won't be in a binary form, which means you will have to compile the source code in order to use it. This is quite often viewed as a difficult task by your average Joe. So here's a basic tutorial on how to compile a program in linux (this also kinda applies to Cygwin, but don't take my word for it, for I have never really tried)
NOTE: This article is pretty much written with the Ubuntu user in mind, but it could very well be helpful even if you have another distribution. First and foremost, you will need to have a running Linux operating system. There are many to choose from, if you want to see some info about linux distributions, take a look at DistroWatch. I will not go into the details on how to install such an operating system, as this is not the purpose of this article, besides, it has become quite easy with distributions such as Ubuntu (that's the one I use). The next thing you will need is a compiler, and standard libraries. To install these in Ubuntu, you would open a terminal and do: sudo apt-get install build-essential Note: From this point on, all bold and indented text will be commands you will have to type in a terminal.
It can also be done by opening the package manager (like synaptic), searching for that package, and finally installing it. This package is actually no more than a list of what is deemed essential by the Ubuntu community, but it also depends on those same packages, so they will be installed as well. I am not sure how you'd go about doing this in other distributions, but it is typically nothing more than installing packages tagged with something along the lines of "Development" or simply "devel". Refer to manuals that come with your distribution, or to your distribution's community for further details.
Next step is downloading the source code of the application you wish to install. In this article I will use gEdit as the example. Most of the time, source code is provided as a compressed archive, which will be a .tar.gz or .tgz 90% of the time. To extract it, use your favorite utility (like File-Roller in Ubuntu), or do: tar xvzf archivename.tar.gz or tar xvzf archivename.tgz Obviously, replace "archivename" to whatever the archive's name really is. This will extract the archive into the directory you are working in, and most of the time it is safe to do it like this, but sometimes (it's quite rare, but still) it won't create a new directory, and extract all the files directly into the current directory. To prevent this, you can make a new directory, and then go into it like this: mkdir newdirectoryname mv archivename.tar.gz newdirectoryname cd newdirectoryname and then extract the archive. As the files are being extracted, you will see where those are being extracted to. The output looks somewhat like this: psychotic@psYchotic:~$ tar xvzf gedit-2.17.1.tar.gz ... So what you will do now, is go into the directory that was just created by the file extraction utility, in this case that would be "gedit-2.14.4" cd gedit-2.14.4 It is usually a good idea to see what kind of options you can change with this script by doing: ./configure --help This will output some parameters you can use, and what they do (usually they disable/enable functionalities in the program). But you'll only really use this when you get the hang of it. Also, not all the parameters are so straight-forward, so it might be an idea to use google to find out what they are for. Usually, all you will have to do is: ./configure A little explanation on why you need to put the "./" (dot slash) in front of "configure": if you type "configure", your shell (the program that executes your commands) will look in some directories that are available in your PATH variable, and if a program named "configure" is found there, it will be executed, instead of the "configure" you have in that directory. The dot is used as a replacement for the path of the directory you are working in. So in my case, the dot represents "/home/psychotic/wget-1.10.1". So to the shell, "./configure" looks just like "/home/psychotic/wget-1.10.1/configure" which is an absolute path, so it doesn't need to search for "configure". Ok, so that's that. But what happens? If all goes as it should, the configure script will check for everything, see everything you need is actually on your computer, and create some files to actually compile the program. But most of the time, especially when you haven't compiled anything before, you will get errors, such as: checking for GEDIT... configure: error: Package requirements ( This means you are missing a library. It's not a catastrophe, it happens all the time, but it's a somewhat tedious task. What you have to do to fix something like this is to find out what package provides the missing library. So we look for the package in question. Usually, these packages have names that begin with "lib" and end with "-dev" (though it's not always necessary to install the development files, but if you do, the binary libraries will also be installed). To find it, you can do two things, fire up your favorite package manager and search for the library, or use google to find out. In this case, the packages we need to install are called "libgtk2.0-dev", "libgtksourceview-dev", "libgnomeui-dev", "libglade2-dev" and "libgnomeprint2.2-dev" (we're still on Ubuntu, so these might not be the names of the packages in another distribution). So we install them: sudo apt-get install libgtk2.0-dev libgtksourceview-dev libgnomeui-dev libglade2-dev libgnomeprint2.2-dev and we try to configure again. ./configure If all went well, the configure script should have finished without any errors (warnings can usually be ignored, although they can mean that a feature will not be compiled). Now we have the files we need to actually compile this program. So now you will do: make That's rather easy, isn't it? This step takes a while, but it doesn't require you to do anything really. You will lots of gibberish on your terminal, and you should ignore all of it, except when there are lines that actually mention an error. In that case something has gone wrong despite the fact that you seem to have the necessary libraries. If this happens, the best thing you can do is use google to search for the error your compiler has come up with and see if others have had the same problem. The final step is installing the program, which is rather easy: sudo make install I would recommend using checkinstall though, it's a little program that makes a package for you, and installs it. This makes it easier to uninstall the program if you wanted to. To install checkinstall, do: sudo apt-get install checkinstall Then, instead of doing "sudo make install" you do: sudo checkinstall This will ask you some questions, and it's safe to just press Enter on most of them, except perhaps when it shows you the name of the package it will create, because it will probably look like this "gedit-2.14.4", and it's easier to just make it "gedit" and change the version to "2.14.4". But I won't really go into this. If you have questions, feel free to comment. Anyway, congratulations! Hopefully, you have just successfully compiled and installed a program! To sum the process up:
Any problems, ask on on our Linux Forum .
Powered by JoomlaCommentCopyright (C) 2006 Frantisek Hliva. All rights reserved.Homepage: http://cavo.co.nr/ |
||||||||||||||||||||||||||


Nowadays, most people have heard about linux. Some even tried it, but few actually use it on a daily basis, as Microsoft Windows seems easier. Also, what most users are afraid of is that they won't be able to use their favorite software, like Microsoft Office. Usually, the kind of software that is used a lot in Windows has a free and open source counterpart in linux (in Microsoft Office's case, OpenOffice.org is one of them).
Any problems, ask on on our