点击此处查看我的另一篇博客:采用def文件创建动态链接库
_declspec(dllexport) int /*_stdcall*/ add(int a, int b)
{
return a + b;
}
_declspec(dllexport) int /*_stdcall*/ subtract(int a, int b)
{
return a - b;
}
#include <iostream>
using namespace std;
#pragma comment(lib,"Dll.lib")
//extern 与 _declspec(dllimport)效果一样,最好用_declspec(dllimport)
//extern add(int a, int b);
//extern subtract(int a, int b);
_declspec(dllimport) add(int a, int b);
_declspec(dllimport) subtract(int a, int b);
#define OK 0
#define ERR -1
int main()
{
int a = 100, b = 40;
cout << "add(" << a << "," << b << ")" <<add(a,b)<< endl;
cout << "subtract(" << a << "," << b << ")" << subtract(a, b) << endl;
system("pause");
return OK;
}
_declspec(dllexport) int /*_stdcall*/ add(int a, int b)
{
return a + b;
}
_declspec(dllexport) int /*_stdcall*/ subtract(int a, int b)
{
return a - b;
}
(3)添加h文件 命名Dll.h
此时只是给客户端使用的,即TestDll使用的,所以用_declspec(dllimport),表明是导入
_declspec(dllimport) int /*_stdcall*/ add(int a, int b);
_declspec(dllimport) int /*_stdcall*/ subtract(int a, int b);
#include <iostream>
#include "*/Dll.h"//加上之后才可以
using namespace std;
#pragma comment(lib,"Dll.lib")
//extern 与 _declspec(dllimport)效果一样,最好用_declspec(dllimport)
//extern add(int a, int b);
//extern subtract(int a, int b);
//_declspec(dllimport) add(int a, int b);
//_declspec(dllimport) subtract(int a, int b);
#define OK 0
#define ERR -1
int main()
{
int a = 100, b = 40;
cout << "add(" << a << "," << b << ")" <<add(a,b)<< endl;
cout << "subtract(" << a << "," << b << ")" << subtract(a, b) << endl;
system("pause");
return OK;
}
#define DLL_API _declspec(dllexport)
#include "Dll1.h"
int /*_stdcall*/ add(int a, int b)
{
return a + b;
}
int /*_stdcall*/ subtract(int a, int b)
{
return a - b;
}
(3)添加h文件 命名Dll.h
#ifdef DLL_API
#else
#define DLL_API _declspec(dllimport)
#endif
DLL_API int /*_stdcall*/ add(int a, int b);
DLL_API int /*_stdcall*/ subtract(int a, int b);
#include <iostream>
#include "*/Dll.h"//加上之后才可以
using namespace std;
#pragma comment(lib,"Dll.lib")
//extern 与 _declspec(dllimport)效果一样,最好用_declspec(dllimport)
//extern add(int a, int b);
//extern subtract(int a, int b);
//_declspec(dllimport) add(int a, int b);
//_declspec(dllimport) subtract(int a, int b);
#define OK 0
#define ERR -1
int main()
{
int a = 100, b = 40;
cout << "add(" << a << "," << b << ")" <<add(a,b)<< endl;
cout << "subtract(" << a << "," << b << ")" << subtract(a, b) << endl;
system("pause");
return OK;
}
#define DLL_API extern "C" _declspec(dllexport)//注意C要大写
#include "Dll1.h"
int /*_stdcall*/ add(int a, int b)
{
return a + b;
}
int /*_stdcall*/ subtract(int a, int b)
{
return a - b;
}
(3)添加h文件 命名Dll.h
#ifdef DLL_API
#else
#define DLL_API extern "C" _declspec(dllimport)
#endif
DLL_API int /*_stdcall*/ add(int a, int b);
DLL_API int /*_stdcall*/ subtract(int a, int b);
#include <iostream>
#include "*/Dll.h"//加上之后才可以
using namespace std;
#pragma comment(lib,"Dll.lib")
//extern 与 _declspec(dllimport)效果一样,最好用_declspec(dllimport)
//extern add(int a, int b);
//extern subtract(int a, int b);
//_declspec(dllimport) add(int a, int b);
//_declspec(dllimport) subtract(int a, int b);
#define OK 0
#define ERR -1
int main()
{
int a = 100, b = 40;
cout << "add(" << a << "," << b << ")" <<add(a,b)<< endl;
cout << "subtract(" << a << "," << b << ")" << subtract(a, b) << endl;
system("pause");
return OK;
}
#define DLL_API _declspec(dllexport)
#include "Dll1.h"
DLL_APPI int /*_stdcall*/ add(int a, int b)
{
return a + b;
}
DLL_API int /*_stdcall*/ subtract(int a, int b)
{
return a - b;
}
void Line:: output()
{
cout<<"void Line:: output()"<<endl;
}
void Line::test()
{
cout<<"void Line::test()"<<endl;
}
(3)添加h文件 命名Dll.h
#ifdef DLL_API
#else
#define DLL_API _declspec(dllimport)
#endif
int /*_stdcall*/ add(int a, int b);
int /*_stdcall*/ subtract(int a, int b);
class DLL_API Line
{
public:
void output();
void test();
}
#include <iostream>
#include "*/Dll.h"//加上之后才可以
using namespace std;
#pragma comment(lib,"Dll.lib")
//extern 与 _declspec(dllimport)效果一样,最好用_declspec(dllimport)
//extern add(int a, int b);
//extern subtract(int a, int b);
//_declspec(dllimport) add(int a, int b);
//_declspec(dllimport) subtract(int a, int b);
#define OK 0
#define ERR -1
int main()
{
int a = 100, b = 40;
cout << "add(" << a << "," << b << ")" <<add(a,b)<< endl;
cout << "subtract(" << a << "," << b << ")" << subtract(a, b) << endl;
Line line;
line.output();
line.test();
system("pause");
return OK;
}