Libraries, my name is Dynamic libraries
"El Ateneo" a very cool and "dynamic" Library in Buenos Aires, Argentina

Libraries, my name is Dynamic libraries

Hi! I'm here again with a new post, although on a topic that we talked about previously: Libraries. Let's start by reviewing a bit.

What are the libraries and why we use in general?

Libraries are a group of files that have a functionality pre-built by third parties, although we can also create our own, and that can be used by any executable. Libraries contain variables and functions within them. It is known as library (or libraries) to certain types of files that we can import or include in our program. These files contain the specifications of different functionalities already built and usable, such as reading from the keyboard or showing something on the screen, among many others. By being able to include these libraries with definitions of different functionalities, we can save a lot of things and time making our coding task simpler.

Some examples of popular libraries are:

stdlib contains a lot of common functions such as atoi.

math a library of functions that perform math calculations.

stdio contains functions that read or write to a file and more.

No alt text provided for this image

What are the differences between static and dynamic libraries?

In one of my previous posts I talked about this

There are two types of libraries, dynamic and statics, in this post we will focus on the Static libraries.

A static library (.a file on linux) is loaded when the program is compiled. The necessary functions of that library are copied into your executable. If you take the executable from one computer to another, the program will work anyway, even if the library is not on the new computer, since the executable has its own copy. The problem is that the executable will be larger, since it carries a copy of the library's functions.

A dynamic library (.so file on linux) is loaded at the time of program execution, as needed. The executable does NOT carry a copy of the library's functions and it needs the library to function. If you take the executable to another computer, you must also take the library with you or make sure it is already there. The advantage is that the executable is usually smaller.

The libraries that are used in many programs or that are in the operating system are usually dynamic, since it is easy for the computer to have them installed and it saves you from having several copies of them in several executables.

The libraries that a single program uses is better to make them static, unless they are many and very large and they generate a gigantic executable.

Because the static library object file (.a) is linked at the end of compilation, each program that requires this library must have its own copy of the relevant library. Unlike the static library, a single shared dynamic library can be loaded into memory at run time and accessible to other programs linked to the same library.

The compiler does not load static libraries at run time; you only need to load the executable file. Instead, static library object files are loaded near the end of compilation during their link phase.

How static libraries work

When you compile your program, the source code for the functions are included in the final executable file. The library is literally copied into or inside your executable file.

This behavior is actually one of the drawbacks of static libraries because it increases the size of their executable files. Also, if the library is updated, your executable will not receive the new updates, unless you recompile it with the new library and distribute the new version.

How dynamic libraries work

In the dynamic libraries we have already loaded into the memory the libraries we want. If not, they will be loaded at the time of program execution. The executable file will have the address in memory of the particular function used in the program.

The full library is not copied to the executable file, but only the addresses in memory of the function used in the program.

Dynamic libraries resolve the problem of the size of the executable file. In addition, if the library is updated, all we have to do is to download only the updated library. It will not be necessary to recompile our program again because our program already has the "address" of the functions used.

How to create a static library

1.  Put all relevant files into one directory including the header (.h ) file containing prototypes for the relevant entities. Make sure that the header (.h) file contains the macros #ifndef <HEADERFILE>_H and #define <HEADERFILE>_H at the top and #endif at the bottom so that the header file is only defined once instead of each time it is called.

2. Batch compile all source (.c) files. Use the -c option so that compiler doesn’t link the object files yet but instead creates counterpart object (.o) file for each source (.c) file.

3. Archive all of the object (.o) files into one static library (.a) file. Use the command option -r to ensure that if the library (.a) file already exists, it will be replaced. The command option -c should be used so that if the file doesn’t exist, it will be created.

4. Move the library (.a) file into the same directory that the entry-point file resides.

5. Now, instead of having to include all relevant file names in the compilation command, only the library (.a) file need be referenced.

How to create a dynamic library?

We will create a dynamic library with 2 functions inside it.

The files inside the library are _functiona.c and the _functionb.c file.

The position-independent code (PIC) is a body of code that, being placed somewhere in the memory. PIC is commonly used for a shared or dynamic libraries, so that the same library code can be loaded in a location in each program address space where it will not overlap any other uses of memory (for example, other shared libraries).

With the compiler gcc, we generate the obj file for each one of these functions (remember when we saw what is the obj file?). After it, we generate the dynamic library with the following commands:

gcc -fPic -c _functiona.c
gcc -fPic -c _functionb.c

We are ready to create our dynamic library, the only thing we need is the following commands. As we saw before in UNIX the file extension for these libraries is .so.

gcc -shared -o libholberton.so _functiona.o _functionb.o

Now, you can index and list the contents of an archive: 

ranlib command in Linux is used to generate index to archive. ranlib generates an index to the contents of an archive and it will be stored in the archive. The index lists each symbol defined by a member of an archive which is simply relocatable object file. You may use nm -s or nm –print-armap to list all of this index. An archive with such an index speeds up the linking to the library and allows routines in the library to call each other without regard to their placement in the archive.

No alt text provided for this image

Syntax:

ranlib [--plugin name] [-DhHvVt] archive

Example: It will generate index to archive as shown in the below example.

No alt text provided for this image

How to use libraries?

We got our dynamic library, libholberton.h, to use it, we can compile a code which requires the functions in that library. This is only and example of a file called main.c

int main ()
{
int a, b;
string = "Hello World! Hello Holberton";
a = _functiona (string);
b = _functionb (string);
return (a * b)

}

And for the final compilation, the only thing you need to do is:

gcc -o main main.c libholberton.so 

One last thing: if you want to use a dynamic library who is out of the actual working directory, you can set up the Global Variable in this way:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

One more thing ;) (steve reference)

If you need it, you can use gcc (the compiler) to link a library file or to look for a library, the option flags you need are -L and -l.

gcc -l links with a library file.

gcc -L looks in directory for library files.

And thats it!



Some commands to work with libraries:

ar — Create or maintain library archives

Description

ar maintains archive libraries. The archive library is a collection of files, typically object files. Using ar, you can create a new library, add members to an existing library, delete members from a library, extract members from a library, and print a table of contents for a library.

A library member is an arbitrary file. Typically, these files are object files or side files, suitable for use by a linkage editor.

If any members of a library are object files, ar creates and maintains an external symbol index for link-editing.

Member names in an archive are only the final component of any path name. When creating a new library member (member) as given on the command line, ar uses the full path name given. When storing the member name in the library, or comparing a member name, ar uses only the final component.

nm — Display symbol table of object, library, or executable files

Description

nm displays the symbol table associated with an object, archive library of objects, or executable files.

ldd — To know the shared library dependencies 

Description

ldd is a Linux command line utility that is used in case a user wants to know the shared library dependencies of an executable or even that of a shared library. You might have noticed many files starting with lib* in /lib and /usr/lib directories of your Linux machine. These files are called libraries. Library is a collection of resources such as subroutines/functions, classes, values or type specifications.

ldconfig —  Configure dynamic linker run-time bindings

Description

ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache is used by the run-time linker, ld.so or ld-linux.soldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.

ldconfig will attempt to deduce the type of ELF libs (i.e., libc5 or libc6/glibc) based on what C libs, if any, the library was linked against.

And that′s it, thanks again for be reading another post about C and libraries.

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

Aaron Valiente的更多文章

社区洞察

其他会员也浏览了