C++制作和调用动态库
创建DLL
1.创建对应的DLL头文件
//防止库被重复调用
#ifndef DLL_DEFINE_H
//第一次引入头文件 创建默认宏
#define DLL_DEFINE_H
//宏判断
#ifdef DLL_EXPORTS
//导出
#define DLLAPI _declspec(dllexport)
#else
//导入
#define DLLAPI _declspec(dllimport)
#endif
#include "iostream"
using namespace std;
class DLLAPI DLL{
public:
//默认构造函数
DLL();
//默认析构函数
~DLL();
//默认测试函数
static void Test();
};
#endif
2.创建动态库是 项目会默认添加对应的XXX_ DLL_EXPORTS预编译头
3.编译之后默认工作目录(
(
S
o
l
u
t
i
o
n
D
i
r
)
(SolutionDir)
(SolutionDir)(Platform)$(Configuration)\)生成了*.lib *.exp *.dll *.pdb文件
调用DLL动态库
1.添加头文件路径
2.添加lib库路径
3.添加对应lib
4.DLL放置在EXE同级目录
最好按这种格式按这种格式放置在项目同级目录下
5.调用代码DLL代码
// TestDLL.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "iostream"
//添加动态库头文件
#include "DLL.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("================ begin\n");
getchar();
//调用DLL动态库
DLL::Test();
printf("\n");
printf("================ end\n");
system("pause");
return 0;
}