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.

C/C++ library programming on Linux – Part one: Static libraries

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!

12 thoughts on “C/C++ library programming on Linux – Part two: Dynamic libraries

  1. A Visitor

    >> I’m very sad to see you blocking ads on TechyTalk.info. In the spirit of open source community and Linux we all love, it would mean a lot to my family and to me if you could take a few seconds to add TechyTalk.info as an exception to your add blocking program. Thanks in advance, hope you enjoy TechyTalk.info.

    Belive me, I’m very happy about it!!

    Reply
  2. Marko Author

    @A Visitor

    Hi Visitor, if you have found this site useful and feel happy about blocking ads while using it that tells a lot about you as a person. Again, I respect your decision and I hope you enjoy TechyTalk.info.

    Reply
  3. thuc

    When I create a My lib: Build lib as client’s gsoap for all application add my lib
    But I have problem when I buil dynamic lib:
    gcc -Wall -fPIC -c lib.c dtmfC.c dtmfClient.c -lgsoap
    ERROR: gcc: -lgsoap: linker input file unused because linking not done

    How to build a lib include lib: gsoap (-lgsoap), PostGresSql (-lpq)

    Reply
    1. Marko Author

      Hello when you are doing linking you don’t specify -c. You can read on it on “man gcc”, here’s the excerpt from that man page “… the -c option says not to run the linker. Then the output consists of object files output by the assembler.”

      Regards,
      Marko

      Reply
  4. zhouz

    Why do the following operation will fail?
    1, gcc -Wall -fPIC -c ctest1.c ctest2.c
    2, gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 ctest1.o ctest2.o
    3, cp libctest.so.1.0 /usr/lib
    4, ln -sf /usr/lib/libctest.so.1.0 /usr/lib/libctest.so.1
    5, ln -sf /usr/lib/libctest.so.1.0 /usr/lib/libctest.so
    6, gcc -Wall cprog.c -lctest -o cprog
    7, ./cprog

    Reply
      1. zhouz

        Thank you for your reply.
        I will copy ctest to /usr/lib directory, according to ld.so searched order, ./cprog operation should not be a problem! But why still tip can’t find library?
        If I carried out the ldconfig, then there is no problem.

        Reply
        1. zhouz

          I’m sorry, I made a mistake. cprog is 64 bit, but I put the library to /usr/lib directory. If copy to /usr/lib64 below no problem.

          Reply
  5. Juan

    Hi Makro,

    I am new to Linux and I would like to explore the possibility of making a DLL from Ubuntu. Would the dynamic libraries you describe in your article work on Windows? I see that they are called they have an .so extension.

    Thanks for the information and help.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *