- MATLAB版本 2017a
- VS版本 VS2015
参考如下步骤:
1、新建一个c++ DLL工程MatlabDllTest
2、头文件
#pragma once
#ifndef MATLABDLLTEST_H
#define MATLABDLLTEST_H
#ifdef __cplusplus
extern "C"
{
#endif
__declspec(dllexport) double add(double x, double y);
#ifdef __cplusplus
}
#endif
#endif
3、cpp文件
// MatlabDllTest.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "MatlabDllTest.h"
double add(double x, double y)
{
return (x + y);
}
选择x64平台生成MatlabDllTest.dll
4、MATLAB中加载DLL
loadlibrary('MatlabDllTest','MatlabDllTest.h');
5、MAT中调用函数
calllib('MatlabDllTest', 'add', 1.3, 4.6)
note:
如果加载的时候报错:
需要在VS中修改项目属性:
修改c/c++->代码生成->运行库设置为:多线程调试(/MTd)
参考链接:
https://www.jianshu.com/p/e910ca82b542
https://blog.youkuaiyun.com/so_geili/article/details/53009680