0% found this document useful (0 votes)
77 views

Shared Library Project Report

This document is a project report on developing a shared C library in Linux. It discusses creating a shared library called "matrix" that contains various matrix functions. The report details the Linux shared library naming conventions and development process. It explains how to create object files with position independent code, build the shared library, install it and use it from another C program. Examples are provided to demonstrate how the matrix functions in the shared library can be called.

Uploaded by

NamburiVK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Shared Library Project Report

This document is a project report on developing a shared C library in Linux. It discusses creating a shared library called "matrix" that contains various matrix functions. The report details the Linux shared library naming conventions and development process. It explains how to create object files with position independent code, build the shared library, install it and use it from another C program. Examples are provided to demonstrate how the matrix functions in the shared library can be called.

Uploaded by

NamburiVK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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
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
9
th
October , 2013.

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 by
which the behaviour is invoked.
UNIX is the operating system which will allow its users to create their
own libraries to suit their programming needs. Shared libraries provide
modularity to the development environment as the library code can be changed,
modified and recompiled without having to re-compile the applications that use
this library.
In this report , the development of the shared LINUX C libraries is
explained .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 basic
matrix functions , matrix , is developed. It has the following functions:
1. To display transpose of a matrix
2. To find the inverse of a matrix
3. To add two matrices
4. To multiply two matrices
5. To find the determinant of a square matrix
6. To find the cofactor matrix of a square matrix
In this project report , a detailed explanation of the libraries and their
development and usage is given . It also illustrates this usage with help of
example code runs.







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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 5
1.1 What is a Library? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 5
1.2 Types of Libraries - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 5
2. Shared Libraries - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7
2.1 Deep into Shared Libraries - - - - - - - - - - - - - - - - - - - - - - - 7
2.2 Why Shared Libraries are used? - - - - - - - - - - - - - - - - - - - -7
3. LINUX C Shared Libraries - - - - - - - - - - - - - - - - - - - - - - - - - - - - 8
3.1 LINUX Shared Library Naming Conventions- - - - - - - - - - - -8
3.1.1 Shared Library Names- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -8
3.1.2 File System Placement- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 9
3.1.3 Environment Variables- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -10
4. Shared Library Development- - - - - - - - - - - - - - - - - - - - - - - - -11
4.1 Creating Object File with Position Independent Code- - - - 18
4.2 Creating Shared Library with Object file- - - - - - - - - - - - - - 19
5. Installing and Using Shared Libraries- - - - - - - - - - - - - - - - - -20
5.1 Installing a Shared Library- - - - - - - - - - - - - - - - - - - - - - - - 20
5.2 Using the Shared Library- - - - - - - - - - - - - - - - - - - - - - - - - 20
5.2.1 Compiling main.c-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -23
5.2.2 Making the library available at run time- - - - - - - - - - - - - - - - - - - - - 25
5.2.3 Running the executable- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 25
5.2.3.1 The Output- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -25
6. Examples- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 27
6.1 Example 1- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -27
6.2 Example 2- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -29
7. Conclusion- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -32
Refernences- - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - -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 the
future compilations easy and efficient.
1.1 What is a Library?
A library is a file containing compiled code from various object
files stuffed into a single file. It may contain a group of functions that are used
in a particular context. For example, the pthread library is used when thread
related functions are to be used in the program. This methodology, also known
as "Shared components" or "Archive libraries", groups together multiple
compiled object code files into a single file known as a library.
Typically C functions/C++ classes and methods which can be
shared by more than one application are broken out of the application's source
code, 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 the
multiple use and sharing of software components between applications. It also
allows application vendors a way to simply release an API to interface with an
application. Components which are large can be created for dynamic use, thus
the library remain separate from the executable reducing it's size and thus disk
space used. The library components are then called by various applications for
use when needed.
1.2 Types of Libraries
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 a
caller 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 as
a static build of the program. Historically, libraries could only be static.
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 files
during building/linking to form a single executable, or they may be
loaded at run-time into the address space of the loaded executable at a
static 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 at
load time or run time, rather than being copied by a linker when it creates
a single monolithic executable file for the program.
Shared libraries can be statically linked, meaning that references to
the library modules are resolved and the modules are allocated memory
when 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 shared
objects are not included into the executable component but are tied to
the execution.
Dynamically loaded/unloaded and linked during execution
(i.e. browser plug-in) using the dynamic linking loader system
functions.

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



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 into the Shared Libraries:
Shared Libraries are the libraries that can be linked to any program
at run-time. They provide a means to use code that can be loaded anywhere in
the 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 the
memory footprint can be kept low as a lot of code is kept common in form of a
shared library.
A shared library is loaded into physical memory only once and
reused by multiple processes via virtual memory. When a shared library is
installed properly, all programs that start afterwards automatically use the new
shared library.
2.2 Why Shared Libraries are used?
Shared libraries provide modularity to the development
environment as the library code can be changed, modified and recompiled
without 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 than
one process, and they reduce the size of the executable . They make developing
applications easier.
A small change in the implementation of a function in the library
don'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 adding
arguments to a call or changing the size of a struct . It's actually much more
flexible and sophisticated.
Having shared libraries allows you to create programs that can
break down functions into several different binary files. If there were no shared
libraries all the programs would be a single, massive binary file. Updating
programs would be pain because the entire project would have to be
recompiled.Also, shared libraries allow programmers to reuse existing binary
files so that standard functions don't have to be recompiled again and again.
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 library
when executing a particular program.
Do all this while programs are running using existing libraries.

