Posts

Showing posts with the label Library

Process of Creating a Dynamic Library(.so) in C

Steps for creating a Dynamic Library in C Step1 : Implement source code for the library add.c:We have implemented the addition function in the file int add(int a, int b) {      return a+b; } sub.c :Implemented the subtraction function in the file int sub(int a, int b) {      return a-b; } Step2: Compile the sources into position independent relocatable  gcc -c -fpic add.c   O/P: add.o  gcc -c -fpic sub.c   O/P: sub.o Step3: As it has to be loaded into memory, we have to make it into ABI format which can be done by linker.So we will use dynamic linker and create shared object image.      gcc --shared -o libarith.so add.o sub.o Whenever we do file libarith.so ,it shows ELF it means that it can be loaded in the memory whereas the same is not true for static library image. app.c: #include <stdio.h> int main() {    printf("add:%d\t sub:%d\n".add(2,3),sub(2,3)); ...

Process of Creating a Static Library(.a) in C

We will see how we can create a static library in C. Now steps for creating a static library for these two files are Step1 : Implement source code for the library add.c:We have implemented the addition function in the file int add(int a, int b) {      return a+b; } sub.c :Implemented the subtraction function in the file int sub(int a, int b) {      return a-b; } Step2: Compile the sources into relocatable binaries gcc -c add.c     O/P: add.o gcc -c sub.c     O/P: sub.o Step3: Use archive tool to create a static library image ar rcs libarith.a add.o sub.o r  --> replace if existing c  --> create s  --> symbol table libarith,a is the name of the static library and the convention is the library name should start with lib. Step4: If we want to see what are the files present in the static library ar -t libarith.a    (OR) nm -s libarith.a Now the library is created,...