How to Create Shared lib :
First, create a source file for your library. Let’s call it mylib.c. Here’s an example of what the file might contain:
printf(“Hello from my library!\n”);
Compile the source file into an object file:
gcc -c -Wall -Werror -fpic mylib.c -o mylib.o
The -c flag tells the compiler to stop after compiling, and not to attempt to link the object file. The -Wall and -Werror flags enable compiler warnings and treat them as errors. The -fpic flag generates position-independent code, which is required for shared libraries. Finally, we specify the input and output files.
Link the object file into a shared library:
gcc -shared -o libmylib.so mylib.o
The -shared flag tells the linker to create a shared library instead of an executable. We specify the output file name, libmylib.so, which follows the standard naming convention for shared libraries.
That’s it! You now have a shared library that can be used by other programs. To use the library in a C program, you would include the header file and link against the library:
gcc -Wall -o myprogram myprogram.c -L. -lmylib
The -L. flag tells the linker to look for the library file in the current directory, and -lmylib specifies that we want to link against libmylib.so.