C++ Dll中导出一个类
//定义一个头文件,创建MyObject.h的头文件 并打印如下代码 #ifndef _MY_OBJECT_H #define _MY_OBJECT_H #ifndef MYDLL_EXPORTS #define MYDLL _declspec(dllexport) //导出函数 #else #define MYDLL _declspec(dllimport) //导入函数 #endif class MYDLL Object { public: Object(int a); void test(); int jiafa(); public: int a; }; #endif _MY_OBJECT_H //函数cpp实现 #include <stdio.h> #include <iostream> #define MYDLLL_EXPORTS //宏定义 #include "myObject.h" Object::Object(int a) { this->a = a; } void Object::test() { std::cout << "hello,world!"; } int Object::jiafa() { return a * 14; } //例子 //创建一个控制台程序。代码如下 #include <iostream> using namespace std; #include "myObject.h" #pragma comment(lib,"my.lib") //加载lib文件 int main() { Object a(1); int ss=a.jiafa(); cout<<ss<<endl; a.test(); return 0; }
运行的结果为: 14 hello,world!