You are on page 1of 7

19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)

http://msdn.microsoft.com/en-us/library/ms235636.aspx 1/7
Walkthrough: Creating and Using a Dynamic
Link Library (C++)
This step-by-step walkthrough shows how to create a dynamic link library (DLL) for use with a C++ app. Using a library is a
great way to reuse code. Rather than re-implementing the same routines in every program that you create, you write them
one time and then reference them from apps that require the functionality. By putting code in the DLL, you save space in
every app that references it, and you can update the DLL without recompiling all of the apps. For more information about
DLLs, see DLLs in Visual C++.
This walkthrough covers these tasks:
Creating a DLL project.
Adding a class to the DLL.
Creating a console app that uses load-time dynamic linking to reference the DLL.
Using the functionality from the class in the app.
Running the app.
This walkthrough creates a DLL that can only be called from apps that use C++ calling conventions. For information about
how to create DLLs for use with other languages, see Calling DLL Functions from Visual Basic Applications.
Prerequisites
This topic assumes that you understand the fundamentals of the C++ language.
To create a dynamic link library (DLL) project
1. On the menu bar, choose File, New, Project.
2. In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Win32.
3. In the center pane, select Win32 Console Application.
4. Specify a name for the projectfor example, MathFuncsDllin the Name box. Specify a name for the solutionfor
example, DynamicLibraryin the Solution name box. Choose the OK button.
5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
6. On the Application Settings page, under Application type, select DLL.
7. Choose the Finish button to create the project.
To add a class to the dynamic link library
1. To create a header file for a new class, on the menu bar, choose Project, Add New Item. In the Add New Item
Visual Studio 2013 18 out of 32 rated this helpful
19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)
http://msdn.microsoft.com/en-us/library/ms235636.aspx 2/7
dialog box, in the left pane, under Visual C++, select Code. In the center pane, select Header File (.h). Specify a
name for the header filefor example, MathFuncsDll.hand then choose the Add button. A blank header file is
displayed.
2. Add the following code to the beginning of the header file:
3. Add a basic class named MyMathFuncs to perform common mathematical operations such as addition, subtraction,
multiplication, and division. The code should resemble this:
When the MATHFUNCSDLL_EXPORTS symbol is defined, the MATHFUNCSDLL_API symbol will set the
__declspec(dllexport) modifier in the member function declarations in this code. This modifier enables the function
to be exported by the DLL so that it can be used by other applications. When MATHFUNCSDLL_EXPORTS is
undefinedfor example, when the header file is included by an applicationMATHFUNCSDLL_API defines the
__declspec(dllimport) modifier in the member function declarations. This modifier optimizes the import of the
function in an application. By default, the New Project template for a DLL adds PROJECTNAME_EXPORTS to the
defined symbols for the DLL project. In this example, MATHFUNCSDLL_EXPORTS is defined when your
MathFuncsDll project is built. For more information, see dllexport, dllimport.
Note
If you are building the DLL project on the command line, use the /D compiler option to define the
MATHFUNCSDLL_EXPORTS symbol.
// MathFuncsDll.h
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
class MyMathFuncs
{
public:
// Returns a + b
static MATHFUNCSDLL_API double Add(double a, double b);
// Returns a - b
static MATHFUNCSDLL_API double Subtract(double a, double b);
// Returns a * b
static MATHFUNCSDLL_API double Multiply(double a, double b);
// Returns a / b
// Throws const std::invalid_argument& if b is 0
static MATHFUNCSDLL_API double Divide(double a, double b);
};
}
C++
C++
19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)
http://msdn.microsoft.com/en-us/library/ms235636.aspx 3/7
4. In the MathFuncsDll project in Solution Explorer, in the Source Files folder, open MathFuncsDll.cpp.
5. Implement the functionality for MyMathFuncs in the source file. The code should resemble this:
6. Compile the dynamic link library by choosing Build, Build Solution on the menu bar.
Note
If you are using an Express edition that does not display a Build menu, on the menu bar, choose Tools, Settings,
Expert Settings to enable it, and then choose Build, Build Solution.
Note
If you are building a project on the command line, use the /LD compiler option to specify that the output file is to
be a DLL. For more information, see /MD, /MT, /LD (Use Run-Time Library). Use the /EHsc compiler option to
enable C++ exception handling. For more information, see /EH (Exception Handling Model).
// MathFuncsDll.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
}
C++
19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)
http://msdn.microsoft.com/en-us/library/ms235636.aspx 4/7
To create an app that references the DLL
1. To create a C++ app that will reference and use the DLL that you just created, on the menu bar, choose File, New,
Project.
2. In the left pane, under Visual C++, select Win32.
3. In the center pane, select Win32 Console Application.
4. Specify a name for the projectfor example, MyExecRefsDllin the Name box. Next to Solution, select Add to
Solution from the drop-down list. This adds the new project to the same solution that contains the DLL. Choose the
OK button.
5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
6. On the Application Settings page, under Application type, select Console application.
7. On the Application Settings page, under Additional options, clear the Precompiled header check box.
8. Choose the Finish button to create the project.
To use the functionality from the class library in the app
1. After you create a console app, an empty program is created for you. The name for the source file is the same as the
name that you chose earlier. In this example, it is named MyExecRefsDll.cpp.
2. To use in the app the math routines that you created in the DLL, you must reference it. To do this, select the
MyExecRefsDll project in Solution Explorer, and then on the menu bar, choose Project, References. In the Property
Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the
Add New Reference button. For more information about the References dialog box, see Framework and References,
Common Properties, <Projectname> Property Pages Dialog Box.
3. The Add Reference dialog box lists the libraries that you can reference. The Project tab lists the projects in the
current solution and any libraries that they contain. On the Projects tab, select the check box next to MathFuncsDll,
and then choose the OK button.
4. To reference the header files of the DLL, you must modify the included directories path. To do this, in the Property
Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General.
Next to Additional Include Directories, specify the path of the location of the MathFuncsDll.h header file. You can
use a relative pathfor example, ..\MathFuncsDll\then choose the OK button.
5. You can now use the MyMathFuncs class in this application. Replace the contents of MyExecRefsDll.cpp with the
following code:
// MyExecRefsDll.cpp
// compile with: /EHsc /link MathFuncsDll.lib
#include <iostream>
#include "MathFuncsDll.h"
using namespace std;
int main()
{
C++
19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)
http://msdn.microsoft.com/en-us/library/ms235636.aspx 5/7
Community Additions
6. Build the executable by choosing Build, Build Solution on the menu bar.
To run the application
1. Make sure that MyExecRefsDll is selected as the default project. In Solution Explorer, select MyExecRefsDll, and
then on the menu bar, choose Project, Set As StartUp Project.
2. To run the project, on the menu bar, choose Debug, Start Without Debugging. The output should resemble this:
a + b = 106.4
a - b = -91.6
a * b = 732.6
a / b = 0.0747475
Caught exception: b cannot be zero!
See Also
Tasks
Visual C++ Guided Tour
Walkthrough: Deploying Your Program (C++)
Concepts
DLLs in Visual C++
Calling DLL Functions from Visual Basic Applications
Other Resources
Deploying Native Desktop Applications (Visual C++)
double a = 7.4;
int b = 99;
cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b) << endl;
cout << "a - b = " <<
MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
cout << "a * b = " <<
MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
cout << "a / b = " <<
MathFuncs::MyMathFuncs::Divide(a, b) << endl;
try
{
cout << "a / 0 = " <<
MathFuncs::MyMathFuncs::Divide(a, 0) << endl;
}
catch (const invalid_argument &e)
{
cout << "Caught exception: " << e.what() << endl;
}
return 0;
}
19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)
http://msdn.microsoft.com/en-us/library/ms235636.aspx 6/7
Problems in the referencing app too
OK it seems every step lacked enough accurate instructions and I got several errors while compiling the app that references the dll too.
This is the solution:
He tells you to add include paths for the header files but omits to say you have to add a path to library as well.
your cpp file should have the following added:
#include "MyMathFuncs.h"
#pragma comment(lib, "MathsFunc.lib") //If you omit this or it doesn't find the lib file then you will get unresolved symbol errors
Hope this helps and sorry for the multiple posts but I bounced into problems at every step.





