C++ dll 导出
可以跨编译器的方法
声明一个抽象类(都是纯虚函数)
// "010.h" #pragma once #ifdef _DLL_EXPORTS #define DLL_API _declspec(dllexport) #else #define DLL_API _declspec(dllimport) #endif class IAnimal { public: virtual void eat() = 0; virtual void sleep() = 0; virtual void delObj() = 0; }; extern "C" DLL_API IAnimal *GetCat();
实现所有的纯虚函数
// 010.cpp : 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include <iostream> using namespace std; #include "010.h" //1、创建导出类 class Cat:public IAnimal { public: Cat(int age) { this->age=age; } virtual ~Cat() { cout<<"Cat destroy!"<<endl<<endl; } virtual void eat() { cout<<"Cat eat fish!"<<endl<<endl; } virtual void sleep() { cout<<"Cat sleep!"<<endl<<endl; } virtual void releaseObj() { delete this; } private: int age; }; //2、创建一个导出函数 extern "C" _declspec(dllexport) IAnimal* GetCat() { return new Cat(6); }
使用
#include <iostream> using namespace std; #include "010.h" int main() { IAnimal* pCat=GetCat(); pCat->eat(); pCat->sleep(); pCat->releaseObj(); return 0; }
C++ dll 导出
https://www.138729.xyz/index.php/archives/3/