问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Matlab调用C++生成的Dll动态链接库

创作时间:
作者:
@小白创作中心

Matlab调用C++生成的Dll动态链接库

引用
CSDN
1.
https://blog.csdn.net/qq_53018083/article/details/139838974

本文将详细介绍如何在Matlab中调用C++生成的Dll动态链接库。通过Visual Studio 2019创建C++项目并生成Dll,然后在Matlab 2019中加载并调用该Dll。文章适合有一定编程基础的读者学习参考。

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\u0064\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说明没有加载  

3.3运行结果:

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号