Static and Dynamic libraries

Static and Dynamic libraries

Imagine you have to run the program you worked for a long time, but in the process you created a huge amount of pieces of code. If you work in your pc it will be easy to use, but if you have to use them on another device, you should have to copy these files, risking losing one of them and asking why your code is not working properly, for these and all another situations there are libraries.

They are a collection of pre-compilated code that can be used all the times and in all the devices that are needed, without the complications of use the disorganized code, today i want to show the advantages and differences between static and dynamic libraries.

How do they work

The main objective of libraries is to reused program code, allows functions and structures been available for been used not only for the main program, but also any other that can used the data that contains the library.

But libraries don′t contain the original source code, to work they need the object files output by the assembler.

How to create them:

The command to create static libraries is the next:

No hay texto alternativo para esta imagen

Otherwise, the command to create dynamic libraries is the next:

No hay texto alternativo para esta imagen

This allows object files been ready to be compiled into a library.

* The -Wall, -pedantic, -Werror and -Wextra flags depend on build requirements, can be omitted if desired.

As you can see, the first difference when create the object files is that we have to use the adittional flag -FPIC

The importance of flag -FPIC is that allows libraries to work no matter where its located in memory in reference to the rest of the code, aditional, allows dynamic libraries been used by more than a program at same time, that′s doesn′t happen with static libraries.

This option allows the operating system to know that it's been used a global offset table (GOT), and all references to your address are relative to it, avoiding the loader would have to modify all the offsets itself, this allows code can be shared through multiple processes.

Next, We have to create the libraries, the dynamic library command is the next:

No hay texto alternativo para esta imagen

While for dynamic libraries, the following command is used:

No hay texto alternativo para esta imagen

The -shared flag compiles all .o files into a sharable library.

Otherwise the ar command archives the library using the header and prototypes that were created to build the library, this makes creating a static library much simpler.

How to use:

No hay texto alternativo para esta imagen

In both cases, this command allows compiler to know where to find the library where it needs to run the program.

With static libraries you can run your program form now, but in dynamic cases you have to do one last instruction:

No hay texto alternativo para esta imagen

This update the?LD_LIBRARY_PATH?environment variable and prepare our working directory to its existing value.

Differences & advantages and drawbacks:

Another advantage of dynamic libraries allows them to be modified without the need to recompile, while static libraries are forced to be recompiled if their code is modified, because it is "locked" in the executable.

Although this can be seen as a great drawback of static libraries, it is also a great advantage, because it avoids corrupting the code it contains, which can make it useless, that is why it is also an additional security resource compared to the dynamic libraries.

Aditional, due all code is inside the library, multiple calls to functions can be made more quickly an easier than a dynamic library.

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

Juan Camilo Bocanegra Osorio的更多文章

  • What Is the Internet of Things?

    What Is the Internet of Things?

    In previous decades, the idea of communication between humans and machines, or between machines, was a concept that…

  • Recursion

    Recursion

    Recursion consists of functions that call themselves, avoiding the use of loops and other iterators, is the process of…

  • C Static Libraries

    C Static Libraries

    Static libraries are a set of functions that are used together, and are stored for use in an executable container, they…

  • Hard and Symbolic links

    Hard and Symbolic links

    Hard and symbolic links are two different methods to point to a file or its data. These belong to the filesystem that…

  • SOFTWARE COMPILATION STEPS

    SOFTWARE COMPILATION STEPS

    The compilation is a step-by-step process in which the source code is converted into machine code. All of this is done…

社区洞察

其他会员也浏览了