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

MathUtils Static Library Lab

The document outlines the steps to create a static library in C called libMathUtils.a, including the implementation of basic mathematical functions in math_utils.c and their declarations in math_utils.h. It details the compilation process of the library and provides a main.c file that demonstrates the usage of the library functions. The lab emphasizes the importance of modular and reusable software development in Linux.

Uploaded by

naeemhuzaifah0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MathUtils Static Library Lab

The document outlines the steps to create a static library in C called libMathUtils.a, including the implementation of basic mathematical functions in math_utils.c and their declarations in math_utils.h. It details the compilation process of the library and provides a main.c file that demonstrates the usage of the library functions. The lab emphasizes the importance of modular and reusable software development in Linux.

Uploaded by

naeemhuzaifah0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C Static Library: libMathUtils.

a
Step 1: math_utils.c
#include "math_utils.h"

void MathUtils_Add(int a, int b, int *result) {


*result = a + b;
}

void MathUtils_Subtract(int a, int b, int *result) {


*result = a - b;
}

void MathUtils_Multiply(int a, int b, int *result) {


*result = a * b;
}

void MathUtils_Divide(int a, int b, float *result) {


*result = (float)a / b;
}

int MathUtils_Factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * MathUtils_Factorial(n - 1);
}

Step 2: math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H

void MathUtils_Add(int a, int b, int *result);


void MathUtils_Subtract(int a, int b, int *result);
void MathUtils_Multiply(int a, int b, int *result);
void MathUtils_Divide(int a, int b, float *result);
int MathUtils_Factorial(int n);

#endif

Step 3: Compile the Static Library


gcc -c math_utils.c -o math_utils.o
ar rcs libMathUtils.a math_utils.o

Step 4: main.c
#include <stdio.h>
#include "math_utils.h"
int main() {
int result;
float div_result;

MathUtils_Add(5, 3, &result);
printf("Add: %d\n", result);

MathUtils_Subtract(10, 4, &result);
printf("Subtract: %d\n", result);

MathUtils_Multiply(6, 7, &result);
printf("Multiply: %d\n", result);

MathUtils_Divide(20, 4, &div_result);
printf("Divide: %.2f\n", div_result);

int fact = MathUtils_Factorial(5);


printf("Factorial: %d\n", fact);

return 0;
}

Step 5: Compile and Link


gcc main.c -L. -lMathUtils -o main

Step 6: Run the Program


./main

Expected Output
Add: 8
Subtract: 6
Multiply: 42
Divide: 5.00
Factorial: 120

Conclusion
This lab provides hands-on experience with creating a C library in
Linux,
which is important for modular and reusable software development.

You might also like