C++ - Windows - Creating a dynamic-link library .DLL

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.

First of all

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).

Creating a .DLL with Visual Studio

From VS > File > New > Project… > Installed > Visual C++ > Windows Desktop > Dynamic-Link Library (DLL).

Then fill the following text inputs:

  • Name: badprog-dynamic-library
  • Location: C:\dev\cpp\tutorial
  • Solution name: solution-dynamic-library

Once created, the Preprocessor gained a new #define:

  • BADPROGDYNAMICLIBRARY_EXPORTS

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:

  • Class Name: BadprogDynamicLibrary
  • .h file: BadprogDynamicLibrary.h
  • .cpp file: BadprogDynamicLibrary.cpp

You can ride of the badprog-dynamic-library.cpp file.

There is also a new file added automatically by Visual Studio in your explorer:

  • dllmain.cpp

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.

Using the .DLL with Visual Studio

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:

  • Name: badprog-executable
  • Location: C:\dev\cpp\tutorial
  • Solution name: solution-executable

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).

Setting the header file directory

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:

  • C:\dev\cpp\tutorial\solution-dynamic-library\badprog-dynamic-library

Then click Apply > OK.

Setting the linker

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:

  • C:\dev\cpp\tutorial\solution-dynamic-library\x64\Debug

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 > Additional Dependencies > Add the following library name:

  • badprog-dynamic-library.lib

OK > Apply > OK.

After builing your .DLL you'll find it in the following directory:

  • C:\dev\cpp\tutorial\solution-dynamic-library\x64\Debug\badprog-dynamic-library.dll

There is also the .lib file:

  • C:\dev\cpp\tutorial\solution-dynamic-library\x64\Debug\badprog-dynamic-library.lib

The .exe will be in the following directory:

  • C:\dev\cpp\tutorial\solution-executable\x64\Debug\badprog-executable.exe

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:

  • C:\dev\cpp\tutorial\solution-executable\x64\Debug\

Hint

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.

Code

BadprogDynamicLibrary.h

#ifndef BADPROG_DYNAMIC_LIBRARY_H
#define BADPROG_DYNAMIC_LIBRARY_H

#ifdef BADPROGDYNAMICLIBRARY_EXPORTS
#define API_BADPROG_DYNAMIC_LIBRARY __declspec(dllexport)
#else
#define API_BADPROG_DYNAMIC_LIBRARY __declspec(dllimport)
#endif

// Badprog.com

class API_BADPROG_DYNAMIC_LIBRARY BadprogDynamicLibrary {
// ============================================================================
//
// ============================================================================
public:
BadprogDynamicLibrary();
~BadprogDynamicLibrary();

// ============================================================================
//
// ============================================================================
public:
void sayHello();
void sayGoodbye();
};

#endif // !BADPROG_DYNAMIC_LIBRARY_H

 

BadprogDynamicLibrary.cpp

#include "stdafx.h"
#include "BadprogDynamicLibrary.h"
#include <iostream>

// Badprog.com

// ============================================================================
//
// ============================================================================
BadprogDynamicLibrary::BadprogDynamicLibrary() {
}

// ============================================================================
//
// ============================================================================
BadprogDynamicLibrary::~BadprogDynamicLibrary() {
}

// ============================================================================
//
// ============================================================================
void BadprogDynamicLibrary::sayHello() {
std::cout << "Hello from the Badprog DLL! :D" << std::endl;
}

// ============================================================================
//
// ============================================================================
void BadprogDynamicLibrary::sayGoodbye() {
std::cout << "Goodbye from the Badprog DLL! :D" << std::endl;
}

 

badprog-executable.cpp

#include "pch.h"
#include "BadprogDynamicLibrary.h"

// Badprog.com

// ============================================================================
//
// ============================================================================
int main() {
//
BadprogDynamicLibrary badprog;
 
badprog.sayHello();
badprog.sayGoodbye();
}

 

Result

Hello from the Badprog DLL! :D

Goodbye from the Badprog DLL! :D

Conclusion

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. laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.