Ex no: 11 Compiling software from source
Aim:
To learn about the common build systems available in Linux and to use them.
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 GNU's 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.
If a source code file is changed then it suffices to re-run make which only recompiles
that part of the body of the source code affected by the change.
● If a .in file has changed then it suffices to re-run config.status and make.
● If the body of source code is copied to another computer then it is suffices to rerun
configure (which runs config.status) and make. (For this reason source code
using the GNU build system is normally distributed without the files thatconfigure
generates.)
● If the body of source code is changed more fundamentally then configure.ac and
the .in files need to be changed and all subsequent steps also followed.
To process files, autoconf uses the GNU implementation of the m4 macro system.
Autoconf comes with several auxiliary programs such as Autoheader, which is used to help
manage C header files; Autoscan, which can create an initial input file for Autoconf; and
ifnames, which can list C pre-processor identifiers used in the program.
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 awide range of systems.
Make
In software development, make is a utility that automatically builds executable programs andlibraries from source code by reading files called makefiles which specify how to derive thetarget program. Make can decide where to start through topological sorting. Though integrateddevelopment environments and language-specific compiler features can also be used tomanage 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. Generallythough, any process that involves transforming a dependency file to a target result (by executingsome number of arbitrary commands) is applicable to make. To cite an example, make could beused to detect a change made to an image file (the dependency) and the target actions thatresult might be to convert the file to some specific format, copy the result into a content management system, and then send e-mail to a predefined set of users that the above actions
were performed.
Commands:
[root@localhost student]# mkdir gnumake1
[root@localhost student]# cd gnumake1
[root@localhost gnumake1]# vi squareroot.c
#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;
}
[root@localhost gnumake1]# cc squareroot.c -o squareroot -lm
[root@localhost gnumake1]# ./squareroot 3
The square root of 3 is 1.73205
[root@localhost gnumake1]# ./squareroot 36
The square root of 36 is 6
[root@localhost gnumake1]# make clean
rm -f squareroot squareroot.o
[root@localhost gnumake1]# make
gcc -g -c -o squareroot.o squareroot.c
gcc -lm squareroot.o -o squareroot
[root@localhost gnumake1]# ./squareroot 64
The square root of 64 is 8
[root@localhost gnumake1]# cd ..
[root@localhost student]# cd cmake
[root@localhost cmake]# cp ../gnumake1/squareroot.c .
[root@localhost cmake]# vi CMakeLists.txt
[root@localhost cmake]# mkdir build
[root@localhost cmake]# cd build
[root@localhost build]# ls
[root@localhost build]# cmake ..
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/lib/ccache/gcc
-- Check for working C compiler: /usr/lib/ccache/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/student/cmake/build
[root@localhost build]# ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
[root@localhost build]# make
Scanning dependencies of target squareroot
[100%] Building C object CMakeFiles/squareroot.dir/squareroot.c.o
Linking C executable squareroot
[100%] Built target squareroot
[root@localhost build]# ls
CMakeCache.txt cmake_install.cmake squareroot
CMakeFiles Makefile
[root@localhost build]# ./squareroot 64
The square root of 64 is 8
[student@localhost ~]$ su
Password:
[root@localhost student]# yum install java-1.6.0-openjdk-devel
Loaded plugins: langpacks, presto, refresh-packagekit
Adding en_US to language list
Setting up Install Process
Package 1:java-1.6.0-openjdk-devel-1.6.0.0-55.1.9.10.fc14.i686 already installed and latest version
Nothing to do
[root@localhost student]# yum install ant
Loaded plugins: langpacks, presto, refresh-packagekit
Adding en_US to language list
Setting up Install Process
Package ant-1.7.1-13.fc13.i686 already installed and latest version
othing to do
[root@localhost student]# cd ../../
[root@localhost /]# mkdir ant
[root@localhost /]# mkdir -p src/hello
[root@localhost /]# vi src/hello/HelloWorld.java
package hello;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
[root@localhost /]# mkdir -p build/classes
[root@localhost /]# javac -sourcepath src -d build/classes/ src/hello/HelloWorld.java
[root@localhost /]# java -cp build/classes hello.HelloWorld
Hello World
[root@localhost /]# vi build.xml
<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>
[root@localhost /]# ant clean
Buildfile: build.xml
clean:
[delete] Deleting directory /build
BUILD SUCCESSFUL
Total time: 0 seconds
[root@localhost /]# ant compile jar run
Buildfile: build.xml
compile:
[mkdir] Created dir: /build/classes
[javac] Compiling 1 source file to /build/classes
jar:
[mkdir] Created dir: /build/jar
[jar] Building jar: /build/jar/HelloWorld.jar
run:
[java] Hello World
BUILD SUCCESSFUL
Total time: 0 seconds
[root@localhost /]#
Output:
Result:
Thus the software was compiled from source packages.
Don't ever give up.
Even when it seems impossible,
Something will always
pull you through.
The hardest times get even
worse when you lose hope.
As long as you believe you can do it, You can.
But When you give up,
You lose !
I DONT GIVE UP.....!!!
In three words I can sum up everything I've learned about life - it goes on......
with regards
prem sasi kumar arivukalanjiam
No comments:
Post a Comment