C/C++ library programming on Linux – Part two: Dynamic libraries
This is my second article in the "C/C++ library programming on Linux" series. I recommend that you read the first part of this article series where I've explained the whole library thing, and gave an example of creating and using static library.
In this article I will explain dynamic libraries and compare them to static libraries. I will also give an example of creating and using dynamic library.
Dynamic (shared) libraries
Dynamic libraries are different from static libraries in a way that by using them, during compilation process, GCC ads only code "hooks" and soname. That "hooks" and library soname are used during the startup of your application to load correct library and connect "inside" with the "outside" code.
There are many advantages of using dynamic libraries. First of all Linux dynamic loader loads dynamic library only once, and than all programs use same copy of this library code. Imagine how many programs link against standard C library and how many memory would be needed to store one copy of this library for every program using it. That is why modern Linux operating system uses almost exclusively dynamic libraries and doesn't us static libraries. Same can be applied to the hard disk space libraries occupy.
Here's the example of creating dynamic library, linking program with that library and testing is everything working as it should. Code used in this examples is given in the first part of my "C/C++ library programming on Linux" article series. /p>
1 2 3 4 5 6 7
gcc -Wall -fPIC -c ctest1.c ctest2.c gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 ctest1.o ctest2.o ln -sf libctest.so.1.0 libctest.so ln -sf libctest.so.1.0 libctest.so.1 gcc -Wall -L. cprog.c -lctest -o cprog export LD_LIBRARY_PATH=. ./cprog
In the first line of code we create library object code, this is nothing special cause we also did that with our static library. Second line creates dynamic library stored in libctest.so.1.0 file with "libctest.so.1" soname. For an explanation of soname please go to the first part of this article series. After we create dynamic library we must create symlinks used to point GNU Linker and Linux dynamic loader to the proper versions of our library. Now when we have our library, we can compile our program that uses this library and link this program to the library we made earlier. We do this with the fifth line of the upper code. At the end we must set the LD_LIBRARY_PATH to the current directory because Linux dynamic loader uses this variable to search for libraries. We could also add this library to the other paths that GNU Linker and Linux dynamic loader use and then we wouldn't need to adjust LD_LIBRARY_PATH. At the end we run our test program and establish that 100/5 really equals 20 and that means that everything is in order.
Again if you have any questions you can ask here and I’ll do my best to explain. In my next article i will explain special scenario of loading and unloading dynamic libraries during execution of your program. Have fun coding!