Frederic VO
6/19/2014
Solution
OK I found a solution but maybe it's just a hack.
#include "stdafx.h"
#define MATHFUNCSDLL_EXPORTS
in the MyMathFunc.cpp file solved the problem. Note: the define SHOULD come after the stdafx.h include or it fails too. It should come
before the other include as it will be interpreted there.
Frederic VO
6/19/2014
19/7/2014 Walkthrough: Creating and Using a Dynamic Link Library (C++)
http://msdn.microsoft.com/en-us/library/ms235636.aspx 7/7
Wrong information
"By default,MATHFUNCSDLL_EXPORTS is defined when your MathFuncsDll project is built. "
This seems to NOT be the case.
Frederic VO
6/19/2014
Same problem
I have the exact same problem. It seems these code snippets aren't even tested before they are published.
Frederic VO
6/19/2014
On my system, I'm seeing dll linkage warnings
The compiler messages are of the form:
MathsFuncsDll.cpp
1>c:\users\davidc\documents\visual studio 2010\projects\dynamiclibrray\mathsfuncsdll\mathsfuncsdll.cpp(13): warning C4273:
'MathFuncs::MyMathFuncs::Add' : inconsistent dll linkage
1> c:\users\davidc\documents\visual studio 2010\projects\dynamiclibrray\mathsfuncsdll\mathfuncsdll.h(15) : see previous
definition of 'Add'
Line 13 in mathsfuncsdll.cpp is "double MyMathFuncs::Add(double a, double b)"
Line 15 in mathfuncsdll.h is "static MATHFUNCSDLL_API double Add(double a, double b);"
What's the correction for these, and more importantly, what's the root cause, so I can avoid it in production code!
daveTheConqueror
1/15/2014
2014 Microsoft

You might also like