Prerequisites:
The biggest problem with the C language is that at one point, the program grows so big that a file is not enough to store the functions.
To resolve that, fortunately for us, we have libraries.
It exists two types of libraries: Static and Dynamic.
Static Library
Begin by the static one.
What is it ?
How to create it ?
Step 1: Create object files
gcc -c fileName.c
This command compile fileName.c in object file and name it as fileName.o.
Step 2: Create static library from object files
ar rc lib.a fileName.o
To generate a static library, you have to create an archive with object files.
c option: to create an archive.
r option: to replace older file with new one.
Step 3: Create an index
ranlib lib.
ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols.
Step 4: Generate your library
gcc -L. main.c -llib -o main.out
L option: specifies the path of the library
l option: specifies the library to use (you don’t have to specify the extension)
You can now execute the program.
./main.out
Dynamic Library
And now, dynamic!
What is it ?
How to create it ?
Step 1: Create object file
gcc -fPIC -c fileName.c -o dynFileName.o
f option: PIC makes position independent code
Step 2: Convert object file to dynamic object file
gcc -shared -o dynFileName.so
share option:
Step 3: Create a dynamic library
gcc -o fileName.c dynFileName.so