Applying dynamic and static Libraries in C programming
Written by David Orejuela

Applying dynamic and static Libraries in C programming

Let's start by defining library, a library is a collection of modules stored in object format (1's and 0's) this objects can be called from any other piece of code. In the C programming world these libraries are designed with the help of all the headers files (.h) and (.c) files that the headers call, so speaking of an example (all code can be downloaded here: Github ) let's say that we have our main.c program and that we want to use the add function from a previous calculator code:

#include "operators.h"


int main (void)
{
	printf("Add result: %d\n", add(1, 5));

	return (0);
}

We want to use a function to do the add action, but instead of putting the operators or creating a new function in our code we are gonna reuse one of our previous project's code and put the code into a library.

No hay texto alternativo para esta imagen

The first step is to create the object file from our c code for this task we can use the gcc compiler with the flag -c which is gonna avoid the linking stage in the compilation process (if you want to check more about compilation process click here: Gcc compilation process ) also, in this case, we are gonna apply dynamic linking for this we are gonna need that the address of our functions can be called relative, so we are gonna apply also the flag -fPIC.

No hay texto alternativo para esta imagen

After creating the .o file we are gonna need to create the dynamic library in linux they end with .so while in windows they end with .dll. the -shared flag is telling the compiler that we want a shared object and the -o flag, let's us put our own name, an important fact, the name of our library is all, lib and .so are naming convention that we have to obey. 

No hay texto alternativo para esta imagen

Perfect, now to get our program running we have to compile our c code with our dynamic library.

No hay texto alternativo para esta imagen


At this point you could be wondering what's the use of having libraries in general, well let's suppose that we just don't wanna add numbers, but that we have a really complex mathematical algorithm that we tested a lot in previous projects and we know that it doesn't fail if we want to recycle that code we just create a library with all the important functions that we had and use them in our new project.

Also having a culture of creating libraries is really important because it is gonna give improve the style of creating modules that can be later updated, so our program can be running up to date and we won't have to touch all our code to create a new test.

  • How do they work

Let's get a little deeper about Dynamic libraries, at the end of our compilation process in the linking stage, our main code is gonna be searching, for all the libraries that we need to execute successfully our code, in the case of dynamic linking we are just going to put the address our code needs to access the library in the future.

What about Static libraries? In this case, all our libraries are gonna be copied into our final executable making our executable size a little larger than when we have a Dynamic library, so how do we create and link a static library in c? as the same as with the Dynamic library, we need to create the .o file, the one we used to create for the Dynamic library is gonna be of use for a Static Library, having a relative path is not gonna be trouble when static linking.

No hay texto alternativo para esta imagen

Right now that's all we need to compile our program, but if we had a lot of libraries and we would want to compile them in just one we could use the command:

 ar rcs static_lib.a *.o 

r is used to insert the files into an archive, c create the archive if it doesn't exist without warnings, and s to write an index to each component of the file, but as we have only one file we are not gonna use ar.

No hay texto alternativo para esta imagen

So ... if we can get the same result, what's the difference? well one is still dependent on the library file while the other not we can check this deleting our .so and .o files

No hay texto alternativo para esta imagen

Finally, we know how to create Dynamic and Static Libraries, and have some differences that we would want to consider at the moment of using them in an important project:

No hay texto alternativo para esta imagen

Follow me on twitter: @DavidOrejuela14

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

David Orejuela的更多文章

  • How to use multi-threading to increase your app performance

    How to use multi-threading to increase your app performance

    Sometimes when we code the paradigm of using multiple threads can be a little scary, but multi-threading is a common…

  • Introduction to ELF

    Introduction to ELF

    What is ELF We are not talking about Christmas ELF, here we are talking about the Unix ELF, Executable and Linkable…

  • Analysing Linux series: How is the RAM and VRAM related?

    Analysing Linux series: How is the RAM and VRAM related?

    RAM stands for Random Access Memory, let's break this with a common example, there are some chips that are used for…

  • Analysing Linux series: The /proc Filesystem

    Analysing Linux series: The /proc Filesystem

    Every time we run Software in Linux a process is created and an ID is assigned to that process (starting from 1 and in…

  • Our experience launching Hovify

    Our experience launching Hovify

    “If you spend too much time thinking about a thing, you’ll never get it done.” – Bruce Lee Our project is a…

    1 条评论
  • What happens when you type https://www.holbertonschool.com in your web browser and press Enter

    What happens when you type https://www.holbertonschool.com in your web browser and press Enter

    First, let's stat that you are on your web browser and you type like this: https://www.holbertonschool.

  • Entering the IOT world

    Entering the IOT world

    “If you think that the internet has changed your life, think again. The IoT is about to change it all over again!” —…

  • Machine learning for everybody

    Machine learning for everybody

    “Machine learning will automate jobs that most people thought could only be done by people.” ~Dave Waters Machine…

  • Python the world of objects

    Python the world of objects

    Abstraction is one of those notions that Python tosses out the window, yet expresses very well. - Gordon McMillan…

  • Entering the world of Class and Instance attributes

    Entering the world of Class and Instance attributes

    In the one and only true way. The object-oriented version of 'Spaghetti code' is, of course, 'Lasagna code'.

社区洞察

其他会员也浏览了