February 26th, 2018

To go to the Github repository, click here.

There is already a lot of stuff on internet, related to dynamic loading of classes from a shared library. However I couldn’t find a simple example and explanation on how to do it with the following conditions :

Introduction

One of the best ways to make a C++ program accept plugins is to use dynamic loading of a class from a library. According to Wikipedia, dynamic loading is the process that allows to retrieve functions and variables from a library. It’s very powerful, for multiple reasons:

Imagine programming an HTTP server, for example. It must ideally have several modules, with different goals for each one (SSL, PHP…). Instead of including all of them in one binary, why not making them shared libraries and load them with dynamic loading?

In this article, I’ll do my best to explain and show you a simple method to load classes from shared libraries, taking care of the conditions listed above.

What we’ll be building

A simple C++ program, running both on UNIX and Windows. It will have a Core part (the executable), responsible for checking the libraries existence, and then to load, use and unload them. The libraries are classes that greet the user from the different Star Wars planets. They share a common interface, IPlanet, which is also known by the Core.

Here is a basic diagram to show how it works:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6080d68f-a614-4361-bb9b-9f55aa27d650/dlloader_architecture-diagram.png

Note that my goal is to show a simple implementation of all of this, therefore adding error handling and/or some optimizations might be good once you get the concept.

Let’s get started!