Creating a .DLL is an interesting process that allows a better comprehension how a dynamic-link library works on Windows.
No need to say that we'll use Visual Studio for that.
Ready to dynamic linking?
Let's link.
We need of course Visual Studio 2017 but feel free to use the version of your choice.
We'are going to create a dynamic-link library, at first, then create an application that'll use this library.
It's an easy tutorial so even if you are a beginner this example will help you.
Important note: We are going to create a .DLL in a x64 version, so we'll have to use it with a x64-executable version as well.
Let's start with the library (.DLL).
From VS > File > New > Project… > Installed > Visual C++ > Windows Desktop > Dynamic-Link Library (DLL).
Then fill the following text inputs:
Once created, the Preprocessor gained a new #define:
You can see this in > Right click your badprog-dynamic-library project > Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions.
You should see the BADPROGDYNAMICLIBRARY_EXPORTS define.
Let's add a new class, from Visual Studio > Right click your badprog-dynamic-library project > Add Class… > And write the following:
You can ride of the badprog-dynamic-library.cpp file.
There is also a new file added automatically by Visual Studio in your explorer:
OK, if you try to compile this project it'll work.
And you'll have your first .DLL file.
But of course, if you try to run it, it won't.
Indeed, no executable created with a .dll project.
We're going now to create our executable project that'll use this dynamic library.
So from another instance of Visual Studio > File > New Project… > Installed > Visual C++ > Windows Desktop > Windows Console Application.
Then fill the following text inputs:
At this point, be sure that your configuration is exactly the same as the .DLL.
For that from Visual Studio > Build > Configuration Manager… > Be sure that Active solution platform is set to x64.
Same for the Project contexts (below in the same window of the Configuraiton Manager).
In order to use the .DLL we need to specify where is the header file of this dynamic library.
From Visual Studio > Right click your badprog-executable project > Properties > Configuration Properties > C/C++ > General > Additional Include Directories > Add the following path:
Then click Apply > OK.
Let's now set the library directory: still from Visual Studio > Right click your badprog-executable project > Properties > Configuration Properties > C/C++ > Linker > General > Additional Library Directories > Add the following path:
OK > Apply > OK.
Let's do the same for the library name: still from Visual Studio > Right click your badprog-executable project > Properties > Configuration Properties > C/C++ > Linker > Input > Additional Dependencies > Add the following library name:
OK > Apply > OK.
After builing your .DLL you'll find it in the following directory:
There is also the .lib file:
The .exe will be in the following directory:
So in order to run your executable you'll have to copy the badprog-dymamic-library.dll in the same directory where there is already the badprog-executable.exe file.
That is in this directory:
To avoid copying your .DLL in the executable directory, each time you recompile your executable, there is a way to copy this .DLL directly in the right directory.
For that from Visual Studio > Right click your badprog-executable project > Properties > Configuration Properties > Build Events > Post-Build Event > Command Line > Write this on the same line:
xcopy /y /d "C:\dev\cpp\tutorial\solution-dynamic-library\x64\Debug\ badprog-dynamic-library.dll" "$(OutDir)"
Let's code a bit.
Hello from the Badprog DLL! :D Goodbye from the Badprog DLL! :D
Creating a dynamic-link library wasn't finally such a big deal.
Don't hesitate to adapt it as your needs.
You are now able to create your own dynamic libraries and share them all around the world.
Good job, you did it.
Comments
Martin (not verified)
Thursday, September 24, 2020 - 10:29am
Permalink
I followed the instructions,
I followed the instructions, but did not find badprog-dynamic-library.lib:
only badprog-dynamic-library.dll was created after I compiled badprog-dynamic-library.
I think some instructions are missing.
Mi-K
Thursday, September 24, 2020 - 11:20pm
Permalink
Hello Martin,
Hello Martin,
You can't "compile" the library, you have to "build" it.
Just click "Build", not "Compile".
You can "compile" the .EXE in the second project.
Do you have copy/paste the code for the 3 following files?
If yes delete the x64 folders then try again.
Add new comment