3.1 LINUX Shared Library Naming Conventions:
For shared libraries to support all of the desired properties, a
number of conventions and guidelines must be followed. One need to
understand the difference between a library's names, in particular its soname
and real name (and how they interact). And also must understand where they
should 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 do
not need to take care about the latest version of the shared library installed in
the 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 the
soname. 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 whenever
the interface changes (as a special exception, the lowest-level C libraries don't
start with lib).
Example : libpthread.so.1
A fully-qualified soname includes as a prefix the
directory it's in; on a working system a fully-qualified soname is simply a
symbolic link to the shared library's real name.
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 is
the filename containing the actual library code. The real name adds to the
soname a period, a minor number, another period, and the release number. The
last period and release number are optional. The minor number and release
number support configuration control by letting you know exactly what
version(s) of the library are installed. These numbers might not be the same as
the numbers used to describe the library in documentation, although that does
make things easier.
Example : libpthread.so.1.1
In addition, there's the name that the compiler uses
when requesting a library, called the linker name, which is simply the
soname without any version number.
Example : libpthread.so
A version number is changed for a shared library
when the changes done in the code make the shared library incompatible with
the previous version. For example, if a function is completely removed then a
new version of the library is required.
A minor number is changed in case there is a
modification in the code that does not make the shared library incompatible
with the previous version being used. For example, a small bug fix won't break
the compatibility of the existing shared library so only a minor number is
changed while version remains the same.

3.1.2 Filesystem Placement
Shared libraries must be placed somewhere in the
filesystem . 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 and
for invoking the installation routines.
According to the Filesystem Hierarchy Standard
(FHS), most libraries should be installed in /usr/lib, but libraries required for
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 be
in /usr/local/lib.

3.1.3 Environment Variables
One can temporarily substitute a different library for
execution. In Linux, the environment variable LD_LIBRARY_PATH is a
colon-separated set of directories where libraries should be searched for first,
before the standard set of directories; this is useful when debugging a new
library or using a nonstandard library for special purposes.This is used in the
following 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 , in
the 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 shared
libraries in LINUX.
Here the development and the execution processes are explained by
creating a shared library , matrix.h and we try to implement the execution of
the programs using this library.
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 shared
library , 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 , int
q );

#endif


Project Report on Shared Library Development in LINUX

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

B) matrix.c

#include<stdio.h>
void 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++)
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];
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");
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];
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]);
}
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<m;i++)
for(j=0;j<n;j++)
{
c[i][j] = a[i][j] + b[i][j] ;
}
printf("\nThe Addition of two Matrices is : \n");
for(i=0;i<m;i++)
{
for( j=0;j<n;j++ )
{
printf("%f\t",c[i][j]);
}
printf("\n");
}
printf("\nThe addition operation completed!");
}

void multiply( float a[25][25] , float b[25][25] , int m , int n , int q )
{
float mul[25][25];
float sum = 0 ;
int c , d , k;
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < n ; k++ )
{
sum = sum + a[c][k] * b[k][d];
}
mul[c][d] = sum;
sum = 0;
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 Creating Object File with Position
Independent Code:
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 code
table 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 a
shared library.The following command is used.

Above we are compiling matrix.c with -fPIC option and generating
matrix.o object file.
Now this obtained object file is used to create the shared library
that has to be linked in order to execute the programs.If there is any error or
warning in the source code of the file , then the object file will not be created.


gcc -c -fPIC matrix.c -o matrix.o
Project Report on Shared Library Development in LINUX

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

4.2 Creating Shared Library with the Object
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 is
incremented 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 previous
step , we create the shared library .This is done as follows.

Option -shared produces a shared object which can then be linked with other
objects to form an executable .
On the above command being successful , we obtain a shared
lib1111111rary named libmatrix.so.This shared library is created in the
current directory.
Now , the shared library is created and is ready to be linked and
used to execute a program.











