一、前言
在工程中,经常会根据不同的场景需求将类封装成库文件,以供他人使用;那么如何利用Qt进行“动态库”的生成及调用呢,以下简要演示实现过程。
开发环境:Qt Creator 4.11.1 + MSVC 2017 (64-bit)
二、生成DLL动态库
新建库函数类。
添加
#include "CreatDll_global.h" 和 CREATDLL_EXPORT
dll1函数声明;
#ifndef DLL1_H
#define DLL1_H
#include "CreatDll_global.h"
class CREATDLL_EXPORT dll1
{
public:
dll1();
public:
int myAdd(int a, int b);
int myMax(int a, int b);
};
#endif // DLL1_H
dll1函数实现;
#include "dll1.h"
dll1::dll1()
{
}
int dll1::myAdd(int a, int b)
{
return a + b;
}
int dll1::myMax(int a, int b)
{
return a > b ? a : b;
}
构建,产生dll 、 lib文件;
三、动态库调用。
复制lib文件并改名,复制dll1.h