Compiling Software From Source
Compiling Software From Source
Introduction:
Open source software is distributed in source code form. In case of popular software
Linux distributions will often have the software packaged in their repositories. If the package is
not package is not in the repository the user has to compile the software from source. To do this
the user has to understand about the build system used in the project.
The GNU build system, also known as the Autotools, is a suite of programming tools
designed to assist in making source-code packages portable to many Unix-like systems. It can
be difficult to make a software program portable: the C compiler differs from system to system;
certain library functions are missing on some systems; header files may have different names.
One way to handle this is write conditional code, with code blocks selected by means of
preprocessor directives (#ifdef); but because of the wide variety of build environments this
approach quickly becomes unmanageable. The GNU build system is designed to address this
problem more manageably.
Tools included in the GNU build system
The GNU build system comprises the GNU utility programs Autoconf, Automake, and Libtool.
Other related tools frequently used with the GNU build system are GNUs make program, GNU
gettext, pkg-config, and the GNU Compiler Collection, also called GCC.
GNU Autoconf
Autoconf generates a configure script based on the contents of a configure.ac file which
characterizes a particular body of source code. The configure script, when run, scans the build
environment and generates a subordinate config.statusscript which, in turn, converts other input
files and most commonly Makefile.in into output files (Makefile) which are appropriate for that
build environment. Finally the make program uses Makefile to generate executable programs
from source code.
The complexity of the GNU build system reflects the variety of circumstances under which a
body of source code may be built.
GNU Automake
Automake helps to create portable Makefiles, which are in turn processed with the make utility.
It takes its input as Makefile.am, and turns it into Makefile.in, which is used by the configure
script to generate the file Makefile output.
GNU Libtool
Libtool helps manage the creation of static and dynamic libraries on various Unix-like operating
systems. Libtool accomplishes this by abstracting the library-creation process, hiding
differences between various systems (e.g. GNU/Linuxsystems vs. Solaris).
Gnulib
Gnulib simplifies the process of making software that uses Autoconf and Automake portable to a
wide range of systems.
Make
In software development, make is a utility that automatically builds executable programs and
libraries from source code by reading files called makefiles which specify how to derive the
target program. Make can decide where to start through topological sorting. Though integrated
development environments and language-specific compiler features can also be used to
manage the build process in modern systems, make remains widely used, especially in Unix.
Make is typically used to build executable programs and libraries from source code. Generally
though, any process that involves transforming a dependency file to a target result (by executing
some number of arbitrary commands) is applicable to make. To cite an example, make could be
2
CMake
CMake is a unified, cross-platform, open-source build system that enables developers to build,
test and package software by specifying build parameters in simple, portable text files. It works
in a compiler-independent manner and the build process works in conjunction with native build
environments, such as make, Apple's Xcode and Microsoft Visual Studio. It also has minimal
dependencies, C++ only. CMake is open source software.
CMake can:
Create libraries
Generate wrappers
Compile source code
Build executables in arbitrary combinations
Apache Ant
Apache Ant is a software tool for automating software build processes. It is similar to Make but
is implemented using the Java language, requires the Java platform, and is best suited to
building Java projects.
The most immediately noticeable difference between Ant and Make is that Ant uses XML to
describe the build process and its dependencies, whereas Make uses Makefile format. By
default the XML file is named build.xml.
Ant is an Apache project. It is open source software, and is released under the Apache Software
License.
Description:
In this exercise students will learn how to use the GNU Autotools, Plain makefiles,
CMake and Ant build scripts. Students will write the build scripts for the various build tools
except for the Autotools where they will study an already existing project as a sample of how to
download and compile opensource software from the net.
Pre-requisites:
The computers need the development tools to be installed. Instructions for installing
them will be given along with each exercise.
Exercises:
Create a directory for all the programs in the exercise.
> mkdir build_systems
> cd build_systems
1. Make
We will be using a simple program written in C and write a makefile to compile the program.
> mkdir gnumake
> cd gnumake
> gedit squareroot.c
The Code:
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
2. CMake
We will now write a simple script for CMake to compile the previously written program.
First we install CMake
cd ..
mkdir cmake
cd cmake
cp ../gnumake/squareroot.c .
CMake is commonly use with out of source builds ie, we build the program in a directory
separate from the source. We use the generated makefile to compile the program.
> mkdir build
> cd build
> ls
> cmake ..
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to:
/home/<user>/projects/buildsystems/cmake/build
> ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
3. Apache Ant
Apache ant uses java. So we need to install java.
cd ../../
mkdir ant
cd ant
mkdir -p src/hello
The Code:
package hello;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The Code:
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar"
basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="hello.HelloWorld"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
</project>
4. GNU Autotools
Now we will learn how to use autotools. We will be using a program designed specifically to
teach about using autotools -- GNU Hello. Copy the file hello-2.7.tar.gz to the buildsystems
project directory and uncompress it.
> cd ..
> ls
10
The program will now reside in the src directory. To install the program log in as root.
> su
> make install
> exit
-t, --traditional
-n, --next-generation
-g, --greeting=TEXT
Instead of using software downloaded as an archive for compilation, source code can also be
obtained from software repositories using version control systems like SVN. Build and
installation instructions will be found along with the source code which can be used to buil the
software.
11
References:
1.
2.
3.
4.
12