1.软件版本
- Visual Studio 2019
- Matlab 2019
2.C++生成Dll
1.创建一个空项目
2.新建一个SimDll.h和SimDll.cpp文件
//SimDll.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
__declspec(dllexport)int add(int a, int b);
__declspec(dllexport)double multiply(double a, double b);
#ifdef __cplusplus
}
#endif // __cplusplus
// SimDll.cpp
#include<iostream>
#include"SimDll.h"
int add(int a, int b)
{
return a + b;
}
double multiply(double a, double b)
{
return a * b;
}
3.配置工程属性
1.选择项目-SimDll属性
2.配置选Release,平台选x64,选择常规-配置类型选动态库(.dll)
3.点击确定后,在主界面选择Release,x64
4.生成-重新生成解决方案。
5.若报错无法打开文件“opencv_world452d.lib” 。
在项目-属性-链接器-输入-附加依赖项,不勾选从父级或项目默认设置继承。
6.重新执行第4步,重新生成解决方案,可看到成功生成。
3.Matlab调用C++生成的Dll
3.1环境配置
使用Matlab调用动态链接库dll_matlab调用dll-CSDN博客
3.2matlab程序
1.第二节生成的动态库目录:...\SimDll\x64\Release
2.将SimDll.dll和SimDll.lib与SimDll.h和DllTest.m放在同一路径
//SimDll.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
__declspec(dllimport)int add(int a, int b);
__declspec(dllimport)double multiply(double a, double b);
#ifdef __cplusplus
}
#endif // __cplusplus
%DllTest.m
%加载库
loadlibrary('SimDll.dll','SimDll.h');
%判断是否加载成功
libisloaded('SimDll');
%查看库里函数
libfunctions('SimDll');
%调用函数
calllib('SimDll', 'multiply', 5.20, 13.14)
%卸载库
unloadlibrary 'SimDll'
%判断库是否加载
libisloaded('SimDll')
%ans=0说明没有加载