Dynamic libraries, how to create them and what makes the difference with static libraries.

Dynamic libraries, how to create them and what makes the difference with static libraries.

Why use libraries in programming?

Programmers must establish an order in their work to avoid errors and be efficient. But beyond order and efficiency, there is the facility to make immediate modifications to the programs that are already compiled, in this task the dynamic libraries have the particular function of allowing the programmer to make updates on their programs without having to compile every time they require it.

How to create a dynamic library in Linux?

To create a dynamic library in Linux, we must first make sure that we have in our repository all the .c files that contain the functions that our program will use.

No hay texto alternativo para esta imagen

The gcc command uses the -c flag to create an object file (.o) for each source file (.c) contained in our repository. Then we add the -fPIC flag to make the code position independent. Summarizing the above, we should be left with a line similar to this:

No hay texto alternativo para esta imagen

as a result, we will have created the new object files:

No hay texto alternativo para esta imagen

The next thing we are going to do is to put together these object files that the previous command just created. We are going to place them inside a library. Then we will use the gcc command again, now we will do it with the -shared flag, and then the -o flag to set the name that we will give to the library.

No hay texto alternativo para esta imagen

In this way we have created our dynamic library.

No hay texto alternativo para esta imagen

To verify that the procedure was correct and that we have the correct functions, we can use the following command.

No hay texto alternativo para esta imagen

The result will be similar to the one shown below.

No hay texto alternativo para esta imagen

How to use the dynamic library in Linux?

Now what we have to do is to compile the library with the main.c file. In the environment variable we have to add the location of the files of our library, this way the program will know where to find the functions.

No hay texto alternativo para esta imagen

Then, we compile it by doing the following:

No hay texto alternativo para esta imagen

This time we use the name "all" as an example, the -L option is to make the program find the library, the dot (.) refers to the current directory, and the -1 option is for the compiler to search for the library.

Differences between static libraries and dynamic libraries.

- In static libraries the linker makes a copy of all the functions selected for the library in the executable file.

- In dynamic libraries this step is not necessary.

Advantages and disadvantages of dynamic libraries.

- Advantage of static libraries: At runtime, static libraries are usually much faster because they are embedded in the code.

- Disadvantage of dynamic libraries: On the contrary, at runtime dynamic libraries may take additional time.

- Disadvantage of static libraries: Static libraries because they are embedded in the program, make the program update processes more difficult.

- Advantage of dynamic libraries: Dynamic libraries provide the ease of updating programs since the libraries are independent files.

ldd (List Dynamic Dependencies)

ldd (List Dynamic Dependencies) is a Linux *nix utility that prints a listing of the shared libraries that are required for each of the programs or shared libraries that have been specified on the command line. The Ldd utility was developed by Roland McGrath and Ulrich Drepper. If a program does not appear in the list, it is because a shared library is missing for that program.

ldconfig

It is important to know where to find the dynamic libraries that a program needs to run.

The ldconfig utility scans the directories where the dynamic libraries are located (/lib and /usr/lib) as well as the directories specified in /etc/ld.so.conf and creates the cache (/etc/ld.so.cache) and symbolic links to these libraries, so that ld-linux.so can quickly find them whenever they are needed. This is done when you run ldconfig with no arguments (you can also add the -v option to see the scanned directories and symlinks created):

ar

In Linux,?ar?is the GNU general purpose archiver. (There are non-GNU variants of?ar?in other Unix-like OSes). With the option?c

ar c... archive-name file...        

ranlib

ranlib?doesn't create libraries at all. In Linux,?ranlib?is a legacy program that adds an (uptodate) symbol table to an?ar?archive if it doesn't have one. It's effect is exactly the same as?ar s, with GNU?ar. Historically, before?ar?was equipped to generate a symbol table itself,?ranlib?was the kludge that injected the magic phony file into an archive to enable the linker to pick object files out of it. In non-GNU Unix-like OSes,?ranlib?might still be needed for this purpose. Your example:

ar qc libgraphics.a *.o
ranlib libgraphics.a        

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

社区洞察

其他会员也浏览了