[C++] C++ is encapsulated into DLL and called (quick start for beginners)

Without further ado, let’s get started! ! ! ! ! ! ! **** (Intrusion and deletion)
Using vs2019 to encapsulate C++ into a DLL and call it mainly involves the following steps:
1) Create a new project and write the .cpp and .h files to be encapsulated;
2) Generate the dynamic link library .dll and static Link library .lib;
3) Call through .h file;
Step 1: Write .cpp and .h files.
The project name of this example is RunLIB.cpp
as follows:

#include "pch.h"
#include<iostream>
class FHello :public IInterface
{
    
    
public:
	FHello();
	virtual void Init();
	virtual void Destroy();
	virtual char* GetName();

private:
	char name[1024];

};

FHello::FHello()
{
    
    
	memset(name, 0, 1024);
	strcpy(name, "hello");
}

void FHello::Init()
{
    
    
	printf("FHello::Init\n");
}

void FHello::Destroy()
{
    
    
	printf("FHello::Destroy\n");
}

char* FHello::GetName()
{
    
    
	return name;
}

IInterface* IInterface::CreateInterface()
{
    
    
	return new FHello();
}

.h is as follows:

#ifndef PCH_H
#define PCH_H
#define _CRT_SECURE_NO_WARNINGS

#define FENGZHUANGCPP_API __declspec(dllexport) //导出

//动态链接库
class FENGZHUANGCPP_API IInterface
{
    
    
public:
	static IInterface* CreateInterface();
	virtual void Init() = 0;
	virtual void Destroy() = 0;
	virtual char *GetName() = 0;
};

#endif //PCH_H

Step 2: Generate dynamic link library .dll and static link library .lib
① Right-click the project name in the solution explorer>>Properties>>Property Configuration>>General>>Configuration type, and select the configuration type as dynamic library (. dll), click OK
Insert image description here
② Right-click the project name of the Solution Explorer, select Generate
③ Right-click the project name of the Solution Explorer>>Properties>>Property Configuration>>General>>Configuration type, select the configuration type as static library (.lib), click OK
Insert image description here
④ Right-click the project name in the Solution Explorer and select Generate.
At this time, you will see the .dll and .lib files you generated in the x64\Debug folder under the project path (such as my (In the path: E:\C++\RunLIB\x64\Debug)
Insert image description here
At this point, we have completed the encapsulation process, so how should we call our interface?
Step 3: Call through .h file;
①Put the generated .dll and .lib, and pch.cpp in a folder named dllib (for later calling)
②Use vs to create a new project named diaoyong (A diaoyong.cpp will be automatically generated at the same time), copy the folder ① to the project directory
Insert image description here
③ Enter the calling code in diaoyong.cpp

#include "dlllib/pch.h"
#include <iostream>
using namespace std;
#pragma comment(lib,"dLLL.lib")
//动态库在运行时,会把代码链接到目标
//静态库在编译期,会把代码链接到目标

int main()
{
    
    
    IInterface* IF = IInterface::CreateInterface();
    cout << IF->GetName() << endl;
    IF->Init();
    IF->Destroy();
    return 0;
}

At this time, the following error will appear when running, because we have not loaded the folder path containing .lib . What we need to perform:
1) Right-click the project name of the solution explorer>>Properties>>Configuration Properties>> C/C++>>Additional Include Directory>>Edit>>Put the path to the .lib file
2) Right-click the project name in the Solution Explorer>>Properties>>Project Properties>>Configuration Properties>>Linker>>Additional Libraries Directory>>Edit>>Put the path of the .lib file in.
Insert image description here
Insert image description here
Insert image description here
After the loading is completed, run the diaoyong.cpp we wrote and it will be successful! ! ! !
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44244190/article/details/123504926