Dynamic Library
What is a Dynamic Library?
Dynamic libraries consist of separate files containing separate pieces of object code. To form a single piece of object code these files are dynamically linked together. Also, dynamic libraries contain extra information that the operating system will require to link the library to other programs in the system.
for more information on static libraries check out my previous blog at https://howtols.blogspot.com/2019/10/c-programming-static-library.html
for more information on static libraries check out my previous blog at https://howtols.blogspot.com/2019/10/c-programming-static-library.html
Why should we use them?
Only one copy of the shared library is kept in memory, making it much faster to compile programs and significantly reducing the size of the executable program also, its load time will be reduced if the shared library code is already present in memory.
A disadvantage compared to a static library is that dynamic libraries have a slower execution time compared to static libraries and potential compatibility issues if a library is changed without recompiling the library into memory.
How to create a dynamic library?
Okay, suppose we have a directory full of files written in c like this:
After we created out files we will compile them using the (GCC) command on the command prompt.
The GNU Compiler Collection or GCC) is a compiler system produced by the GNU Project supporting various programming languages. For more information on how to use gcc, type the command (man gcc) in your command prompt or terminal. This information is also mentioned in my Static Libraries blog.
next step is to compile all the c files into object files with this line: gcc -fPIC -c *.c
Now that we have made our c files into object files we type this command: gcc *.o -shared -o liball.so
We are telling our computer to compile all the obeject files onto a Dynamic Library. Using the (-shared) flag will make the library dynamic and the naming convention for dynamic libraries is such that each shared library name must start with (lib) and end with (.so).
And it looks like this:
And finally we end up using the command :
(export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH)
You can see the result of this code by using the (printenv) command in the command line.
No comments:
Post a Comment