How to Create a DLL (Dynamic Link Library) in Visual C++
We all come across dynamic link libraries (DLLs) aka shared libraries while developing Windows programs. These libraries contain definitions of various functions whose declarations have been made in header files. This article shows you how to create a DLL.
What is a DLL?
As the name implies, dynamic link libraries are accessed by a program at run-time rather than at the time of compilation/ linking. Therefore, they do not have to be linked with a program while creating an executable of that program. They are linked dynamically to the program(s) which need to use them at run-time.
How to Create a DLL?
This tutorial shows you how to create a dll i.e. dynamic link library in Visual C++ using Visual Studio IDE.
1. Create a Win32 Console Application project in Visual C++.
2. Click Next.
3. Select the radio button labelled DLL and click Finish.
4. The project is created and it already contains two .cpp files named dllmain.cpp (present in every DLL) and mydll.cpp (source file whose name varies, depending upon the name of the project).
5. Add a header file.
6. Add the following statements in the header file :
#ifdef MYDLL_EXPORTS #define MYDLL_API __declspec(dllexport) #else #define MYDLL_API __declspec(dllimport) #endif
Below these statements, add the declarations of the functions you want to define in your DLL.
For example,
namespace MYDLL { class MYDLL { public: static MYDLL_API double Add(double a, double b); static MYDLL_API double Subtract(double a, double b); static MYDLL_API double Multiply(double a, double b); static MYDLL_API double Divide(double a, double b); }; }
7. In the source file (in this case, mydll.cpp), add the following statements below #include “stdafx.h” :
#include "mydll.h" #include <stdexcept> using namespace std;
After this, add the definitions of the functions declared in the header file.
8. Build the project.
9. The DLL has been created and is present in the Debug folder of the current project. Copy the mydll.lib file to “C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\lib” (depending upon the version of Visual Studio you have).
Please Note that the Program Files (x86) folder exists only on 64-bit versions of Windows. On 32-bit versions of Windows, you need to use the Program Files folder.
Testing the DLL created above
1. Create a new Win32 Console Application project in Visual C++.
2. Click Next.
3. Uncheck the check box labelled Precompiled header and click Finish.
4. Add the following code to the source file of this project :
5. Add “mydll.h” and “mydll.dll” to the folder named mdllcheck.
6. Build this project.
7. The output of this program is displayed below.
You can download the above DLL project here.
Did you like this article? Let us know in your comments below.
How to Create a Static Library in Visual C++
Static Libraries vs Dynamic Libraries : Advantages and Disadvantages
This is really interesting, You're very skilled blogger.
I have joined your rss feed and look forward to seeking more
of your wonderful post. Also, I've shared your site
in my social networks!
Thanks for your feedback. We really appreciate it…
The sole purpose of using DLLs is to share code among different programs. Using DLLs allows you to write a certain piece of code once, and use it with many other programs. You don't need to use DLLs if you have no intention to create a shareable library.
Is it necessary to use DLLs? Can't I just put all my code in .cpp files and .h files?