Some of static and more of dynamic libraries

Carolina Ramón
5 min readMay 4, 2021

--

we first start with the concept:

what is a librery and why is important use them?

A library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a .h file (named the "header") and an implementation expressed in a .c file. This .c file might be precompiled or otherwise inaccessible, or it might be available to the programmer. (Note: Libraries may call functions in other libraries such as the Standard C or math libraries to do various tasks.)

As a general rule, headers should contain any declarations and macro definitions (preprocessor #defines) to be "seen" by the other modules in a program.

Possible declarations:

  • struct, union, and enum declarations
  • typedef declarations
  • external function declarations
  • global variable declarations

example:

Here you can see the header of the librery: HOLBERTON_H and you can see too some funtions than can be use is yoy declare in othre file.

Here are 4 simple reasons that make libraries important:

1. They work

One of the most important reasons you should use library functions is simply because they work. These functions have gone through multiple rigorous testing and are easy to use.

2. The functions are optimized for performance

Since, the functions are “standard library” functions, a dedicated group of developers constantly make them better. In the process, they are able to create the most efficient code optimized for maximum performance.

3. It saves considerable development time

Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn’t worry about creating them once again.

4. The functions are portable

With ever-changing real-world needs, your application is expected to work every time, everywhere. And, these library functions help you in that they do the same thing on every computer.

some of the most used libraries are:

How do they work

It´s simple, C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

How to create them

Before this, you need to know something important. There are two types of libraries static libreries and dynamic libreries.

Basically the difference between the two is Static library is a collection of object files, while dynamic or shared library is a collection of functions compiled and stored in an executable with purpose of being linked by other programs at run-time. For more information about statis libreries, clic here

Now we can continue with how to create them. The static libraries ypu can create follow the next steps:

1- Compile library files.

gcc -c lib_mylib.c -o lib_mylib.o

2- Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.

ar rcs lib_mylib.a lib_mylib.o

and the dynamic librery like this:

  1. compile use this commandgcc -fPIC -c *.c

The flag -fPIC stands for Position Independent Code, a characteristic required by shared libraries.

2. create the library named

gcc -shared -Wl,-soname,libtools.so -o libtools.so *.o

The -shared key tells the compiler to produce a shared object which can then be linked with other objects to form an executable. -Wl flag passes an options to linker with following format -Wl,options, in case of our example it sets the name of library, as it will be passed to the linker.

How to use them?

we have add a path to the library to the LD_LIBRARY_PATH environment variable like this:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Other way to make it work is Move the library to /usr/local/lib . Now we need to run ldconfig on the directory you moved it to. This will add our library to a cache which is searched through when a program looks for a shared library:

$ ldconfig /usr/local/lib

Finally to use our new dynamic library we can compile as follows:

gcc our_sources.c -L. -ltools -o resulted_program

The -L flag specifies the path to the library, in our case it is current directory, and -l flag specifies the name of the library to use.

What are the advantages and drawbacks of each of them?

Static libraries:

Advantages:

Your application is standalone i.e. does not have any runtime dependency.

The loading time is faster

Suitable for short-scale projects.

Drawbacks:

The final executable size is bigger as it contains the code of library functions as well as your own code.

The compilation time is bigger e.g. compiler has to create new executable even for any small change.

Dynamic libraries:

Advantages:

Save the disk sapce and program size is smaller.

Programs in different programming language can still use the same dynamic library.

Drawbacks:

Your application is not standalone. if DLL does not exist at loading time, your program will fail.

If DLL/.so files updated with newer version and new version is not back compatible, your application will fail.

I hope this information is useful for everyone. A virtual hug

--

--

No responses yet