dynamic and static libraries small peack into them
Static Library :
A static library is a programming concept in which shared libraries with special functionalities, classes or resources are linked to external applications or components, facilitating the creation of stand-alone and executable files. During linking, a static library's external parts are loaded into the address space or merged with application code
Dynamic Library :
A dynamic library is a programming concept in which shared libraries with special functionalities are launched only during program execution, which minimizes overall program size and facilitates improved application performance for reduced memory consumption. In most software programs, distributing specific functionalities into distinct modules allows loading as needed.
When to use Dynamic linking and static linking:
The operating system provides facilities for creating and using dynamically linked shared libraries. With dynamic linking, external symbols referenced in user code and defined in a shared library are resolved by the loader at load time. When you compile a program that uses shared libraries, they are dynamically linked to your program by default. The idea behind shared libraries is to have only one copy of commonly used routines and to maintain this common copy in a unique shared-library segment. These common routines can significantly reduce the size of executable programs, thereby saving disk space.
In statically-linked programs, all code is contained in a single executable module. Library references are more efficient because the library procedures are statically linked into the program. Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system.
how to create a static Library :
1. Create a C file that contains functions in your library.
We have created only one file for simplicity. We can also create multiple files in a library.
2. Create a header file for the library that contains all the prototypes .
3. Compile library files.
4. Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.
how to create a Dinamic library :
To create a dynamic library in Linux, simply type the following command:
gcc -c -fPIC *.c
This command essentially generates one object file .o for each source file .c . The -fPIC flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory. Some operating systems and processors need to build libraries from position-independent code so that they can decide at runtime where they want to load it into memory. The -c options just ensures that each .o file isn’t linked yet.
Next, type in the following command:
gcc *.o -shared -o
liball.so (substitute your desired library name with all) and hit return. The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag. The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so . Other than that though, let your imagination run free when considering names for your dynamic libraries.
Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command:
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH