* 新建动态库
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) int add(const char* mystr);
#ifdef __cplusplus
}
#endif
#endif //PCH_H
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
#include <iostream>
using namespace std;
int add(const char* mystr)
{
cout << mystr << endl;
return 0;
}
* 只有动态库DLL时
#include <iostream>
#include<windows.h>
int main()
{
HINSTANCE hDllInst;
hDllInst = LoadLibrary(L"20199321.dll"); //调用DLL
typedef int(*PLUSFUNC)(const char* str); //后边为参数,前面为返回值
PLUSFUNC plus_str = (PLUSFUNC)GetProcAddress(hDllInst, "add"); //GetProcAddress为获取该函数的地址
std::cout << plus_str("abcd");
}
* 使用LIB加DLL加H文件隐式链接时
- 添加头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上h文件存放目录
- 添加库文件目录:工程---属性---配置属性---链接器---常规---附加库目录:加上lib文件存放目录
- 添加引用lib文件:工程---属性---配置属性---链接器---输入---附加依赖项:加上lib文件名
- 添加引用dll文件: 把dll文件放到编译好的exe程序同目录即可