What is a DLL?
When you develop software on Windows or simply browse through folders on a Windows system, you often come across files with the .dll extension. Moreover, senior members in your team often say things like “package XXX functionality into a DLL” for other modules to use, etc.
Sounds impressive, but what exactly is a DLL?
DLL (Dynamic Link Library) is a common file format in the Microsoft Windows operating system. It is a reusable library that contains code, data, and resources for applications to use during runtime.
By the way, on Linux, there are shared libraries, and on macOS, there are dynamic shared libraries, which are concepts similar to DLLs.
DLL files typically contain a set of functions or routines that can be accessed and used by other programs. Such libraries provide a modular approach, allowing developers to divide their code into small, independent units for easier management and maintenance. When an application needs to use these functions, they can dynamically link to the DLL file and invoke its functions without rewriting the entire functionality.
What are the benefits?
The benefits of using DLLs include saving memory space (as multiple applications can share the same instance of a DLL), simplifying code maintenance and updates (by updating the DLL file without modifying each application using it), and improving program execution efficiency (due to code reusability and optimized functions shared across different applications).
Sounds like a great thing, doesn’t it?
How to export a DLL?
Let’s look at some code for a quick explanation
1
2
3
4
5
6
#include "pch.h"
extern "C" __declspec(dllexport) int MyFirstFunction(int a, int b)
{
return a + b;
}
Here, we’ve written a simple addition function.
__declspec(dllexport) indicates to the compiler that the function should be exported to the DLL so that other modules can use it.
extern "C" is mainly used to inform the C++ compiler that the function should be compiled and linked according to the rules of C language, not the C++ name mangling way.
What does that mean? When we define a function in C++ code, the C++ compiler mangles its name to handle issues like function overloading and type safety. This makes interoperability with other languages (like C, C#, Python, etc.) difficult because different compilers may have different name mangling schemes. In conclusion, when interfacing or interoperating with other languages (like C, C#, Python, etc.) in C++, remember to use extern "C".
Name Mangling
Since we mentioned it, let’s briefly explain.
C++ Name Mangling is a name processing mechanism performed by C++ compilers to support features like function overloading and function templates.
When you define multiple functions with the same name in C++, they may have different parameter types, numbers, or const/volatile qualifiers, resulting in function overloading. To ensure these functions can be correctly identified during linking, the compiler modifies their names, a process known as name mangling.
Name mangling typically involves adding extra characters or numbers to the function names to distinguish them. This allows achieving function overloading while identifying different functions during linking.
For example, if we have the following two functions
1
2
void foo(int x);
void foo(double y);
After name mangling, these two functions might become
1
2
void _Z3fooi(int x);
void _Z3food(double y);
This way, even though these two functions both have the same name foo, their names are different during linking, allowing the compiler to correctly identify and link them.
It’s not meant for humans, you could say.
Alright, that’s a basic understanding. We’ll discuss how to use a DLL once you’ve got it and how to turn classes and structures into DLLs for other modules to call in the future.
