How to start using Dynamic-link libraries (DLL)

This entry was posted on

  • snippet
  • tutorial
  • cpp

Since we are going to be using DLL’s to have 1 single copy of our engine code, let’s understand the basics of DLL’s. For those of you, who are on the fence of whether to use a DLL or not. Here are some of the advantages of using a dll as per MSDN

We will be using __declspec(dllexport) and __declspec(dllimport) to export and import functions, data and objects to and from a DLL. We will mainly be using it to export/import functions or interface declarations and not member variables.

I prefer to have a header file which can encapsulate the above function calls. I will explain it with an example

#ifdef project_EXPORTS
#define project_API __declspec(dllexport)
#define project_API __declspec(dllimport)

For the project from which we want to export functions, project_EXPORTS macro will be defined so that project_API automatically map to __declspec(dllexport). For all other projects, project_API will map to __declspec(dllimport)

Now for whichever interface, we need to export all/the functions all we need to do is include the above header and add the following code

class project_API className

This takes care of exporting/importing all the functions, member variables in the class className.

More like this

Ankur Sheel © 2021
Connect with me