The differences between static and dynamic libraries

Because several programs can use the same shared library, the library will have different addresses in memory for each program.

And we still want our programs to have access to the code wherever it is stored.

Now we can create the ‘mylib’ library with our object file.

Here’s how:Static:$ ar rc libmylib.

a my_file.

oDynamic:$ gcc -shared -o libmylib.

so my_file.

oIn the first case, we use the command ar to create a ‘.

a’ file (a stands for archive), and in the second case we use gcc with flags ‘-shared’, and of course ‘-o’ because we compile from an object file, to create a ‘.

so’ file (so is for shared object).

How to use libraries (Linux only)Now that we have our library, we have to use is to compile and create our program.

Let’s say our entry point is in the ‘main.

c’ file, and we want to name our program ‘test’.

Here’s the command to compile the program with our library in both cases:$ gcc -L.

main.

c -lmylib -o testThe ‘-L’ flag tells the compiler where it needs to look for the library, so in this case where the library is in the current working directory, we just use a dot.

the ‘-lmylib’ tells the compiler to link the code in main.

c with the code in the library my_lib.

Finally, the ‘-o’ flag allows us to give a name to the executable, in this case it will be ‘test’.

This is where dynamic libraries differ from static.

With a static library, we can just run the code now.

But with a dynamic library, it won’t work.

A useful tool to show this is ldd, a command that prints shared libraries dependencies.

In our case, the output of ‘ldd test’ will contain a line that says:libmylib.

so => not foundThis means the loader will not be able to find the library at runtime, so we need to place it in a standard location.

This is done by updating the LD_LIBRARY_PATH environment variable and prepending our working directory to its existing value.

Here’s a simple command to do so:$ export LD_LIBRARY_PATH=.

:$LD_LIBRARY_PATHLet’s try ‘ldd test’ again, and the output will be:libmylib.

so => .

/libmylib.

so (0x00007fd4bf2d9000)On top of ldd for the dependencies, we can use the command ‘nm’ to check the symbols of a dynamic library.

These symbols include the functions that are defined in the library.

For dynamic libraries, we use the -D flag, like this:$ nm -D libmylib.

soOkay, now that we’re sure that the dynamic library has been created and is usable, we can run our program by typing ‘.

/test’ in our command line.

Pros and cons of the two types of librariesThe first advantage that a dynamic library has over a static one is that it takes less space in memory, because a static library will link all the function definitions in the program, whereas a dynamic one will look in the code and see which functions are called, and link only these to the program.

Another advantage of the dynamic library over the static one is that if we modify the code in one of the functions it contains, we don’t need to recompile, we can just execute the program again!.This is not possible with a static library, we would have to recompile it (see the flowchart at the top of this article).

Plus, several programs can use the dynamic library.

The static library also has advantages over the dynamic library.

One of them is that the program using it is faster at execution time.

This is because we have linked the library before runtime, whereas dynamic libraries are actually linked at runtime.

Another one is that all the code of the functions is in the executable file, so there is not problem of compatibility.

ConclusionWhile it might not be the most passioning subject in computer science, people that code in C should know the tools and methods we talked about to create libraries.

Depending on the application, developers can either choose static or dynamic libraries, which are both very useful and practical program building tools.

SourcesShared libraries with GCC on Linux – Cprogramming.

comHow to create and use shared libraries with GCC/G++ on Linuxwww.

cprogramming.

comC++ CookbookFollow these steps: Use your compiler to compile the source files into object files.

If you're using Windows, use the…www.

oreilly.

comShared (dynamic) libraries in the C programming languageLibraries are linked in the final step of C program compilation (where a C program is converted to a form the machine…medium.

comhttps://medium.

com/@nickteixeira/shared-dynamic-libraries-vs-static-libraries-differences-in-performance-2716f5b3c826.. More details

Leave a Reply