Skip to content
C++ Better Explained
Go back
How to Create a C++ DLL in Visual Studio (Step by Step)
Edit page

How to Create a C++ DLL in Visual Studio (Step by Step)

A DLL (Dynamic Link Library) lets you package C++ code into a reusable component that other programs can load at runtime. It’s how Windows shares code between applications — and once you understand the pattern, it’s straightforward.

This guide walks you through creating a C++ DLL in Visual Studio, exporting a function from it, and then calling that function from a separate program. We’ll build a tiny math library as the example.

What Is a DLL (and Why Use One)?

A DLL is a compiled file full of functions and data that multiple programs can use without baking the code into each one. Think of it as a shared toolbox: write the tools once, then any program can open the box and use them.

Why bother?

The trade-off is that the DLL has to be present and findable when the program runs — more on that pitfall later.

What You’ll Need

Step 1: Create the DLL Project

  1. Open Visual Studio and choose File > New > Project.
  2. In the Create a new project dialog, set Language to C++, Platform to Windows, and Project type to Library.
  3. From the filtered list, select Dynamic-link Library (DLL) and click Next.
  4. Name the project MathLibrary, leave the default location, and click Create.

Visual Studio generates the project with a couple of helper files (a precompiled header, pch.h / pch.cpp). You don’t need to touch those for now.

Step 2: Add a Header That Exports Your Function

A DLL is only useful if other programs can see its functions. You make a function visible — or exported — using __declspec(dllexport).

Add a header file: right-click the project, choose Add > New Item, select Header File (.h), and name it MathLibrary.h. Then add this:

#pragma once

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

extern "C" MATHLIBRARY_API double Add(double a, double b);

This little block is the heart of building a DLL, so let’s unpack it.

Understanding the export macro

You want the same header to work in two situations: when building the DLL, and when using it from another program. The macro handles both:

One header, two behaviors, no manual switching. That’s the standard Windows DLL pattern.

What does extern "C" do here?

C++ “mangles” function names (encodes parameter info into the symbol name). extern "C" turns that off so the exported name stays clean and predictable — which makes the DLL much easier to call from other languages and tools. For a simple C-style function like Add, it’s the safe choice. If you’re new to how declarations and definitions split across files, the header files guide is worth a read.

Step 3: Implement the Function

Open MathLibrary.cpp (or add it) and write the actual code. Include the precompiled header if your project uses one, then your own header:

#include "pch.h"        // remove this line if your project has no pch
#include "MathLibrary.h"

double Add(double a, double b)
{
    return a + b;
}

Notice the implementation itself has no __declspec on it — the export attribute belongs on the declaration in the header.

Step 4: Build the DLL

Choose Build > Build Solution (or press Ctrl+Shift+B). When it succeeds, look in your project’s output folder (for example, x64\Debug). You’ll find two files that matter:

Hold on to both. You’ll use the .lib when compiling the client, and the .dll when running it.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Step 5: Create a Program That Uses the DLL

Now let’s call Add from a separate application.

  1. Add a new project to the solution: File > Add > New Project, choose Console App (C++), and name it MathClient.
  2. Write code that uses the function:
#include <iostream>
#include "MathLibrary.h"

int main()
{
    double result = Add(3.5, 4.0);
    std::cout << "3.5 + 4.0 = " << result << "\n";
    return 0;
}

For this to compile and run, you need to connect three things: the header, the .lib, and the .dll.

Point the client at the header

In the client project’s properties, under C/C++ > General > Additional Include Directories, add the folder that contains MathLibrary.h. (Alternatively, just copy the header into the client project.)

The cleanest way is to let Visual Studio handle it: right-click the client project, choose Add > Reference, and check MathLibrary. Visual Studio then links the .lib automatically.

If you prefer to wire it up manually:

Make the DLL findable at runtime

This is the step everyone forgets. The .lib gets you through compiling, but when the program runs, Windows still needs the actual MathLibrary.dll.

The simplest fix: copy MathLibrary.dll into the same folder as your client .exe (its output folder, e.g. x64\Debug). You can automate this with a post-build step, but copying it by hand is fine while you’re learning.

Set MathClient as the startup project and run it. You should see:

3.5 + 4.0 = 7.5

That’s a working DLL, called from a separate program.

Common Problems and Fixes

“Cannot find MathLibrary.dll” at runtime. The .dll isn’t next to your .exe. Copy it into the executable’s output folder. Remember: the .lib is a compile-time dependency; the .dll is a run-time one.

“Unresolved external symbol” when linking. The client found the header (so it knows Add exists) but not the import library. Double-check the reference to the DLL project, or that MathLibrary.lib is listed in Additional Dependencies with the correct library directory.

The function name looks mangled / other languages can’t call it. You likely dropped extern "C". Add it to keep C-style linkage for exported functions.

Architecture mismatch (x86 vs x64). The DLL and the client must target the same platform. If the DLL is x64, build the client as x64 too. Mixing them causes load failures.

You changed the DLL but the client still uses the old behavior. Rebuild the DLL, then copy the new .dll next to the .exe again. It’s easy to keep running a stale copy.

Quick Recap

Creating a C++ DLL in Visual Studio comes down to five moves:

  1. Create a Dynamic-link Library (DLL) project.
  2. Export functions with a macro built on __declspec(dllexport) / __declspec(dllimport).
  3. Implement the functions in a .cpp file.
  4. Build to produce the .dll and its .lib import library.
  5. In the client, include the header, link the .lib, and place the .dll next to the .exe.

Once that loop clicks, you can package anything — math routines, file utilities, whole class libraries — into reusable DLLs.

Want to strengthen the fundamentals that DLLs build on, like functions, headers, and how compilation and linking actually work? That’s exactly what this site is for:

👉 Get the C++ Better Explained Ebook — $19

Watch the Video Tutorial

Some people learn better by watching. Here’s a video walkthrough that covers the same material:

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Next Post
Orthogonality in Programming: Why Independent Code Wins