How to Create DLL Files: 10 Steps (with Images)

Table of contents:

How to Create DLL Files: 10 Steps (with Images)
How to Create DLL Files: 10 Steps (with Images)
Anonim

DLLs are dynamically linked library files created and controlled with the C++ language. They make it easy to share, store or save simple code. This article will teach you how to create a DLL file with Visual Studio for Windows or Visual Studio for Mac. At installation time, check the option "Desktop development with C++". If you already have Visual Studio but have not checked this option, install it again and check it.

Steps

11227960 1
11227960 1

Step 1. Open Visual Studio

You can find it in the "Applications" folder in the "Start" menu. Since a DLL file is a library of information, it's just a piece of a project, and it usually requires an accompanying application to access it.

  • You can download Visual Studio for Windows from the following address:
  • Download the Mac version from the following address:
  • This article will use code provided by Microsoft to explain how to create a DLL file.
11227960 2
11227960 2

Step 2. Click on the File tab

Find it above the project space (Windows) or at the top of the screen (Mac).

11227960 3
11227960 3

Step 3. Click New and then on Project.

Then the "Create a new project" dialog will be displayed.

11227960 4
11227960 4

Step 4. Set your Language, Platform and Project Type preferences

This is a way to filter the project templates that will be displayed.

  • click in Language and select C++ in the dropdown menu.
11227960 5
11227960 5

Step 5. Click Platform and select Windows in the dropdown menu.

11227960 6
11227960 6

Step 6. Click Project Type and select Library' in the drop-down menu.

11227960 7
11227960 7

Step 7. Click Dynamic Link Library (DLL)

Your choice will be highlighted in blue color. Then click on Advance to continue.

11227960 8
11227960 8

Step 8. Enter a name for the project in the "Name" text box

For example, type something like "Math_Library".

11227960 9
11227960 9

Step 9. Click Create

Now your DLL project has been created.

11227960 10
11227960 10

Step 10. Add a file header to the DLL

To do this, click on "Project" in the menu bar and select "Add new item".

  • Select Visual C++ in the menu on the left side of the dialog box.
  • Select File header (.h) in the center of the header.
  • Enter the name such as "Math_Library.h" in the text field below the menu options.
  • click in Add to generate a blank file header.
11227960 11
11227960 11

Step 11. Enter the following code in the new header

    // Math_Library.h - Contains math function declarations #pragma once #ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API __declspec(dllexport) #else #define MATHLIBRARY_API __declspec(dllimport) #endif // The Fibonacci Recurrence Relation // where it describes a Fibonacci sequence F(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 relationship yields the well-known Fibonacci // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, … // Initializes a Fibonacci relation sequence // so that F (0) = a, F (1) = b. // This function must be called before other functions. extern "C" MATHLIBRARY_API void fibonacci_init(const unsigned long long a, const unsigned long long b); // Produces the next value in the sequence. // Returns true on success and updates current value and index; // Returns false if the value exceeds, leaving the 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 string. extern "C" MATHLIBRARY_API unsigned fibonacci_index();

  • This is sample code obtained from the Microsoft support website.
11227960 12
11227960 12

Step 12. Add a CPP file to the DLL

To do so, click on "Add New Item" in the "Project" menu bar.

  • Select Visual C++ in the menu on the left side of the dialog box.
  • Select C++ File (.cpp) in the center of the dialog box.
  • Enter the name such as "Math_Library.cpp" in the text field below the menu options.
  • click in Add to generate a blank file.
11227960 13
11227960 13

Step 13. Enter the following code into the blank file

    // Math_Library.cpp: Defines the functions exported to the DLL file. #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 applicable static unsigned long long current_; // Current value of static string unsigned index_; // Current position of sequence // Initializes a Fibonacci relation sequence // so that F (0) = a, F (1) = b. // This function must be called before other functions. void fibonacci_init(const unsigned long long a, const unsigned long long b) { index_ = 0; current_ = a; previous_=b; // show a special case when initialized } // Output the next value in the sequence. // Returns true on success, or false if the value exceeds. bool fibonacci_next() { // checks for an exceeded value in the result or position if ((ULLONG_MAX - previous_ < current_) || (UINT_MAX == index_)) { return false; } // Special case when index == 0, returns the value of b case (index_ > 0) { // otherwise calculates the next value of the sequence previous_ += current_; } std::swap(current_, previous_); ++index_; return true; } // Get the current value in the string. unsigned long long fibonacci_current() { return current_; } // Get the current index position in the string. unsigned fibonacci_index() { return index_; }

  • This is sample code obtained from the Microsoft support website.
11227960 14
11227960 14

Step 14. Click Build on the menu bar

Find it above the project space (Windows) or at the top of the screen (Mac).

11227960 15
11227960 15

Step 15. Click Build Solution

Then you should see text similar to the one below:

    1>------ Build started: Project: MathLibrary, Configuration: Debug Win32 ------ 1>MathLibrary.cpp 1>dllmain.cpp 1>Generating Code… 1> Creating 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) ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

  • If the creation of the DLL file is successful, you will find out on this screen. If there were any errors, they will be listed for you to fix.

Popular by topic