首先建立动态库工程
新建.cpp 、.h文件
在cpp中:
#define DLL1_API __declspec(dllexport) //在动态库中使用,也即不用.h文件
#include"DLL1.h"
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
在.h中:
#ifdef DLL1_API //判断DLL1_API是否定义,定义了什么都不做
#else //否则定义为
#define DLL1_API _declspec(dllimport)
#endif
//加上上面部分使函数既可以在DLL中使用
//也可以在外部被调用
DLL1_API int add(int a, int b);
DLL1_API int subtract(int a, int b);
在动态库中定义类:
1、DLL1_API放在类的定义处时,类中public属性的值在类的外部都能使用
class DLL1_API Point
{
public:
void output(int x, int y);
};
2、DLL1_API放在类的public属性的某一个函数前面,该函数在外面可以用,其他的不能用