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

Utpal Kumar   2 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.

In C/C++, a library is a file containing several indexed objects (functions, variables, etc), that can be used as a single entity in linking parts of a program. Unix systems have two kinds of libraries - static libraries and shared (or dynamic) libraries. Static libraries are linked into the program during the linking phase of compilation. 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.

Static libraries have the advantage of speed. It can be loaded very fast. However, if the static libraries are huge, they can have issues with the memory. However, shared libraries can be slightly slower, but it has the advantage that it uses far less memory to run our programs.

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