Os Exp2
Os Exp2
Experiment 2
AIM : Write a program for creating a static/dynamic link library for complex
number operations and then test this library through linux linker.
Static Linking
Thoery: Static linking is when a program includes all the necessary code, including
libraries, directly into the final executable during compilation. This means
the program doesn’t need to rely on external libraries when it runs, making
it easier to move between different systems. It also runs faster and avoids
dependency issues. However, the downside is that the executable file
becomes larger because the code from libraries is included in each
program.
Code: 1. expo.c → It is the exponential function which help us to calculate e^x and x^y
#include <stdio.h>
#include <math.h>
2. fact.c →It is the factorial function used to calculate the factorial of a number
#include <stdio.h>
3. lib_mylib.h
4. driver.c
#include <stdio.h>
#include "lib_mylib.h"
int main() {
int n;
printf("\nEnter a number to calculate factorial: ");
scanf("%d", &n);
printf("Factorial of %d is: %lld\n", n, factorial(n));
double x, y;
int choice;
printf("\nEnter value for x: ");
scanf("%lf", &x);
if (result != -1) {
printf("Result: %.2f\n", result);
}
return 0;
}
OUTPUT:
Dynamic Linking
Theory: Dynamic linking is when a program uses external libraries during runtime
instead of including them in the program at compile time. This means the
program doesn’t have all the code built into it; instead, it refers to shared
libraries that are loaded when the program runs. This makes the program
smaller and allows multiple programs to use the same library, saving
memory. However, the program relies on the correct version of the library
being available when it runs, and if the library changes, the program might
need to be updated. Dynamic linking is useful for saving space and making
updates easier.
Code: 1.expo.c → To calculate e^x and x^y
#include <stdio.h>
#include <math.h>
#include <stdio.h>
3. lib_mylib.h
4. driver.c
#include <stdio.h>
#include "lib_mylib.h"
int main() {
int n;
double x, y;
int choice;
printf("\nEnter value for x: ");
scanf("%lf", &x);
if (result != -1) {
printf("Result: %.2f\n", result);
}
return 0;
}
Output:
Conclusion: The creation of static and dynamic link libraries for complex number
operations demonstrates the power of modular programming, where
commonly used functionalities can be packaged into reusable libraries. By
testing these libraries with the Linux linker, we gain hands-on experience in
linking compiled code with external libraries, understanding the differences
between static and dynamic linking, and learning how to manage
dependencies efficiently in software development. This process enhances
the portability, maintainability, and flexibility of programs.