0
In this post i'm gonna to say about common build systems available in Linux and to use them. Open Source is famous term , in my point of view it means share the code, share the knowledge. Every one we know how to write a program. But few of them know how to build it after the developed code. So today i'm gonna to give you more details about making execution file by building it. It can be done with some tools.

Let we see about how to use the

--> Make
--> CMak
--> Ant build scripts
--> GNU Autotools



Just follow the steps to continue this tutorial....

Step 1 : Open terminal by Accessories -> Terminal or CTRL+ALT+T

Step 2 : Create the common directory to manage all the build programs.  For this create  tutorial_build_programs directory. So copy the below command and paste into your terminal.

mkdir tutorial_build_programs

Now move on to this directory by,

cd tutorial_build_programs


1. Make

Make is an utility, that uses makefiles to automatically build executable programs and libraries from source code. Makefile is nothing but a normal file which specifies how to derive the target program(source program).

Just follow the steps to use make....

Step 3 : create individual directory for make to work on it.

mkdir make_dir

Now move on this directory by,

cd make_dir
Then create sample C program. Here i'm gonna  to write C program to find the square root of the number. Enter the following command in terminal

gedit squareroot.c

Copy the below code and paste into squareroot.c file . After save it and close the file.

#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;
}

Test the above program by

gcc squareroot.c -o squareroot -lm

 ./squareroot 9

 Check the screenshot below...


Now we write a simple makefile to compile the program.

Enter the command in terminal as..

gedit Makefile

Copy the code and paste it in makefile and save it.

CC    =gcc
CFLAGS    =-g
LDLIBS    =-lm
all:squareroot
squareroot:squareroot.o
squareroot.o:squareroot.c
clean:
    rm -f squareroot squareroot.o


Now we test the Makefile: Enter the commands one by one...

make

make clean

make

./squareroot 9


That's all. You are done. successfully created executable file. Normally this process is the creating configure files for programs. Enjoy.


2. CMake

CMake is a 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. CMake  has following functions...

-> Creating Libraries
-> Generating wrappers
-> Compiling Source Code
-> Building Executables in arbitrary combinations

Step 4 :  Install CMake by

sudo apt-get install cmake

Move to previous common folder( tutorial_build_programs ) as

cd ..

Create new working directory for CMake and move on it by

mkdir cmake

cd cmake

Now copy the squareroot.c file ( previously written in make process) to cmake directory as

cp ../make_dir/squareroot.c .

Now we create configuration files for CMake. Enter the below command in terminal to create new CMakeLists.txt file

gedit CMakeLists.txt

and enter the following code in CMakeLists.txt file

cmake_minimum_required (VERSION 2.6)
project (squareroot)
add_executable(squareroot squareroot.c)
TARGET_LINK_LIBRARIES(squareroot m)





The above code generates the makefile. After this generation, with the help of this makefile program starts to executes as same like make (previous) process.

So to build makefile and further process, just create another new directory as build and generate makefile in it.

Enter the following commands one-by-one in your terminal...

mkdir build

cd build

ls

cmake ..

Now check the generate file list in build directory

ls

After generated makefile, do make process as

make

Again check the executable file after compilation

ls

Execute the program by using created executable file

./squareroot 9



That's all. Done!



3. Apache Ant

Apache Ant is a software tool for automating software build processes. Apache Ant is similar to make but it uses XML to describe the build process and its dependencies, whereas Make uses Makefile format. Apache Ant is implemented using java. So it is good for java projects.

Before starting to work on Apache Ant, you need to install java and compiler.

sudo apt-get install openjdk-7-jdk

Install Ant by

sudo apt-get install ant

Enter the following commands one-by-one in terminal ...

Move to common folder ( tutorial_build_program ) as

cd ../../

Create new directory as ant for working ant process and move into it...

mkdir ant

cd ant

Inside the ant directory, create src/hello directory for source file of HelloWorld.java

mkdir -p src/hello

Create HelloWorld.java under src/hello directory as

gedit src/hello/HelloWorld.java
 And now copy the simple helloworld program given below and paste into the HelloWorld.java file. Save it and close it.

package hello;
public class HelloWorld 

{
public static void main(String[] args){
System.out.println("Hello World");
}
}

 To build a class file for HelloWorld.java create new directory as build/classes

mkdir -p build/classes

Now test the HelloWorld file by compiling and running it as...
 
javac -sourcepath src -d build/classes/ src/hello/HelloWorld.java

 Check the result as..

 java -cp build/classes hello.HelloWorld

After that creat xml file for ant process..

gedit build.xml

 Copy the code and paste into the build.xml file


<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>

 Now run ant as...


ant clean


ant compile jar run





4.  GNU Autotools


We will be using a program designed specifically to teach about using autotools


Click  Here and download the latest version of hello-2.9.tar.gz file


http://ftp.gnu.org/gnu/hello/hello-2.9.tar.gz


 Copy the downloaded hello-2.9.tar.gz file and paste in the tutorial_build_programs directory. After that enter the following commands given in table as one-by-one for Autotools method...

GNU Autotools Process
     cd ..
     ls
     tar -xzf hello-2.7.tar.gz                          
     cd hello-2.7
     ./configure
     make
     src/hello
     sudo make install
     hello
     sudo make uninstall


That's all . You completed this tutorial...

If you have any queries, post below. We will help you...

Post a Comment

 
Top