Dynamic Libraries in C for Linux

Dynamic Libraries in C for Linux

Why use libraries:

In C there are two types of libraries (Static library and Dynamic library), some time ago I did a post about one of those types: https://www.dhirubhai.net/pulse/how-use-static-libraries-c-agustin-flom/?trackingId=sOQEFK5%2FxrH8Ub9tpAXK9A%3D%3D. This time we are going to talk about the other way of making a library in C for a Linux OS and this is: Dynamic libraries. As I wrote in the last post, libraries are an important topic because they allow programmers to: reuse their code by creating libraries or to use library functions created by others without having to rewrite the code.

How do they work:

When using a dynamic library, the programmer is referencing that library when it needs to at runtime, this is possible thanks to the dynamic loader. On the other hand, static libraries are not relevant during runtime and are only linked in the last phase of the compilation process. Static libraries insert a copy of their entire code into your program, while dynamic libraries allow for addresses of specific called functions to be accessed at runtime.

How to create Dynamic Libraries on Linux:

In this part I am going to focus on how to create dynamic libraries, if you have any doubts about how to create static libraries please refer to my post on: Static libraries.

The first step is to compile using gcc with all the .c files that we want to add to our library (you could also select only some files instead of all). We are going to use the -c flag to generate the object files (.o) from the source files. Another flag that we are going to use is -fPIC this is to make the code position independent, this will allow using the relative addresses instead of absolute addresses.

gcc -c -fPIC *.c        

The next step will be to create a dynamic library with all the object codes of the .c files that we used, for this example, I will name the library as: libdynamic.so

gcc -shared -o libdynamic.so *.o        

Here we used the -shared flag to indicate to the compiler (gcc) that we are going to create a dynamic library. The -o flag allows us to select the name: libdynamic.so. That's it, with these two steps you have just created a dynamic library.

If you want to verify that your dynamic library was created without problems and that the right functions are in it you can use the following command

nm -D libdynamic.so.        

nm refers to: "name mangling" and the -D flag is used to indicate that the symbol is in the initialized data section (see memory layout in C for more information).

How to use Dynamic Libraries on Linux:

In order to use the dynamic library that you have created, you need to add the location of your dynamic library into the environmental variable LD_LIBRARY_PATH so the compiler knows where to find the object code of the functions you use. To do this you need to use the following code:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH        

For example, if you want to use some functions of the dynamic library that we just created (libdynamic.so) in a file named: main.c and you want to name your executable as: "test" you should do the following:

gcc -L. main.c -ldynamic -o test        

The -L flag is used to tell the program that the library is in the current working directory (because we use a dot) and the -l indicates the name of the library we want to use.

If you want to check that your library is found by the loader you can use the ldd command.

Example of the use of the commands I just described:

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

Main differences between Dynamic and Static libraries:

The main difference between both types of libraries is when the code of our program has access to the libraries and uses the object code of the function that it needs. In the case of static libraries, it is accessed when compiling and in the dynamic libraries, it accesses in runtime (when your program is running).

What are the advantages and drawbacks of each of them:

Static library:

+ Faster than dynamic libraries because the code of the functions that you used is inside the executable file of your program.

+ Another advantage related to the first one is that static libraries are resistant to vulnerabilities because as was stated before it's object code is inside the executable file.

- Increased in memory usage.

Dynamic library:

+ Less memory space than static library.

+ Programs that use the dynamic library don't have to be recompiled when the library is updated.use

- There is the possibility of breaking executables if the dynamic library is corrupt or not present when you want to run your program.

Thanks for reading I hope this was useful! Until next time.

Dario Zalovich

Front End Developer en Blend | Lic. en Dise?o Multimedia | Profesor en Universidad ORT

2 年

Awesome article! Keep on going Agustin!

要查看或添加评论,请登录

Agustin Flom的更多文章

  • Startup School - Ycombinator

    Startup School - Ycombinator

    Espa?ol: Desde hace un tiempo, mi interés se ha centrado en el fascinante ecosistema de las startups, y recientemente…

    1 条评论
  • What happens when you type google.com in your browser

    What happens when you type google.com in your browser

    Introduction In this article, we are going to discuss behind the scenes what happens when we search for a URL (Uniform…

  • Recursion in computer science:

    Recursion in computer science:

    Introduction In programming, a recursive function is known as a function that calls itself over and over until a base…

  • Objects in Python

    Objects in Python

    Introduction: There are different types of programming languages, like: procedural programming language…

  • How to use static libraries in C

    How to use static libraries in C

    Today I want to talk about a topic that can save you a lot of time and that is libraries. There are two types of…

  • GCC process breakdown

    GCC process breakdown

    Introduction: In some programming languages like C you need to compile your code in order to be executed, I would like…

    3 条评论

社区洞察

其他会员也浏览了