Basics Of Compiling Software From Source Code In Linux
Table of Contents
From time to time, you may want or need to compile a software/program from the source code. This involves compiling C or C++ code into something we can execute in Linux. Depending on the software you wish to compile, it’s often unnecessary to do so. This is because your distribution will likely have the binary form of the program already in the software repositories.
In fact, you should choose the binary packages over compiling from the source. One reason is that it could potentially introduce problems for your system. However, if ever the time should come where you need compile from source, this guide will give you some basic pointers.
A Typical Example
Let’s take a program written in either C or C++. Downloading the source code often involves either downloading and unpacking a package (often a tarball), or cloning a git repository. The typical method of operation is the execution of these three commands: $
./configure $ make $ sudo make install
The first command runs the configure script that analyses the libraries installed on your system. If a required library isn’t installed, it will report it and you will need to install it. Not only will you need the library installed, but also the development files must be present as well. Source code files use the functionality found in these libraries.
After the configure command is executed, you should have a Makefile present. By running ‘make’, it will read the Makefile in the current directory and start running the compiler (gcc for C, or g++ for C++) to compile the software.
The third command isn’t strictly essential but is recommended for system-wide access, that is running the executable from anywhere in the terminal. This command tells ‘make’ to run instructions for installing the program to the system.
The INSTALL Or README Files
There may be extra instructions that must be completed before compiling can begin. Reading this file (usually INSTALL) should also reveal what development libraries may need to be installed first. Such packages will contain the suffix “-dev” to indicate that these are the development headers and must be installed. The configure script may also require extra parameters as well. README will contain any other information.
You may even find that none of the three commands are run during the whole process. Even variations of make such as qmake (for Qt projects) or cmake could be used instead.
Install A GitHub Program
Let’s begin by installing a text editor called wxMEdit, found on GitHub. Here I demonstrate how to install a program by compiling from source. This is an almost typical example of the scope of this guide. If you visit the GitHub repository, you will see that the readme file has details about the application. There aren’t any compiling instructions so the standard sequence applies, with one difference: autogen.sh.
First, we change to where we wish to store the repository in the terminal and clone it:
$ cd wherever $ git clone git://github.com/wxMEdit/wxMEdit.git
Autogen.sh
If you find this file in the source code, you should run this first before “./configure” as this will perform tasks that will allow you to compile the software successfully. If that is successful, then you can run “./configure”, “make”, and then “sudo make install”, and whatever else you need to run (in this case, nothing else).
$ ./autogen.sh
$ ./configure
$ make
You should see no errors in the make output.
$ sudo make install
Troubleshooting
If you do so regularly, you are bound to encounter certain issues when compiling from source.
Missing Libraries
Remember, it’s not enough for the binary libraries to be installed; the source files or development headers must also be installed as well. The configure script, along with the documentation, will usually alert you of any libraries (development headers) you need to install for a successful compilation. Ensure these are installed on your system.
They’re Installed And Other Errors Occur
Conclusion
Compiling software can either be quite simple, or quite painful. Fortunately, many Linux distributions provide binary packages ready to install so compiling from source isn’t usually necessary. It is better to stick with the binary packages provided by the software repos provided by your distribution if they’re available.
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.