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

Experiment 10: Create Your Library in Linux Environment and Use It. (A) Power Function (B) Factorial Function (C) Square Root Function

The document describes creating and using functions in a Linux library. It provides code examples for three functions - a power function, factorial function, and square root function. Each function is defined in a .c file, declared in a .h header file, and called from a main program file to demonstrate using the library functions.

Uploaded by

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

Experiment 10: Create Your Library in Linux Environment and Use It. (A) Power Function (B) Factorial Function (C) Square Root Function

The document describes creating and using functions in a Linux library. It provides code examples for three functions - a power function, factorial function, and square root function. Each function is defined in a .c file, declared in a .h header file, and called from a main program file to demonstrate using the library functions.

Uploaded by

Kaitlyn beckham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 10: Create your library in Linux environment and use it.

(a) Power function


(b) Factorial function
(c) Square Root function

1. What is a library? Why create it?


 
Ans :
library is ​a collection of pre-compiled and non-volatile routines used by programs. These
routines, sometimes called modules, can include configuration data, documentation, message
templates, subroutines, classes, values or type specifications. The greatest asset of a library, and
reason for their use, is the ability of reusing the behavior. When you use a library, a program
gains the behavior implemented inside that library without having to implement that behavior
itself.
If you are going to use only some specific framework or you want to get done only some
specific goal, you might not want to learn the whole language and its logic. As with everything
else, learning requires investing your time and effort, languages particularly. In case you are not
in it for a longer run, this investment can become a waste of your resources.
In short, if you want to get a job done fast without hiring and never looking back, the
wiser choice would be to pick the framework or library of your choice, learn all necessary to use
it and then move on.
 
 
2. Explain difference between Static and Dynamic (Shared) Libraries.
Ans:
 
Static Libraries Dynamic Libraries

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.

Performed by linkers Performed by operating System

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.

$ ​cc -o libfoo.so.1 -G -K pic 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 :
 

You might also like