How to create and use a Dynamic Link Library in C++ (Visual Studio)
Create DLL Project
open visual studio & click create a new project
click Next
give project name, solution name and location
select dynamic link library (DLL) from the application type drop down
Right click on the project and create c++ header file called ForexLibrary.h
add the following code
#pragma once
#ifdef FOREXLIBRARY_EXPORTS
#define FOREXLIBRARY_API __declspec(dllexport)
#else
#define FOREXLIBRARY_API __declspec(dllimport)
#endif
#include <string.h>
using namespace std;
FOREXLIBRARY_API int __stdcall add(int a, int b);
Right click on the project and create c++ header file called ForexLibrary.cpp
add the following code
#include "ForexLibrary.h"
int __stdcall add(int a, int b)
{
return a + b;
}
Right click on the project and build the project
ForexLibrary.lib & ForexLibrary.dll will be generated in the debug folder.
Create Client Project
open visual studio & click create a new project
Select Console App
click next
give project name, solution name and location
click create
select x64 for solution platform
If you are working with the DLL source code and client source code at the same time, it's better if these two projects can be synced.
To do so set the additional includes directories path to the original header
Right click on the ForexLibrary node and open properties
Select All Configurations from the Configuration drop down box
Select Configuration properties -> C/C++ -> General
Edit additional include directories
Select the path to the header file in your DLL project
#include <iostream>
#include "ForexLibrary.h"
int main()
{
add(5,5);
}
Add the DLL import library to your project
Right click on ForexClient node & select the properties
Select All Configurations from the Configurations drop down
Edit Configuration properties -> Linker -> Input , Additonal Dependencies
Set the original path to ForexLibrary.lib which is from the DLL project
Edit Configuration properties -> Linker -> General , Additonal Library Directories
Comments
Post a Comment