DEV Community

Cover image for Creating and Using Dynamic Libraries ( C )
Laban🔌💻
Laban🔌💻

Posted on

Creating and Using Dynamic Libraries ( C )

I provided a thorough description of how to make and use static libraries in the post linked to below. Please take a look at it if you need a refresher. I'll concentrate on detailing how to develop and use dynamic libraries in the C programming language in this article.

I'll need to establish a few words before I can explain why using dynamic libraries is important. A function is first and foremost a piece of a program that is named and performs a certain task. By using the previously mentioned function to complete a task instead of having to rewrite that particular block of code later on in the program, functions may be used throughout the program to save time.

Despite the fact that static libraries can be used by numerous programs, once a program has been compiled, it becomes impossible to change the various pieces of object code in an executable file because all the code is contained in a single file that was statically linked during the program's compilation.

The topic of dynamic or shared libraries is now finally brought up. Dynamic libraries, in contrast to static libraries, are made up of independent files that each contain a unique piece of object code. A single object code file is created by dynamically linking these files together. Dynamic libraries also provide additional data that the operating system will need in order to link the library to other programs.

Because each application or program can access the dynamic library without needing a separate copy, as would be the case if we were using static libraries, the inference is that memory is conserved while utilizing dynamic libraries. Static libraries execute more quickly at run-time since the object code for the library's functions is already present in the executable file, even if dynamic libraries allow for source code changes without requiring a complete program recompilation. Multiple function calls are therefore dealt with more effectively than when using dynamic libraries.

How to Create a Dynamic Library (Linux)

Simply type the following command on Linux to build a dynamic library: Press return after typing gcc *.c -c -fPIC. Essentially, this operation generates one object file  .ofor  each source file. The -fPIC flag guarantees position independence for the code.

This implies that the location of where the computer loads the code into memory is irrelevant. Some processors and operating systems must create libraries from position-independent code so that they can choose where to load them into memory during runtime. The -c options just ensures that each .o file isn’t linked yet.

Next, type in the following command: gcc *.o -shared -o liball.so(substitute your desired library name with all) and hit return. The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag. The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so . Other than that though, let your imagination run free when considering names for your dynamic libraries.

Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command: export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

How Do we use it?

First of all, we need to create a test file that uses our functions described in the library. convert that file into object code (in this case is not important to convert it into PIC because it won’t be requested from another directory).
gcc test.c -c

When we have already compiled it, we need to specify that we are linking it to a dynamic library. This could be done by running the command with the file that we are going to link followed by the flag -L. Telling the compiler to look for libraries in the current directory. After that we need to specify the name of the library, in this case, is “l_test”. Note that the name of the library passed is now shorten on the word lib. In this step, we can use that shortcut because the compiler is assuming that all the words beginning with l will be libraries. And as usual, we give a name to our executable program.

gcc test.o -L. l_test -o testlib

If we try to run our executable file that calls the functions that we have described. It will give us an error because by default the system will be searching for libraries in the native libraries directory of the system and our library is in the current directory. We can get away with this error in two ways. The first one is to copy our library to the default searching directory, /lib, /usr/lib, usr/local/lib. By doing that at the run time our library will be in the place that the system would search.

The second one is by giving the program the specific path of where is located the library we want to use before executing the file. In order to get the specific paths, we can run the command “pwd”. Copy and paste it after the following variable LD_LIBRARY_PATH . By doing this we are giving the address of our library for the file to link it at runtime.
LD_LIBRARY_PATH=”:/home/ubuntu/programs/libtest;$LD_LIBRARY_PATH” ./testlib

That is pretty much all, I hope you enjoyed this article and learned something new

Top comments (0)