动态库的创建
1.通过 .h 创建。
2.通过.def 创建。
创建 .h 动态库
.h 中去声明 接口。.cpp 去实现具体方法。
项目属性 =》常规=》配置类型 =》动态库(.dll)
创建.def 动态库
参见 https://www.jianshu.com/p/7ff0184d7708
主项目调用 .h的动态库
引用步骤
1.项目属性 =》C/C+ =》常规 =》附加包含目录
把 h类型的动态库的 .h 路径 写入。
2.项目属性 =》C/C+ =》 链接器 =》常规 =》 附加库目录
把 h类型的动态库的 .lib 路径 写入。
3.项目属性 =》C/C+ =》 链接器 =》输入 =》附加依赖项
写入h类的 lib 文件名
或在项目中写入
#pragma comment(lib,"dllTest.lib")
代码
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "dllTest.h"
#pragma comment(lib,"dllTest.lib")
int _tmain(int argc, _TCHAR* argv[])
{
int a = 5, b = 3;
cout << a << " + " << b << " = " << add(a, b) << endl;
cout << " ========================= " << endl;
return 0;
}
主项目调用 .def的动态库
引用步骤
不用做 头 include,lib 库目录,附加项依赖。
代码
#include "stdafx.h"
#include "tchar.h"
#include <iostream>
#include <Windows.h>
using namespace std;
typedef double(WINAPI *AddFunc)(double a, double b);
int main()
{
//add(1, 2);
HMODULE hdll = LoadLibrary(_T("..\\debug\\MFCLibrary1.dll"));
if (hdll != NULL)
{
AddFunc ADD1= (AddFunc)GetProcAddress(hdll, (char*)(1));
if (ADD1 != NULL)
{
cout << "显示调用" << endl;
cout << ADD1(3, 4) << endl;
}
system("pause");
}
return 0;
}
网友评论