Experiment 10: Create Your Library in Linux Environment and Use It. (A) Power Function (B) Factorial Function (C) Square Root Function
Experiment 10: Create Your Library in Linux Environment and Use It. (A) Power Function (B) Factorial Function (C) Square Root Function
It happens as the last step of the compilation Shared libraries are added during the linking
process. After the program is placed in the process when executable files and libraries are
memory added to the memory.
Static libraries are much bigger in size, Dynamic libraries are much smaller, because
because external programs are built in the there is only one copy of the dynamic library
executable file. that is kept in memory.
Executable files will have to be recompiled if In shared libraries, no need to recompile the
any changes were applied to external files. executable.
Takes longer to execute, because loading into It is faster because shared library code is
the memory happens every time while already in the memory.
executing.
Never has a compatibility issue, since all code Programs are dependent on having a
is in one executable module. compatible library. Dependent programs will
not work if the library gets removed from the
system .
3. Explain use of shared object name for libraries.
Shared objects are one form of output created by the link-editor and are generated by specifying
the -G option. In the following example, the shared object libfoo.so.1 is generated from the input
file foo.c.
A shared object is an indivisible unit that is generated from one or more relocatable objects.
Shared objects can be bound with dynamic executables to form a runnable process. As their
name implies, shared objects can be shared by more than one application. Because of this
potentially far-reaching effect, this chapter describes this form of link-editor output in greater
depth than has been covered in previous chapters.
For a shared object to be bound to a dynamic executable or another shared object, it must first be
available to the link-edit of the required output file. During this link-edit, any input shared
objects are interpreted as if they had been added to the logical address space of the output file
being produced. All the functionality of the shared object is made available to the output file.
Any input shared objects become dependencies of this output file. A small amount of
bookkeeping information is maintained within the output file to describe these dependencies.
The runtime linker interprets this information and completes the processing of these shared
objects as part of creating a runnable process.
1. Power Function
powshared.c :
#include<stdio.h>
int power(int b,int e)
{ if(e>1) return b*power(b,e-1);
return b; }
powshared.h :
#include<stdio.h>
#include "powshared.c"
extern int power(int b,int e);
powmain.h :
#include<stdio.h>
#include "powshared.h"
int main()
{ int b,e;
printf("Enter base = ");
scanf("%d",&b);
printf("Enter exponent = ");
scanf("%d",&e);
printf("%d to the power %d = %d\n",b,e,power(b,e));
return 0; }
2. Factorial Function
factshared.c :
#include<stdio.h>
int fact(int f)
{ if(f>=1) return f*fact(f-1);
else return 1; }
factshared.h :
#include<stdio.h>
#include "factshared.c"
extern int fact(int f);
factmain.c :
#include<stdio.h>
#include "factshared.h"
int main()
{ int f;
printf("Enter a number = ");
scanf("%d",&f);
printf("Factorial = %d\n",fact(f));
return 0; }
3. Square Root Function
sqrootshared.c :
#include<stdio.h>
#include<math.h>
int sqroot(int n)
{ int i,x=1,c=0;
for(i=1;x<=n;i+=2)
{ x=x+i;
c++; }
return c; }
sqrootshared.h :
#include<stdio.h>
#include "sqrootshared.c"
extern int sqroot(int n);
sqrootmain.c :
#include<stdio.h>
#include "sqrootshared.h"
int main()
{ int b;
printf("Enter a number = ");
scanf("%d",&b);
printf("Square root = %d\n",sqroot(b));
return 0; }
Output :