参考https://blog.csdn.net/whu_zs/article/details/80344822
主要是将详细化上面的过程。
1. 新建动态链接库(DLL)
建好后,当前目录为
2. 新建头文件dll.h
在dll.h文件中写入以下定义
#pragma once //避免重复编译
/*
生成dll的工程时,vs默认定义宏:DLL_EXPORTS,不是dll工程,没有该宏定义
可以在"属性->预处理器->预处理器定义"里面看见该宏
以下语句的含义:如果定义该宏,则DLL_API是导出.(dll生成工程定义了该宏,所以是导出)
如果未定义该宏,则DLL_API是导入.(调用dll的工程没有定义该宏,所以是导入dll)
*/
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
#include <string>
namespace zsdll //定义命名空间,可要可不要,这里只是为了说明命名空间的用法
{
/*图像二值化,调用opencv库*/
//extern "C" 作用是编译时用C的编译方式,保证函数名不变
//C++存在函数重载,所以编译函数时会在函数名后面加上参数类型,如果C++调用C编写的dll库就需要extern "C"
extern "C" DLL_API void ThresholdImg(std::string SrcPath, std::string ResPath, double thresh);
/*插入排序*/
extern "C" DLL_API void InsertSort(int *data_array, int size_array);
};
#pragma once
3.编辑dll2文件
写入下面程序
// Dll1.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include <iostream>
#include <opencv2\opencv.hpp>
#include "dll.h"
//函数1 图像
void zsdll::ThresholdImg(std::string SrcPath, std::string ResPath, double thresh)
{
cv::Mat srcImg = cv::imread(SrcPath, 0);
cv::Mat resImg;
srcImg.copyTo(resImg); //深复制
cv::threshold(srcImg, resImg, thresh, 255, CV_THRESH_BINARY);
cv::imwrite(ResPath, resImg);
}
//函数2 排序
void zsdll::InsertSort(int *data_array, int size_array)
{
int *res_array = new int[size_array + 1]{ 0 };
for (int i = 1; i < size_array + 1; i++)
{
res_array[i] = data_array[i - 1];
}
//插入排序
int location{ 0 };
for (int i = 2; i < size_array + 1; i++)
{
res_array[0] = res_array[i];
for (int j = i - 1; j >= 0; j--) //遍历前面有序数据
{
location = j;
if (res_array[j] <= res_array[0])
break;
res_array[j + 1] = res_array[j];
}
res_array[location + 1] = res_array[0];
}
std::cout << "排序结果: ";
for (int i = 1; i < size_array + 1; i++)
{
std::cout << res_array[i] << "\t";
}
std::cout << std::endl;
delete[] res_array;
}
2.3. 生成解决方案: 生成->生成解决方案,
在工程目录下找到以下文件(Dll1与Dll2文件一样的含义)
自此dll文件就生成完毕了。
##################################################################################
导入dll文件
新建空项目
2.将上面的三个文件考入当前工程目录下
在属性页面配置如下
链接器
在loadDLL.cpp文件中写入以下程序
#include <iostream>
#include <string>
#include "dll.h" //导入头文件
void main()
{
/*图像二值化*/
std::string InputPath = "src.jpg";
std::string OutPath = "res.jpg";
zsdll::ThresholdImg(InputPath, OutPath, 100);//引入函数1
std::cout << "图像二值化完成" << std::endl;
/*插入排序*/
int *data = new int[10]{ 5,3,2,7,8,1,0,4,6,9 };
std::cout << "原始数据:";
for (int i = 0; i < 10; i++)
{
std::cout << data[i] << "\t";
}
std::cout << std::endl;
zsdll::InsertSort(data, 10);//引入函数2
system("PAUSE ");
delete[] data;
}
结果展示