The DLL files (from the English Dynamic-Linked Library) represent Windows dynamic libraries created and managed through the C ++ programming language. The purpose of DLLs is to simplify the sharing and management of programming code. This article explains how to create a DLL file using Visual Studio, a Windows app, or Visual Studio for Mac. During installation, make sure the "Develop desktop applications with C ++" checkbox is selected. If you have already installed Visual Studio, but did not include the installation of the indicated component, you will need to run the installation wizard again to update your development environment.
Steps
Step 1. Launch Visual Studio
You can do this from the "Start" menu or the "Applications" folder. Since a DLL file is nothing more than a library containing compiled code, it is only a small piece of a project and often requires the use of an application in order to be used or to have access to its contents.
- You can download Visual Studio for Windows from this link:
- Visual Studio for Mac can be downloaded from this link:
- This article uses sample source code provided directly by Microsoft to explain how to create and compile a DLL.
Step 2. Click on the File menu
It is located at the top of the program window (on Windows) or the screen (on Mac).
Step 3. Click on the New item and choose the option Project.
The "Create a new project" dialog will appear.
Step 4. Set the Language, Platform and Project Type options
It is a series of filters based on which the list of project templates available to you will be created.
Click on the drop-down menu Language and click on the option C ++.
Step 5. Click on the Platform drop-down menu and choose the option Windows.
Step 6. Click on the Project Type menu and choose the option Bookshelf.
Step 7. Click on the Dynamic Link Library (DLL) entry
The selected option will be displayed in blue. At this point click on the button Come on to continue.
Step 8. Name your project by typing it in the "Name" text box
For example, use the name "MathLibrary".
Step 9. Click the Create button
The project for creating a DLL will be automatically prepared by Visual Studio
Step 10. Add a header file for the DLL
Click on the "Add New Item" option from the "Project" menu.
- Select the option Visual C ++ from the menu located on the left side of the dialog box that appeared.
- Select the item Header file (.h) from the main pane of the dialog box.
- Type the name "MathLibrary.h" in the text field visible at the bottom of the window.
- Click on the button add to generate an empty header file.
Step 11. Insert the following source code inside the header file you just created
The sample code was provided directly from the Microsoft website.
// MathLibrary.h - Contains declarations of math functions #pragma once #ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API _declspec (dllexport) #else #define MATHLIBRARY_API _declspec (dllimport) // Facciendifurrence sequence // describes The Furrence // sequence where the relation n) is {n = 0, a // {n = 1, b // {n> 1, F (n-2) + F (n-1) // for some initial integral values a and b. // If the sequence is initialized F (0) = 1, F (1) = 1, // then this relation produces the well-known Fibonacci // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34,… // Initialize a Fibonacci relation sequence // such that F (0) = a, F (1) = b. // This function must be called before any other function. extern "C" MATHLIBRARY_API void fibonacci_init (const unsigned long long a, const unsigned long long b); // Produce the next value in the sequence. // Returns true on success and updates current value and index; // false on overflow, leaves current value and index unchanged. extern "C" MATHLIBRARY_API bool fibonacci_next (); // Get the current value in the sequence. extern "C" MATHLIBRARY_API unsigned long long fibonacci_current (); // Get the position of the current value in the sequence. extern "C" MATHLIBRARY_API unsigned fibonacci_index ();
Step 12. Add a CPP file to the DLL
Click on the Add New Item option from the "Project" menu.
- Select the item "Visual C ++" from the menu located on the left side of the window.
- Choose the item "C ++ File (.cpp)" from the central pane of the window.
- Type the name "MathLibrary.cpp" into the "Name" field located at the bottom of the window.
- Click the Add button to generate an empty file.
Step 13. Paste the following code into the blank file you just created
// MathLibrary.cpp: Defines the exported functions for the DLL. #include "stdafx.h" // use pch.h in Visual Studio 2019 #include #include #include "MathLibrary.h" // DLL internal state variables: static unsigned long long previous_; // Previous value, if any static unsigned long long current_; // Current sequence value static unsigned index_; // Current seq. position // Initialize a Fibonacci relation sequence // such that F (0) = a, F (1) = b. // This function must be called before any other function. void fibonacci_init (const unsigned long long a, const unsigned long long b) {index_ = 0; current_ = a; previous_ = b; // see special case when initialized} // Produce the next value in the sequence. // Returns true on success, false on overflow. bool fibonacci_next () {// check to see if we'd overflow result or position if ((ULLONG_MAX - previous_ <current_) || (UINT_MAX == index_)) {return false; } // Special case when index == 0, just return b value if (index_> 0) {// otherwise, calculate next sequence value previous_ + = current_; } std:: swap (current_, previous_); ++ index_; return true; } // Get the current value in the sequence. unsigned long long fibonacci_current () {return current_; } // Get the current index position in the sequence. unsigned fibonacci_index () {return index_; }
Step 14. Click on the Compile menu
It is located at the top of the project window (on Windows) or along the top of the screen (on Mac).
Step 15. Click on the Compile Solution option
After clicking on the indicated option you will see a text similar to the following:
1> ------ Start compilation: Project: MathLibrary, Configuration: Debug Win32 ------ 1> MathLibrary.cpp 1> dllmain.cpp 1> Generate code… 1> Create library C: / Users / username / Source / Repos / MathLibrary / Debug / MathLibrary.lib and object C: / Users / username / Source / Repos / MathLibrary / Debug / MathLibrary.exp 1> MathLibrary.vcxproj -> C: / Users / username / Source / Repos / MathLibrary / Debug / MathLibrary.dll 1> MathLibrary.vcxproj -> C: / Users / username / Source / Repos / MathLibrary / Debug / MathLibrary.pdb (Partial PDB) ========== Compilation: 1 completed, 0 failed, 0 updated, 0 ignored ==========