How to load dynamic libraries in C/C++?

Utpal Kumar   3 minute read      

We will see how to load shared libraries in C/C++. We will write a library to convert km to degrees and vice-versa. Then we create a utility program to convert km to degrees and vice-versa using this library.

We will see how to load shared (or dynamic) libraries in C/C++. This approach becomes particularly useful when our project becomes too big to easily manage with one script.

Static vs Dynamic Libraries in C/C++

In C programming, static and dynamic libraries are two different ways of organizing and distributing library code for use in other programs.

A static library is a collection of object files that are linked with a program at compile time to create a single executable file. The code in a static library is compiled into the executable file, and the resulting executable file contains all the necessary code for the program to run, including the code from the library. Static libraries are typically denoted with the file extension “.a” on Unix-based systems or “.lib” on Windows.

A dynamic library, also known as a shared library, is a collection of object files that are loaded by a program at runtime. Unlike a static library, the code in a dynamic library is not compiled into the executable file. Instead, the executable file contains references to the functions and data in the library, and the library is loaded by the operating system when the program is executed. Multiple programs can use the same dynamic library, which can reduce the overall memory usage of the system. Dynamic libraries are typically denoted with the file extension “.so” on Unix-based systems or “.dll” on Windows.

The main difference between static and dynamic libraries is that the code in a static library is linked with the program at compile time, while the code in a dynamic library is loaded at runtime. Static libraries result in larger executable files, but they are easier to distribute because they do not have any external dependencies. Dynamic libraries result in smaller executable files, but they require the library to be present on the system at runtime, which can complicate distribution.

Shared libraries are linked in two stages:

  1. Compile time: linker verifies all the objects required by the program but does not insert them into the executable.
  2. Run time: Dynamic loader of the system checks which shared libraries were linked with the program and loads them into memory.

We will write a library to convert km to degrees and vice-versa. Then we create a utility program to convert km to degrees and vice-versa using this library.

Create a library with functions

Let us create a shared library with functions to convert km to degrees and degrees to km. We save this library as support.c.

Create utility to load the library and perform conversions

Now, we will write a utility program to load the functions from the library based on the user call. The program can be called as below:

  1. To convert 12 degrees to km
     ./convert_km_degree.elf -d 12
    
  2. To convert 1000 km to degrees
     ./convert_km_degree.elf -k 1000
    

Here, we user input the degress using the option “d” and the program will return epicentral distance in kms. While if the user gives option of “k”, then the program will convert values to degrees.

Compile the utility script with shared library

We can write a makefile to compile the above utility program:

cc=gcc
CFLAGS=-Wall -g
BINS=support.dylib convert_km_degree.elf #for Linux use .so not .dylib

all: $(BINS)

%.dylib: %.c
	$(CC) $(FLAGS) -fPIC -shared -o $@ $^

%.elf: %.c
	$(CC) $(CFLAGS) -o $@ $^

clean:
	rm $(BINS)
	rm -r *.dSYM

In my case, I have used Mac (unix) to compile the codes and hence I used .dylib to specify the library. If you are using Linux then use .so instead.

Example run

>> ./convert_km_degree.elf -k 44
44.0000 km --> 0.3960 degrees

>> ./convert_km_degree.elf -d 4.5
4.5000 degrees --> 499.9500 km

>> ./convert_km_degree.elf -k 1000 -d 12 -k 3455
1000.0000 km --> 9.0009 degrees
12.0000 degrees --> 1333.2000 km
3455.0000 km --> 31.0981 degrees

Conclusions

This post mainly aims to give a quick instructions on how to load a shared library in C/C++. It also gives instructions on how to provide user arguments to the program using different options.

Disclaimer of liability

The information provided by the Earth Inversion is made available for educational purposes only.

Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.

UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.


Leave a comment