Simple way of creating C/C++ DLL (Dynamic Link Library)

Khalil Oumimoun
4 min readSep 11, 2020

In this article, we discuss the use of Dynamic Link Library (DLL) with a simple example. These libraries contain code and data that can be used by third-party applications. By using a DLL, we will be able to promote code reuse and efficient memory usage. This technique will also allow us to modularize out programs into separate components that can each be used as needed.

Introduction to Dynamic Link Library

A DLL file (Dynamic Link Library) is a type of file that contains data and instructions that are used by other programs and can be called when needed. DLL files allow multiple programs to share functions and classes simultaneously. Unlike executable programs, DLL files cannot be run directly but instead must be called upon by some other program.

The word “dynamic” is used to explain that the data is not always available in memory, it is only put to use when the program actively calls for it. DLL files allow to separate a program into multiple unique components which can then be added or removed to include or exclude certain functionalities. This is very useful because it lowers the use of memory since the program does not need to load everything all at once.

C and C++

Microsoft Visual C++ provides multiple extensions to the standard C++ which allow functions and classes to be specified as imported or exported directly in the C++ code. These extensions use the attribute __declspec before a function or class declaration.

Please note that when accessing C functions or classes from C++, the functions must be declared as extern “C” in the C++ code, to inform the compiler not to mangle the functions or classes but rather use the “C” linkage. Because C++ supports function overloading, we can declare more than one functions with the same name and different parameters. We then need to provide the compiler with additional information on the function names to avoid undefined reference errors or linking errors (as C does not support function overloading).

The solution here is to specify extern “C” in our C++ program. When the compiler find an extern “C” block, it ensures that the function names are not mangled — that the compiler emits a binary file with their names unchanged, as a “C” compiler would do. This additional information allows us to avoid linking problems and conflicts in binary code.

Creating DLL

The example below is using Visual Studio 2019 to create and build a simple DLL:

Open Visual Studio and create a new Project. To specify that this is a DLL, we will need to select “Dynamic-Link Library with exports (DLL)”

Microsoft Visual Studio 2019 — New Project
Microsoft Visual Studio 2019 — DLL project

After selecting a name for our project and a save location. We go ahead and open the “Header Files” folder that was generated ([ProjectName].h file):

Microsoft Visual Studio 2019 — Solution Explorer

The header file will be used to declare all our classes and functions. First, we define a C++ macro that exports the functions for third-party applications (Unity) that are going to be using the DLL to access functions and classes. This block is called conditional group.

We can remove the class and function examples since we will be writing our own functions for testing.

We create three simple functions. The first one displays a number, the second one displays a random number and the third one adds two numbers.

Please note that we are using extern “C” to avoid Name mangling as discussed above.

In our Solution Explorer, open the [ProjectName.cpp] file:

Microsoft Visual Studio 2019 — Solution Explorer

Now that we have our test functions. Let us Build our solution, but before, we need to make sure that we are building it to the correct expected 64-bit architecture. We need to compile our libraries for x64 platform (64-bit). To change this in Visual Studio, we can go to the Configuration Manager, then select x64 under “Platform”. Or simply select x64 from the drop-down menu at the top.

Microsoft Visual Studio 2019 — Selecting the correct architecture
Microsoft Visual Studio 2019 — Building process
Microsoft Visual Studio 2019 — Build Console

Our Solution builds successfully

To easily access the generated DLLs, right click on your Solution in the Solution Explorer and select “Open folder in File Explorer”

Microsoft Visual Studio 2019 — File Explorer

Conclusion

This article aims to provide an easy to follow example on how to create a Dynamic Link Library. In the next article, we will discuss how we can use this library from a third party software such as Unity3D to find a simple technique to integrate existing C/C++ code in Unity3D. Allowing us to use plugins to access already existing C/C++ methods and classes directly from our C# Unity script.

--

--

Khalil Oumimoun

Computer Science Grad student and researcher working on VR surgery simulations: