shared library project report

Upload: namburivk

Post on 02-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Shared Library Project Report

    1/33

    Project report on

    Shared Library Development

    In LINUX

    Under the Guidance of

    Sri A.DIVAKAR, ASSISTANT PROFESSOR,

    DEPT. OF CSE,

    GIT , GITAM UNIVERSITY.

    Namburi Vamsi Krishna

    Regd no:1210311438

    3/4 B.Tech (B4), CSE DEPARTMENT

    GIT, GITAM UNIVERSITY

  • 8/10/2019 Shared Library Project Report

    2/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 2

    Project Report

    Shared Library Development in LINUX

    Namburi Vamsi Krishna

    Supervisor: A.Divakar

    9th October , 2013.

  • 8/10/2019 Shared Library Project Report

    3/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 3

    ABSTARCT

    In computer science, a library is a collection of implementations of behaviour, written in terms of a language , that has a well-defined interface bywhich the behaviour is invoked.

    UNIX is the operating system which will allow its users to create theirown libraries to suit their programming needs. Shared libraries providemodularity to the development environment as the library code can be changed,modified and recompiled without having to re-compile the applications that usethis library.

    In this report , the development of the shared LINUX C libraries isexplained .The development , shown in this project is done on the FEDORA-17,a Linux environment , having kernel release 3.3.4-5.fc17.x86_64.

    Here in this project , a shared C library that consists of all the basicmatrix functions , matrix , is developed. It has the following functions:

    1. To display transpose of a matrix2. To find the inverse of a matrix

    3.

    To add two matrices4. To multiply two matrices5. To find the determinant of a square matrix6. To find the cofactor matrix of a square matrix

    In this project report , a detailed explanation of the libraries and theirdevelopment and usage is given . It also illustrates this usage with help ofexample code runs.

  • 8/10/2019 Shared Library Project Report

    4/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 4

    TABLE OF CONTENTS

    1. Introduction - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -51.1 What is a Library? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 51.2 Types of Libraries - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 5

    2. Shared Libraries - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -72.1 Deep into Shared Libraries - - - - - - - - - - - - - - - - - - - - - - - 72.2 Why Shared Libraries are used? - - - - - - - - - - - - - - - - - - - - 7

    3. LINUX C Shared Libraries - - - - - - - - - - - - - - - - - - - - - - - - - - - -8

    3.1 LINUX Shared Library Naming Conventions- - - - - - - - - - - - 83.1.1 Shared Library Names- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 83.1.2 File System Placement- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 93.1.3 Environment Variables- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 10

    4. Shared Library Development- - - - - - - - - - - - - - - - - - - - - - - - -114.1 Creating Object File with Position Independent Code- - - - 184.2 Creating Shared Library with Object file- - - - - - - - - - - - - - 19

    5. Installing and Using Shared Libraries- - - - - - - - - - - - - - - - - -20

    5.1 Installing a Shared Library- - - - - - - - - - - - - - - - - - - - - - - - 205.2 Using the Shared Library- - - - - - - - - - - - - - - - - - - - - - - - - 20

    5.2.1 Compiling main.c-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 235.2.2 Making the library available at run time- - - - - - - - - - - - - - - - - - - - - 255.2.3 Running the executable- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 25

    5.2.3.1 The Output- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 25

    6. Examples- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -276.1 Example 1- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 27

    6.2 Example 2- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 297. Conclusion- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -32

    Refernences- - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - -33

  • 8/10/2019 Shared Library Project Report

    5/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 5

    1. Introduction

    The purpose of this project is to develop a Shared C Library , to make thefuture compilations easy and efficient.

    1 1 W hat i s a Library?

    A library is a file containing compiled code from various objectfiles stuffed into a single file. It may contain a group of functions that are usedin a particular context. For example, the pthread library is used when threadrelated functions are to be used in the program. This methodology, also knownas "Shared components" or "Archive libraries" , groups together multiplecompiled object code files into a single file known as a library.

    Typically C functions/C++ classes and methods which can beshared by more than one application are broken out of the application's sourcecode, compiled and bundled into a library. The C standard libraries and C++STL are examples of shared components which can be linked with your code.The benefit is that each and every object file need not be stated when linking

    because the developer can reference the individual library. This simplifies themultiple use and sharing of software components between applications. It also

    allows application vendors a way to simply release an API to interface with anapplication. Components which are large can be created for dynamic use, thusthe library remain separate from the executable reducing it's size and thus diskspace used. The library components are then called by various applications foruse when needed.

    1 2 Types of Librar ies

    Broadly, a library (or Program Library) can be of two types :

    a) Static Libraries:In computer science, a static library or statically-linked library is a

    set of routines, external functions and variables which are resolved in acaller at compile-time and copied into a target application by a compiler,linker, or binder, producing an object file and a stand-alone executable.

    This executable and the process of compiling it are both known asa static build of the program. Historically, libraries could only be static.

  • 8/10/2019 Shared Library Project Report

    6/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 6

    Static libraries are either merged with other static libraries and object filesduring building/linking to form a single executable, or they may beloaded at run-time into the address space of the loaded executable at astatic memory offset determined at compile-time/link-time.

    b) Shared Libraries:A shared library or shared object is a file that is intended to be

    shared by executable files and further shared objects files. Modules used by a program are loaded from individual shared objects into memory atload time or run time, rather than being copied by a linker when it createsa single monolithic executable file for the program.

    Shared libraries can be statically linked, meaning that references tothe library modules are resolved and the modules are allocated memorywhen the executable file is created. But often linking of shared libraries is

    postponed until they are loaded.

    There are two Linux C/C++ library types which can be created:a) Static libraries (.a):

    Library of object code which is linked with, and becomes part of the application.

    b) Dynamically linked shared object libraries (.so):There is only one form of this library but it can be used in

    two ways.Dynamically linked at run time but statically aware. The

    libraries must be available during compile/link phase. The sharedobjects are not included into the executable component but are tied tothe execution.

    Dynamically loaded/unloaded and linked during execution(i.e. browser plug-in) using the dynamic linking loader systemfunctions.

    In this project , the focus is laid on the LINUX shared C librariesand their development.

  • 8/10/2019 Shared Library Project Report

    7/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 7

    2. Shared Libraries

    2 1 Deep in to the Shared L ibrar ies :

    Shared Libraries are the libraries that can be linked to any programat run-time. They provide a means to use code that can be loaded anywhere inthe memory. Once loaded, the shared library code can be used by any number of

    programs. So, this way the size of programs(using shared library) and thememory footprint can be kept low as a lot of code is kept common in form of ashared library.

    A shared library is loaded into physical memory only once andreused by multiple processes via virtual memory. When a shared library isinstalled properly, all programs that start afterwards automatically use the newshared library.

    2 2 W hy Shared Librar ies are used?

    Shared libraries provide modularity to the developmentenvironment as the library code can be changed, modified and recompiledwithout having to re-compile the applications that use this library. For example,for any change in the pthread library code, no change is required in the

    programs using pthread shared library.

    Static Libraries reduce memory consumption if used by more thanone process, and they reduce the size of the executable . They make developingapplications easier.

    A small change in the implementation of a function in the librarydon't need the user to recompile and relink his application code every time. You

    need to only relink if you make incompatible changes, such as addingarguments to a call or changing the size of a struct . It's actually much moreflexible and sophisticated.

    Having shared libraries allows you to create programs that can break down functions into several different binary files. If there were no sharedlibraries all the programs would be a single, massive binary file. Updating

    programs would be pain because the entire project would have to berecompiled.Also, shared libraries allow programmers to reuse existing binary

    files so that standard functions don't have to be recompiled again and again.

  • 8/10/2019 Shared Library Project Report

    8/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 8

    3. LINUX C Shared Libraries

    The LINUX C shared libraries .so files .These are same as that of the.dll files in the Windows. LINUX permits you to:

    Update libraries and still support programs that want to use older,non-backward-compatible versions of those libraries

    Override specific libraries or even specific functions in a librarywhen executing a particular program.

    Do all this while programs are running using existing libraries.

    3 1 LINU X Shared Library Naming Convent ions :

    For shared libraries to support all of the desired properties, anumber of conventions and guidelines must be followed. One need tounderstand the difference between a library's names, in particular its soname and real name (and how they interact). And also must understand where theyshould be placed in the filesystem.

    These naming conventions help multiple versions of same shared

    library to co-exist in a system. The programs linking with the shared library donot need to take care about the latest version of the shared library installed inthe system. Once the latest version of the shared library is installed successfully,all the programs automatically start linking to the latest version.

    3.1.1 Shared Library Names

    Every shared library has a special name called thesoname . The soname has the prefix lib , the name of the library, the phrase.so, followed by a period and a version number that is incremented wheneverthe interface changes (as a special exception, the lowest-level C libraries don'tstart with lib ).

    Example : libpthread.so.1

    A fully-qualified soname includes as a prefix thedirectory it's in; on a working system a fully-qualified soname is simply asymbolic link to the shared library's real name .

  • 8/10/2019 Shared Library Project Report

    9/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 9

    Every shared library also has a real name , which isthe filename containing the actual library code. The real name adds to thesoname a period, a minor number, another period, and the release number. Thelast period and release number are optional. The minor number and releasenumber support configuration control by letting you know exactly whatversion(s) of the library are installed. These numbers might not be the same asthe numbers used to describe the library in documentation, although that doesmake things easier.

    Example : libpthread.so.1.1

    In addition, there's the name that the compiler useswhen requesting a library, called the linker name , which is simply thesoname without any version number.

    Example : libpthread.so

    A version number is changed for a shared librarywhen the changes done in the code make the shared library incompatible withthe previous version. For example, if a function is completely removed then anew version of the library is required.

    A minor number is changed in case there is amodification in the code that does not make the shared library incompatiblewith the previous version being used. For example, a small bug fix won't breakthe compatibility of the existing shared library so only a minor number ischanged while version remains the same.

    3.1.2 Filesystem Placement

    Shared libraries must be placed somewhere in thefilesystem . The GNU standards recommend installing by default all libraries in/usr/local/lib when distributing source code (and all commands should go into/usr/local/bin). They also define the convention for overriding these defaults andfor invoking the installation routines.

    According to the Filesystem Hierarchy Standard(FHS), most libraries should be installed in /usr/lib , but libraries required for

  • 8/10/2019 Shared Library Project Report

    10/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 10

    startup should be in /lib and libraries that are not part of the system should bein /usr/local/lib .

    3.1.3 Environment Variables

    One can temporarily substitute a different library forexecution. In Linux, the environment variable LD_LIBRARY_PATH is acolon-separated set of directories where libraries should be searched for first,

    before the standard set of directories; this is useful when debugging a newlibrary or using a nonstandard library for special purposes.This is used in thefollowing way:

    export LD_LIBRARY_PATH = absolute_path_of_the_directory_containing_the_library.Example : export LD_LIBRARY_PATH=/home/cf/lib

    If in current directory you can give the following command:

    export LD_LIBRARY_PATH = .

    If we have to append a new directory to the existing paths then add the directories separated by colons to environment variable , inthe following way:

    export LD_LIBRARY_PATH=/opt/lib : absolute_path_of_the_directory_having_the_library : $LD_LIBRARY_PATH

    Example :

    export LD_LIBRARY_PATH=/opt/lib:/home/cf/lib:$LD_LIBRARY_PATH

    Keeping these conventions in mind , we will create the sharedlibraries in LINUX.

    Here the development and the execution processes are explained bycreating a shared library , matrix.h and we try to implement the execution of

    the programs using this library.

  • 8/10/2019 Shared Library Project Report

    11/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 11

    4. Shared Library Development

    Here the development processes are explained by creating a sharedlibrary , matrix.h.

    To develop a shared library , we use the following source codes:

    A) matrix.h

    #ifndef matrix_h__

    #define matrix_h__

    void transpose( float a[25][25] , int m , int n );

    void determinant( float a[25][25] , int n );

    void inverse( float a[25][25] , int m , int n );

    void cofactors( float a[25][25] , int m );

    void add( float a[25][25] , float b[25][25] , int m , int n );

    void multiply( float a[25][25] , float b[25][25] , int m , int n , intq );

    #endif

  • 8/10/2019 Shared Library Project Report

    12/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 12

    B) matrix.c

    #includevoid transpose( float a[25][25] , int m , int n ){

    int i , j;printf("\nIn Transpose Function:");printf("\nThe given matrix is:\n");for( i = 0 ; i < m ; i++ ){

    for( j = 0 ; j < n ; j++ ){

    printf( "%f\t" , a[i][j] );}printf("\n");

    }printf("\nThe Transpose of given matrix is:\n");for( j = 0 ; j < n ; j++ ){

    for( i = 0 ; i < m ; i++ ){

    printf( "%f\t" , a[i][j] );}printf("\n");

    }printf("\nThe transpose function completed!!");

    }

    float determinant( float x[25][25] , int k ){

    float u = 1, det = 0, y[25][25];int i, j, g, h, c;if ( (k == 1) ){

    return ( x[0][0] ) ;}else{

    det = 0;for (c = 0; c < k; c++)

  • 8/10/2019 Shared Library Project Report

    13/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 13

    {g = 0;h = 0;for (i = 0; i < k; i++)

    {for (j = 0; j < k; j++){

    y[i][j] = 0;if (i != 0 && j != c){

    y[g][h] = x[i][j];if (h < (k - 2))

    h++;else{

    h = 0;g++;

    }}

    }}

    det = det + u * (x[0][c] * determinant(y, k - 1));u = -1 * u;}

    }return (det);

    }void inverse( float a[25][25] , int m , int n ){

    float d2 ;

    int i, j , power = 1;if( m == n ){

    d2 = determinant(a, n);printf("\nTHE DETERMINANT IS=%f", d2);if (d2 == 0)

    printf("\nMATRIX IS NOT INVERSIBLE\n");else{

    float b1[25][25], fac[25][25];

  • 8/10/2019 Shared Library Project Report

    14/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 14

    int p, q, r, s;for (q = 0; q < n; q++){

    for (p = 0; p < n; p++)

    {r = 0;s = 0;for (i = 0; i < n; i++){

    for (j = 0; j < n; j++){

    b1[i][j] = 0;if (i != q && j != p){

    b1[r][s] = a[i][j];if (s < (n - 2))

    s++;else{

    s = 0;r++;

    }}}

    }for( i = 0 ; i < ( q+p ) ; i++ ){

    power = power * ( -1 );}

    fac[q][p] = power * (float)determinant(b1, (n - 1));

    }}printf("\nThe Cofactor matrix is:\n");for( i = 0 ; i < q ; i++ ){

    for( j = 0 ; j < p ; j++ ){

    printf("%f\t" , fac[i][j]);}printf("\n");

  • 8/10/2019 Shared Library Project Report

    15/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 15

    }float b2[25][25], inv[25][25], d1;for (i = 0; i < n; i++){

    for (j = 0; j < n; j++){

    b2[i][j] = fac[j][i];}

    }d1 = determinant(a, n);inv[i][j] = 0;for (i = 0; i < n; i++){

    for (j = 0; j < n; j++){

    inv[i][j] = b2[i][j] / d1;}

    }printf("\nTHE INVERSE OF THE MATRIX:\n");for (i = 0; i < n; i++){

    for (j = 0; j < n; j++){printf("\t%f", inv[i][j]);

    }printf("\n");

    }}}else

    {printf("\nThe given matrix is not square!Inverse doesn't

    exist!!");}printf("\nThe inversion operation completed!");

    }

    void cofactors(float num[25][25], int f){

    float b[25][25], fac[25][25];

  • 8/10/2019 Shared Library Project Report

    16/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 16

    int p, q, m, n, i, j , power = 1;for (q = 0; q < f; q++){

    for (p = 0; p < f; p++)

    {m = 0;n = 0;for (i = 0; i < f; i++){

    for (j = 0; j < f; j++){

    b[i][j] = 0;if (i != q && j != p){

    b[m][n] = num[i][j];if (n < (f - 2))

    n++;else{

    n = 0;m++;

    }}}

    }for( i = 0 ; i < ( q+p ) ; i++ ){

    power = power * ( -1 );}

    fac[q][p] = power * determinant(b, f - 1);

    }}printf("\nThe Cofactor matrix is:\n");printf("\n");for( i = 0 ; i < q ; i++ ){

    for( j = 0 ; j < p ; j++ ){

    printf("%f\t" , fac[i][j]);}

  • 8/10/2019 Shared Library Project Report

    17/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 17

    printf("\n");}

    }

    void add( float a[25][25] , float b[25][25] , int m , int n ){

    int i , j;float c[25][25];for(i=0;i

  • 8/10/2019 Shared Library Project Report

    18/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 18

    }}printf("\nProduct of entered matrices:-\n");for ( c = 0 ; c < m ; c++ )

    {for ( d = 0 ; d < q ; d++ ){

    printf("%f\t", mul[c][d]);}printf("\n");

    }printf("\nThe multiplication operation completed!");

    }

    Now , the process of the development of the shared library is as follows.

    4 1 Cr eat ing Ob jec t F i le wi th Posi t ion

    Independent Co de:

    All the code that goes into a shared library needs to be position independent. We can make gcc emit position-independent code by passing it one of the command-line switches -fpic or -fPIC (the former is preferred, unless the modules have grown so large that the relocatable codetable is simply too small in which case the compiler will emit an error message,and you have to use -fPIC).

    First we will create object files for all .c files that goes into ashared library.The following command is used.

    Above we are compiling matrix.c with -fPIC option and generatingmatrix.o object file.

    Now this obtained object file is used to create the shared librarythat has to be linked in order to execute the programs.If there is any error orwarning in the source code of the file , then the object file will not be created.

    gcc -c -fPIC matrix.c -o matrix.o

  • 8/10/2019 Shared Library Project Report

    19/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 19

    4 2 Creat ing Sh ared Library w i th the Ob jec t

    File:

    As we know , every shared library has a prefix "lib", the name of

    the library, the phrase ".so", followed by a period and a version number that isincremented whenever the interface changes (as a special exception, the lowest-level C libraries don't start with "lib").

    So using the object file matrix.o that we obtained in the previousstep , we create the shared library .This is done as follows.

    Option -shared produces a shared object which can then be linked with otherobjects to form an executable .

    On the above command being successful , we obtain a sharedlib1111111rary named libmatrix.so .This shared library is created in thecurrent directory.

    Now , the shared library is created and is ready to be linked andused to execute a program.

    gcc -shared -o libmatrix.so matrix.o

  • 8/10/2019 Shared Library Project Report

    20/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 20

    5.Installing and Using a Shared Library

    5 1 Instal l ing a Shar ed Library:

    The created shared library must be installed first , andthen can be used.This is done by ldconfig .

    This is done by copying the library into one of thestandard directories (e.g., /usr/lib) and run ldconfig . First, you'll need to createthe shared libraries somewhere. Then, you'll need to set up the necessarysymbolic links, in particular a link from a soname to the real name (as well asfrom a versionless soname, that is, a soname that ends in ``.so'' for users whodon't specify a version at all). The approach is to run:

    But many a times , this installing a shared library is notrequired , especially if one is using those libraries for special purposes.Then we

    just follow the approach of using the updated environment variables.

    5 2 Using the Shared L ibrary:

    To use the shared library created , we need to link it tothe application.Let our application in hand be the execution a program main.c with source code:

    #include #include "matrix.h"int main(){

    float a[25][25] , b[25][25] , dtr;int i , j , m , n , p , q , ch;char c;printf("Enter the number of rows of primary matrix Matrix A:\n");scanf( "%d" , &m );printf("Enter the number of columns of primary matrix Matrix A:\n");scanf( "%d" , &n );printf("Enter the elements of primary matrix Matrix A:\n");

    for ( i = 0 ; i < m ; c++ )

    ldconfig -n directory_with_shared_libraries

  • 8/10/2019 Shared Library Project Report

    21/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 21

    for ( j = 0 ; j < n ; d++ )scanf("%d", &a[i][j]);

    printf("Entered elements of primary matrix Matrix A are:\n");for ( i = 0 ; i < m ; c++ )

    {for ( j = 0 ; j < n ; d++ )

    printf("%d\t", a[i][j]);printf("\n");

    }do{

    printf("Enter the number of operation you wanted to perform onthe primary matrix:\n");

    printf("1.Transpose of a matrix\n2.Inverse of a matrix\n3.Add twomatrices\n4.Multiply two matrices\n5.Determinant of a matrix\n6.Cofactormatrix for a matrix\nYour choice is:");

    scanf( "%d" , &ch );switch( ch ){

    case 1 : printf("\n-----TRANSPOSE FUNCTION-----\n");transpose( a , m , n );

    break;case 2 : printf("----INVERSE FUNCTION-----\n");inverse( a , m , n );break;

    case 3 : printf("---------------ADD OPERATION----------------\n");printf("Enter the number of rows of Matrix B:\n");scanf( "%d" , &p );printf("Enter the number of columns of Matrix

    B:\n");

    scanf( "%d" , &q );if( m == p && n == q ){

    printf("Enter the elements of Matrix B:\n");for ( i = 0 ; i < p ; c++ )for ( j = 0 ; j < q ; d++ )

    scanf("%f", &b[i][j]);printf("Entered elements of Matrix B are:\n");for ( i = 0 ; i < p ; c++ ){

  • 8/10/2019 Shared Library Project Report

    22/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 22

    for ( j = 0 ; j < q ; d++ ){

    printf("%f\t", b[i][j]);}

    printf("\n");}add( a , b , m , n );}else{

    printf("\nThe order of matrices are notequal!Matrix Addition is NOT POSSIBLE!!!");

    }break;

    case 4 : printf("------MULTIPLY OPERATION---------\n");printf("Enter the number of rows of Matrix B:\n");scanf( "%d" , &p );

    printf("Enter the number of columns of MatrixB:\n");

    scanf( "%d" , &q );if( n == p )

    { printf("Enter the elements of Matrix B:\n");for ( i = 0 ; i < p ; c++ )for ( j = 0 ; j < q ; d++ )

    scanf("%f", &b[i][j]);printf("Entered elements of Matrix B are:\n");for ( i = 0 ; i < p ; c++ ){

    for ( j = 0 ; j < q ; d++ )

    {printf("%f\t", b[i][j]);

    }printf("\n");

    }multiply( a , b , m , n , q );}

    else{

    printf("\nThe count of rows of first matrix and that of

  • 8/10/2019 Shared Library Project Report

    23/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 23

    the columns of second matrix are not equal!Matrix Multiplication is NOTPOSSIBLE!!!");

    }break;

    case 5: printf(----------DETERMINANT FUNCTION----------);dtr = determinant( a , n );printf(The value of determinant is:%f , dtr );break;

    case 6: printf(-------COFACTOR OPERATION----------);cofactors( a , n );break;

    default : printf("Invalid choice!!!");}printf("Would like to perform another operation?( y / n ):");scanf("%c" , &c);}while( c != 'n' && c == 'y' );if( c != 'n' && c != 'y' )

    printf("Invalid choice!Termination of program!!!");if( c == 'n' )

    printf("Program session successful!!!Completion of program!!!");return(0);

    }

    5.2.1 Compiling main.c:

    The linking of the shared library is done whilecompiling the program main.c.This is done using the following commands:

    In the above command -l option tells the compiler to look for a file namedlibsomething.so . The something is specified by the argument immediatelyfollowing the -l. i.e. lmatrix.

    But,the above command gives error as:

    gcc -o main main.c -lmatrix

    /usr/bin/ld: cannot find -lmatrix

    collect2: ld returned 1 exit status

  • 8/10/2019 Shared Library Project Report

    24/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 24

    This has occurred as the linker doesnt know where to find libmatrix.GCC has alist of places to look by default for shared libraries, but our directory is not inthat list .So that's the reason compilation failed at linking level.

    Now we need to tell GCC where to findlibmatrix.so. We will do that with the -L option as,

    -L option tells the compiler where to find thelibrary. The path to the directory containing the shared libraries is followed by"-L".

    If no -L is specified, the compiler will search the usual locations."-L." means looking for the shared libraries in the current directory and "-L/home/cf/lib" means looking for the shared libraries at "/opt/lib" path. You canspecify as many -l and -L options as you like.

    But the above solution cant alone solve the problem.If we run theexecutable obtained main , we get the following output on terminal:

    So when we use the command ldd on the executable main ,we get

    So if we use the ldd command to view the library dependancies,

    gcc -o main main.c lmatrix -L/home/namburivk/shlib

    [namburivk@NamburiVK ~]$ ./main

    ./main: error while loading shared libraries: libmatrix.so: cannotopen shared object file: No such file or directory

    [namburivk@NamburiVK ~]$

    [namburivk@NamburiVK ~]$ ldd main

    linux-vdso.so.1 => (0x00007fff71fff000)

    libmatrix.so => not found

    libc.so.6 => /lib64/libc.so.6 (0x0000003f95600000)

    /lib64/ld-linux-x86-64.so.2 (0x0000003f95200000)

    [namburivk@NamburiVK ~]$

  • 8/10/2019 Shared Library Project Report

    25/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 25

    We can clearly observe that the libmatrix.so is not found.Now we use theenvironment variables as we are now using the non standard shared libraries.

    5.2.2 Making the library available at run time:

    As we discussed earlier , we update theenvironment variables to link the non standard libraries.This is done as:

    Clearly , now the libmatrix.so location is known by the linker as/home/namburivk/shlib.

    5.2.3 Running the executable:

    The last step in the process is running the

    application , in this case the executable.This is run as:

    5.2.3.1 The Output:

    The output of the above program is as:

    [namburivk@NamburiVK ~]$ ./mainEnter the number of rows of primary matrix Matrix A:

    [namburivk@NamburiVK ~]$ exportLD_LIBRARY_PATH=/opt/lib:/home/namburivk/shlib:$LD_LIBRARY_PATH

    [namburivk@NamburiVK ~]$ gcc -o main main.c -lmatrix - L/home/namburivk/shlib

    [namburivk@NamburiVK ~]$ ldd mainlinux-vdso.so.1 => (0x00007fff2eac3000)

    libmatrix.so => /home/namburivk/shlib/libmatrix.so (0x00007f9c4ec71000)

    libc.so.6 => /lib64/libc.so.6 (0x0000003f95600000)

    /lib64/ld-linux-x86-64.so.2 (0x0000003f95200000)

    [namburivk@NamburiVK ~]$

    [namburivk@NamburiVK ~]$ ./main

  • 8/10/2019 Shared Library Project Report

    26/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 26

    2Enter the number of columns for primary matrix Matrix A:2Enter the elements of primary matrix Matrix A:

    1 23 4Entered elements of primary matrix Matrix A are:1.000000 2.0000003.000000 4.000000Enter the number of operation you wanted to perform on primary matrix:1.Transpose of a matrix2.Inverse of a matrix3.Add two matrices4.Multiply two matrices5.Determinant of a matrix6.Cofactor matrix for a matrixYour choice is:5---------------------DETERMINANT FUNCTION-------------------------The value of determinant is:-2.000000Would you like to perform anotheroperation:nProgram Session successful!!!Completion of program!!!

  • 8/10/2019 Shared Library Project Report

    27/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 27

    6. Examples

    Using the created shared library, matrix.h ,we see two examples.

    6 1 E xample 1 :

    In this example , we try to find the transpose and inverse of amatrix.The source code is as follows:

    #include #include matrix.hvoid main(){

    float a[25][25];int i , j , m , n;printf("Enter the number of rows of matrix :\n");scanf( "%d" , &m );printf("Enter the number of columns of matrix:\n");scanf( "%d" , &n );printf("Enter the elements of matrix:\n");for ( i = 0 ; i < m ; c++ )

    for ( j = 0 ; j < n ; d++ )scanf("%d", &a[i][j]);

    printf("Entered elements of matrix are:\n");for ( i = 0 ; i < m ; c++ ){

    for ( j = 0 ; j < n ; d++ )printf("%d\t", a[i][j]);

    printf("\n");}transpose( a , m , n );inverse( a , m , n );

    }

    The output of Example1 is as follows:

  • 8/10/2019 Shared Library Project Report

    28/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 28

    Run 1:

    [namburivk @NamburiVK ~]$gcc -o eg1 example1.c -lmatrix -L/home/namburivk/shlib

    [namburivk @NamburiVK ~]$./eg1Enter the number of rows of matrix:2Enter the number of columns of matrix:2Enter the elements of matrix:1 23 4Entered elements of matrix are:

    1.000000 2.0000003.000000 4.000000In Traspose Function:The given matrix is:1.000000 2.0000003.000000 4.000000The transpose matrix of the given matrix is:1.000000 3.0000002.000000 4.000000The transpose function completed!!THE DETERMINANT IS: -2.000000The Cofactor matrix is:4.000000 -2.000000-3.000000 1.000000THE INVERSE OF THE MATRIX:-2.000000 1.0000001.500000 -0.500000

    The inverse operation completed!

    Run 2:

    [namburivk @NamburiVK ~]$gcc -o eg1 example1.c -lmatrix -L/home/namburivk/shlib[namburivk @NamburiVK ~]$./eg1Enter the number of rows of matrix:2Enter the number of columns of matrix:

  • 8/10/2019 Shared Library Project Report

    29/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 29

    1Enter the elements of matrix:12

    Entered elements of matrix are:1.0000002.000000In Traspose Function:The given matrix is:1.0000002.000000The transpose matrix of the given matrix is:1.000000 2.000000 The transpose function completed!!The given matrix is not square!Inverse doesn't exist!!

    6.2 Example 2:

    In this example , we find the sum of two matrices , A andB.The source code is as follows:

    #include #include "matrix.h"int main(){

    float a[25][25] , b[25][25] ;int i , j , m , n , p , q ;printf("Enter the number of rows of Matrix A:\n");scanf( "%d" , &m );printf("Enter the number of columns of Matrix A:\n");scanf( "%d" , &n );printf("Enter the elements of Matrix A:\n");for ( i = 0 ; i < m ; c++ )for ( j = 0 ; j < n ; d++ )

    scanf("%d", &a[i][j]);printf("Entered elements of Matrix A are:\n");for ( i = 0 ; i < m ; c++ ){

    for ( j = 0 ; j < n ; d++ )

  • 8/10/2019 Shared Library Project Report

    30/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 30

    printf("%d\t", a[i][j]);printf("\n");

    }printf("Enter the number of rows of Matrix B:\n");

    scanf( "%d" , &p );printf("Enter the number of columns of Matrix B:\n");scanf( "%d" , &q );if( m == p && n == q ){

    printf("Enter the elements of Matrix B:\n");for ( i = 0 ; i < p ; c++ )for ( j = 0 ; j < q ; d++ )

    scanf("%f", &b[i][j]);printf("Entered elements of Matrix B are:\n");for ( i = 0 ; i < p ; c++ ){

    for ( j = 0 ; j < q ; d++ ){

    printf("%f\t", b[i][j]);}printf("\n");

    }add( a , b , m , n );}else{

    printf("\nThe order of matrices are not equal!Matrix Addition isNOT POSSIBLE!!!");

    }

    }

    Run 1:

    [namburivk @NamburiVK ~]$gcc -o eg2 example2.c -lmatrix -L/home/namburivk/shlib[namburivk @NamburiVK ~]$./eg2Enter the number of rows of Matrix A:

    2Enter the number of columns of Matrix A:

  • 8/10/2019 Shared Library Project Report

    31/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 31

    2Enter the elements of Matrix A:1 23 4

    Entered elements of Matrix A are:1.000000 2.0000003.000000 4.000000Enter the number of rows of Matrix B:2Enter the number of columns of Matrix B:2Enter the elements of Matrix B:5 67 8Entered elements of Matrix B are:5.000000 6.0000007.000000 8.000000The addition of two matrices is:6.000000 8.00000010.000000 12.000000The addition operation completed!

    Run 2:

    [namburivk @NamburiVK ~]$gcc -o eg2 example2.c -lmatrix -L/home/namburivk/shlib[namburivk @NamburiVK ~]$./eg2Enter the number of rows of Matrix A:2Enter the number of columns of Matrix A:2Enter the elements of Matrix A:1 23 4Entered elements of Matrix A are:1.000000 2.0000003.000000 4.000000Enter the number of rows of Matrix B:

    2Enter the number of columns of Matrix B:

  • 8/10/2019 Shared Library Project Report

    32/33

    Project Report on Shared Library Development in LINUX

    Namburi Vamsi Krishna , B4 , Dept., of CSE , GIT , GITAM University Page 32

    1The order of matrices are not equal!Matrix Addition is NOT POSSIBLE!!!

    7. Conclusion

    The creation and the usage of the shared libraries has its ownadvantages and therefore is an area which is to be used for efficient

    programming.

    In LINUX , this development of the shared libraries is very easyand at the same time it is effective .But one has to take care of the file system

    placements ,especially while working with non standard libraries.

    The development started with a kernel of release 3.3.4-5.fc17.x86_64.The process walked through all the steps as explained in thisreport .Errors occurred at several stages and they have been removed byrequired measures.The shared library libmatrix.so is now a non standardlibrary and can be used with the applications involving the operations onmatrices.

    Finally , the development of the shared library in LINUX is

    successfully completed and experience is gained throughout the process .The project has met its objectives.

  • 8/10/2019 Shared Library Project Report

    33/33

    Project Report on Shared Library Development in LINUX

    References:

    1. Program Library HOW TO:

    http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

    2.Linux Tutorial YoLinux:

    http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

    3.Library( Computing ) Wikipedia:

    http://en.wikipedia.org/wiki/Library_(computing)