-
Create a library project to build DLL file. You will get
.lib
,.dll
, and.h
files. -
Create another project to use the DLL file. Then follow steps 2 to 6 on this project. You need to tell Visual Studio where to find the header files and library files via its Properties window.
-
Go to project Properties > C/C++ > General and set Additional Include Directories to directory for header files (.h). (Or you can just copy the header files into your project.)
If you build the project, you will get error
LNK2019: unresolved external symbol
because there is no library file yet. -
Go to project Properties > Linker > General and set Additional Library Directories to directory for library files (.lib). (Or you can just copy the library files into your project)
Use
$(IntDir)
macro to replace "Debug" or "Release" string. Library files contain information about where the functions live in the DLL file. It's required for the linker, will be combined into.exe
file when built. -
Go to project Properties > Linker > Input and set Additional Dependencies to library file name e.g.
MathLibrary.lib
.Step 4 and 5 are used together. If you build the project, it will succeed now. But you still won't be able to run the application because the DLL file is still missing. You will see a dialog box saying
The code execution cannot proceed because xxxxx.dll was not found. Reinstalling the program may fix this problem.
-
Go to project Properties > Build Events > Post-Build Event and set Command Line to
xcopy /y /d "YourDLLProject\$(IntDir)YourDLLFile.dll" "$(OutDir)"
to automatically copy the DLL file from the DLL project to your current project's output directory after every build.
The DLL file does not need any extra path configuration. It just needs to be present close to the
.exe
file when it's executing. You can also manually copy the DLL file to the output directory and it will work the same like this step.
Read more from this tutorial: https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2017