Matlab调用C++生成的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\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说明没有加载

3.3运行结果:

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值