Static vs Dynamic libraries.

The more code we write, the more we realize that some parts or chunks of code are repeatedly used in many programs. Something as repetitive like a mathematic operation can be re-used over and over in virtually every program we write.

A library allows us to have those repetitive functions outside the specific programs and be able to utilize them whenever we need. There are huge advantages associated to that, being the most evident not having to re-write the code over an over again every time we need to use the same function.

Libraries are already-compiled and usage-ready functions to be used by any program we write.

In order to insert our code in a library, it must be organized in the following way:

  • One or more .c source files containing the code for our functions.
  • One or more .h header files containing the types (typedefs, structs) and prototypes of the functions we want to have available for utilization.
int sum (int a, int b)
{
return a+b;
}
int sub (int a, int b)
{
return a-b;
}
library1.h#ifndef _LIBRARY_1_H
#define _LIBRARY_1_H
int sum (int a, int b);
int sub (int a, int b);
#endif

Static and dynamic.

The Uruguay’s Congress library. Totally unrelated to this article subject but it looks cool.

In Linux we can create two types of libraries: statics and dynamics.

An static library is “copied” into our program when we compile it. Once we have the executable file for our program, the library is no longer used by it. In fact, we could even delete it and the program would continue to work properly as it has copied everything it requires from the library. By the way, the program would only copy what it requires from the library. If the library contains two functions but only one is called by the program, that would be the only one copied over.

A dynamic library is NOT copied into the program. Whenever we are running our executable, each time the code requires something from the library it will go and get it from there. If the library is deleted, the program will return an error.

What are the advantages and disadvantages of each type of library?

  • A program compiled with static libraries is larger, as a copy of everything required from the library is created. For static libraries memory footprints are also larger.
  • A program compiled with static libraries can even be moved over to a different computer without the need to move the libraries along with it. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.
  • A program compiled with static libraries is (at least on paper) faster on its execution. When it calls a function from the library, it is actually on its own code. Therefore, there is no need to access the library file to retrieve information from it.
  • One drawback of static libraries is, for any change in the static libraries, you have to recompile the main program every time.
  • If we change or even remove a static library, already created executables are not affected. However, if we change a dynamic library, executable will be affected. This could be an advantage, if we have amended the library in order to correct an error (as the error will be fixed in all executables).
  • Dynamic libraries will conserve memory since each program can access the library without creating an individual copy of the functions.

Ultimately, deciding what type of library will be used is a negotiation between the advantages and inconveniences of each one, affected by the purpose of the software we are designing.

For smaller, simpler programs, static libraries are often used. Dynamic libraries are commonly used for larger programs or system libraries.

In Unix, static libraries are normally named libname.a and dynamic libraries are named libname.so.

Creating static libraries using GNU.

Let’s assume we have a (very) simple C program called lib_mylib.c:

We also have a header .h file with the function prototype on it:

void fun(void);#endif

In order to create a library (regardless of it being static or dynamic), we must first obtain the .o files from our .c files. The .o or object code files contain the compiled content of the corresponding .c file.

So, in order to create the .o file from the lib_mylib.c program, we must do the following entry:

In order to create the static library we must use the command ar (ar as in archive). This step is to bundle multiple object files in one static library:

From this point on the library can be called from another program. In order to test this, let’s create a program called driver that uses the newly created static library:

void main(){
fun();
}

Let’s then compile it:

We must link the compiled driver program to the static library. Note that -L. is used to tell that the static library is in current folder.

In the above command it is worth noting that the source code, driver in this case, needs to be listed before the -l flag. The expression, -l combined with _mylib tells the compiler to look for an archive called lib_mylib.a. This is why is so important to use the standard format for naming the libraries.

Finally, we can execute the driver program:

Creating dynamic libraries using GNU.

The process starts in a similar way than before, by creating the .o files out of the source .c files. That is done quick and easy with the following command:

The -fPIC flag is an instruction for the code to be position-independent. This means that the generated machine code is not dependent on being located at a specific address in order to work. The -c flag ensures that each .o file is not linked yet.

Next step is to enter this command:

The -shared flag specifies that you intend to create a dynamic library.

Finally we must export the path for libraries for programs to know where to look for the specific library. This is done by using this command:

Let’s go a little deeper on this last step.

In Linux, the environment variable LD_LIBRARY_PATH is set of directories where libraries should be searched for first. It tells the dynamic link loader where to search for the dynamic shared libraries an application was linked against. The directories specified in LD_LIBRARY_PATH get searched before the standard locations.

A few useful tools when working with libraries:

The nm command will list symbols from object files, or when used when -D (“D” as in “dynamic”), it will basically show you the content of the library:

The ldd command will list the shared libraries required by each program or shared library specified on the command line. You can do this simply by typing ldd and the name of your executable file:

Finally ldconfig is used to create 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).

ldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.