gcc -shared -o libmatrix.so matrix.o
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 Installing a Shared Library:
The created shared library must be installed first , and
then can be used.This is done by ldconfig.
This is done by copying the library into one of the
standard directories (e.g., /usr/lib) and run ldconfig . First, you'll need to create
the shared libraries somewhere. Then, you'll need to set up the necessary
symbolic links, in particular a link from a soname to the real name (as well as
from a versionless soname, that is, a soname that ends in ``.so'' for users who
don't specify a version at all). The approach is to run:


But many a times , this installing a shared library is not
required , 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 Library:
To use the shared library created , we need to link it to
the application.Let our application in hand be the execution a program main.c
with source code:
#include <stdio.h>
#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
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 on
the primary matrix:\n");
printf("1.Transpose of a matrix\n2.Inverse of a matrix\n3.Add two
matrices\n4.Multiply two matrices\n5.Determinant of a matrix\n6.Cofactor
matrix 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++ )
{
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 not
equal!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 Matrix
B:\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
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 NOT
POSSIBLE!!!");
}
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 while
compiling 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 named
libsomething.so . The something is specified by the argument immediately
following 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
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 a
list of places to look by default for shared libraries, but our directory is not in
that list .So that's the reason compilation failed at linking level.
Now we need to tell GCC where to find
libmatrix.so. We will do that with the -L option as,

-L option tells the compiler where to find the
library. 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 can
specify as many -l and -L options as you like.
But the above solution cant alone solve the problem.If we run the
executable 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: cannot
open 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 ~]$
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 the
environment 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 the
environment 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 ~]$ ./main
Enter the number of rows of primary matrix Matrix A:
[namburivk@NamburiVK ~]$ export
LD_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 main
linux-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
Project Report on Shared Library Development in LINUX

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

2
Enter the number of columns for primary matrix Matrix A:
2
Enter the elements of primary matrix Matrix A:
1 2
3 4
Entered elements of primary matrix Matrix A are:
1.000000 2.000000
3.000000 4.000000
Enter the number of operation you wanted to perform on primary matrix:
1.Transpose of a matrix
2.Inverse of a matrix
3.Add two matrices
4.Multiply two matrices
5.Determinant of a matrix
6.Cofactor matrix for a matrix
Your choice is:5
---------------------DETERMINANT FUNCTION-------------------------
The value of determinant is:-2.000000Would you like to perform another
operation:n
Program Session successful!!!Completion of program!!!













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 Example 1:
In this example , we try to find the transpose and inverse of a
matrix.The source code is as follows:
#include <stdio.h>
#include matrix.h
void 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:


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 ~]$./eg1
Enter the number of rows of matrix:
2
Enter the number of columns of matrix:
2
Enter the elements of matrix:
1 2
3 4
Entered elements of matrix are:
1.000000 2.000000
3.000000 4.000000
In Traspose Function:
The given matrix is:
1.000000 2.000000
3.000000 4.000000
The transpose matrix of the given matrix is:
1.000000 3.000000
2.000000 4.000000
The transpose function completed!!
THE DETERMINANT IS: -2.000000
The Cofactor matrix is:
4.000000 -2.000000
-3.000000 1.000000
THE INVERSE OF THE MATRIX:
-2.000000 1.000000
1.500000 -0.500000
The inverse operation completed!

Run 2:
[namburivk @NamburiVK ~]$gcc -o eg1 example1.c -lmatrix -
L/home/namburivk/shlib
[namburivk @NamburiVK ~]$./eg1
Enter the number of rows of matrix:
2
Enter the number of columns of matrix:
Project Report on Shared Library Development in LINUX

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

1
Enter the elements of matrix:
1
2
Entered elements of matrix are:
1.000000
2.000000
In Traspose Function:
The given matrix is:
1.000000
2.000000
The 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 and
B.The source code is as follows:
#include <stdio.h>
#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++ )
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 is
NOT POSSIBLE!!!");
}

}

Run 1:
[namburivk @NamburiVK ~]$gcc -o eg2 example2.c -lmatrix -
L/home/namburivk/shlib
[namburivk @NamburiVK ~]$./eg2
Enter the number of rows of Matrix A:
2
Enter the number of columns of Matrix A:
Project Report on Shared Library Development in LINUX

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

2
Enter the elements of Matrix A:
1 2
3 4
Entered elements of Matrix A are:
1.000000 2.000000
3.000000 4.000000
Enter the number of rows of Matrix B:
2
Enter the number of columns of Matrix B:
2
Enter the elements of Matrix B:
5 6
7 8
Entered elements of Matrix B are:
5.000000 6.000000
7.000000 8.000000
The addition of two matrices is:
6.000000 8.000000
10.000000 12.000000
The addition operation completed!

Run 2:
[namburivk @NamburiVK ~]$gcc -o eg2 example2.c -lmatrix -
L/home/namburivk/shlib
[namburivk @NamburiVK ~]$./eg2
Enter the number of rows of Matrix A:
2
Enter the number of columns of Matrix A:
2
Enter the elements of Matrix A:
1 2
3 4
Entered elements of Matrix A are:
1.000000 2.000000
3.000000 4.000000
Enter the number of rows of Matrix B:
2
Enter the number of columns of Matrix B:
Project Report on Shared Library Development in LINUX

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

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

7. Conclusion
The creation and the usage of the shared libraries has its own
advantages and therefore is an area which is to be used for efficient
programming.
In LINUX , this development of the shared libraries is very easy
and 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 this
report .Errors occurred at several stages and they have been removed by
required measures.The shared library libmatrix.so is now a non standard
library and can be used with the applications involving the operations on
matrices.
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.










Project Report on Shared Library Development in LINUX

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

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)

You might also like