C - Dynamic libraries

C - Dynamic libraries

What’s a library?

It is a compilation of the prototypes of functions that we use in our programs, the functions in programming language C, are declared and stored in a library, to be used at the time we need them, without the need to be copying the code every time this happens. This saves a lot of time when executing a program according to its complexity, accelerating the compilation process, additionally when working in teams this allows easiness in the tasks that are developed, easy reading and better management of the code, here the importance of libraries is evident.

A library in programming language C can be of two types:

  • Shared or dynamic library
  • Static Library

How do libraries work?

Static libraries, unlike dynamics, gather object files into one, while dynamics exist as separate files outside the executable, which means that static libraries add those files as they link, before it becomes an executable binary and dynamic libraries also have object files but are not added during compilation, they are kept separate from the executable binary, they are added to memory so that during execution these object files we need are kept available (linking process, final step of compilation).

The static libraries do not change while until we edit the functions contained in it, the dynamic libraries can at the time of compilation add functions during execution and this will not affect its operation, as previously mentioned, there is no need to compile again and again the program with the dynamic libraries.

How to create a Dynamic Library(Linux)

To create a dynamic library in Linux, simply type the following command:

gcc *.c -c -fPIC        

The?“-fPIC”?flag that we add to the compilation for the creation of dynamic libraries gives the compiler the command that the object code is independent of the position.

Next, type in the following command:

gcc *.o -shared -o liball.so        

The wilcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag.

How to use them?

One difference between dynamic and static libraries is that the former refer to the library that is placed in your program and the latter, on the contrary, copy the entire library.

Here we will link our functions in the code once the dynamic library is created:

gcc -Wall -pedantic -Werror -Wextra -L. main.c  l’name_library‘ -o        

The?“-L”?flag commands the compiler to take the function it needs from the?“.c”?file, then places the library name with the?“-l”?indicator without the?“.so”?extension, at the time of linking it is not important to point out these endings because the compiler assumes it automatically.

Then we add the route of the library with the following command:

export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH        

At the end, if you want to verify that the above process has been done well, you can use the?“nm”?command (this command lists symbols of the object files) along with the name of your library and you will see a “list” of the functions within the library.


pieroramirez@ubuntu:~/ ls -la lib*
-rwxrwxr-x 1 piero ramirez 13632 Jan  7 06:25 liball.so
julien@ubuntu:~/ nm -D liball.so 
0000000000000a90 T _abs
0000000000000aa9 T _atoi
0000000000202048 B __bss_start
                 w __cxa_finalize
0000000000202048 D _edata
0000000000202050 B _end
00000000000011f8 T _fini
                 w __gmon_start__
0000000000000900 T _init
0000000000000bd7 T _isalpha
0000000000000c04 T _isdigit
0000000000000c25 T _islower
0000000000000c46 T _isupper
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses
0000000000000c67 T _memcpy
0000000000000caa T _memset
0000000000000ce9 T _putchar
0000000000000d0e T _puts
0000000000000d4a T _strcat
0000000000000dcf T _strchr
0000000000000e21 T _strcmp
0000000000000e89 T _strcpy
0000000000000eeb T _strlen
0000000000000f15 T _strncat
0000000000000fa5 T _strncpy
0000000000001029 T _strpbrk
000000000000109d T _strspn
0000000000001176 T _strstr
                 U write
pieroramirez@ubuntu:~/         

What are the advantages and drawbacks of each of them?

1. Advantages:

  • Static libraries:

- Library previously loaded in an executable file.

- Not required at runtime or included when running.

- Faster compiling time and speed than linking to individual files and increasing binary code size.

- As the object code is included in the executable, multiple function calls are handled.

  • Dynamic libraries:

-The programs that use them don’t need to be compiled in case something in the code is edited.

-At runtime, a copy of the files is created so they are outside the executable file.

-Memory saving when running multiple library files.

-It doesn’t need to be compiled over and over again in case you have any editions after this process.

2. Disadvantages:

- Long loading times and performance in terms of execution and binding

- Compatibility problems, if you change the library, the program that uses that library may not work and adjustments may need to be made.

No hay texto alternativo para esta imagen

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

社区洞察

其他会员也浏览了