C++与matlab混编(VS2013+Matlab 2016a x64+win7 x64) 成功步骤

本文介绍如何使用Matlab R2016a与VS2013进行混合编程,通过创建并编译一个简单的加法函数,将其封装为DLL文件,并在VS环境中调用。步骤包括在Matlab中编写函数、导出为DLL、配置VS工程及环境变量等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载+原创
本博客
参考原博客:Matlab 2016a和VS2013混合Dll编程步骤

平台:
VS2013
MATLAB R2016a (64-bit)
win7 x64

##1. 与原博客的不同之处:
只需做一些在main.cpp中做一点小修改即可。

// main.cpp : 定义控制台应用程序的入口点。  
#include "stdafx.h"
#include <iostream>  
#include "mclmcr.h"  
#include "matrix.h"  
#include "mclcppclass.h"  
#include "Detector.h"  
using namespace std;
#pragma comment(lib,"Detector.lib")  

int _tmain(int argc, _TCHAR* argv[])
{

	if (!DetectorInitialize())   
	 //初始化  //注意这个初始化中 !DetectorInitialize() 字段中的 Detector与matlab编译成的.lib同名
	{
		cout << "Could not initialize addfunction!";
		exit(0);
	}
	
	mwArray result(1, 1, mxDOUBLE_CLASS, mxREAL);
	mwArray a(1, 1, mxDOUBLE_CLASS, mxREAL);
	mwArray b(1, 1, mxDOUBLE_CLASS, mxREAL);
	double data1; double data2;

	cout << "\n*********************************************\n";
	cout << "***** 该程序功能:实现任意两个实数的和。*****\n";
	cout << "*********************************************\n\n";
	cout << "请输入任意两个实数a,b:";
	cin >> data1 >> data2;
	a.SetData(&data1, 1);
	b.SetData(&data2, 1);
	result.SetData(&data2, 1);

	Detector(1, result, a, b);  //调用自己用matlab写的函数 
	//.m文件中为:function y=Detector(x,z);
	//.h文件中为:extern LIB_Detector_CPP_API void MW_CALL_CONV Detector(int nargout, mwArray& y, const mwArray& x, const mwArray& z);	

	double x = a.Get(1, 1);
	double y = b.Get(1, 1);
	double z = result.Get(1, 1);
	cout << "a+b= " << z << endl << endl;

	DetectorTerminate(); 
	 //注意结束中 !DetectorInitialize() 字段中的 Detector也与matlab编译成的.lib同名
	return 0;
}

在这里提供一下 我的步骤图(有一样的可以参考一下):
##2.我的步骤
###2.1在matlab中建立一个Detector.m文件,实现加法功能

%实现加法功能
function y=Detector(x,z)
y=x+z;

###2.2. 从Matlab导出.m文件为Dll文件
Step1:将Matlab工作路径转为目标.m文件所在文件夹,在控制栏输入“mex –setup”,弹出如下说明:

Step2:点击“mex –setup C++”,弹出如下说明,看到“MEX 配置为使用 ‘Microsoft Visual C++ 2013 Professional’ 以进行 C++ 语言编译。”,说明配置好用C++语言进行编译;

Step3:继续输入“deploytool”,弹出compile窗口,选择第三个,即Library Compiler;

Step4:三步配置Library Compiler
(1)在弹出窗口中,左上角选择“C++ Shared Library”,
(2)右侧添加需要转换的.m文件,下面第一行输入转换后的Dll文件名,
(3)最后点击右上角“Package”对号;然后,生成二进制文件

Step5:生成后文件夹如下图所示,第一个文件夹中的exe文件用于程序发布时用,第二个文件夹中包含相当于release下得.h,.lib和 .dll文件,第三个文件包含测试用地.h,.lib和 .dll文件等文件,是测试需要的;(第二个文件和第三个文件中的.h,.lib和 .dll文件完全相同。)

Step6:新建一个Win32控制台应用程序工程,命名为Test;Source Files中添加 main.cpp文件,将本博客刚开始写的 main.cpp代码写入其中。保存。

然后将for_redistribution_files_only(Step5)文件中的 .h,.lib和 .dll文件拷到新建应用程序下和.cpp文件放在一起,如下图

###2.3. 配置电脑系统环境变量和VS工程属性;
Step0:在系统环境变量的Path中添加“Matlab安装目录\bin\win64;”
(这里对原博客进行了补充)
计算机->属性->高级系统设置->环境变量->系统变量->Path
添加“;Matlab安装目录\bin\win64”(Matlab安装目录 指你自己的matlab安装目录)



Step1:在项目属性中的包含目录添加“Matlab安装目录\extern\include;”
Step2:在项目属性中的库目录添加“Matlab安装目录\extern\include\win64\microsoft;”(与原博客不同,没有bin文件)

Step3:在项目属性链接器输入中添加以下库名:
libmat.lib
libmex.lib
mclmcr.lib
mclmcrrt.lib
libemlrt.lib
libeng.lib
libfixedpoint.lib
libcovrt.lib

将库名粘贴到这里:然后点击OK.

Step5:为项目添加matlab生成的Detector.h头文件( 2.2.中Step6中的.h文件 第二张图片 )。
Test Project配置好的的Solution Explorer 的目录如下图所示:

至此,配置结束。
点击,Ctrl+F5 ->Yes即可出现结果。_
这里写图片描述

在这里提供 stdafx.h 文件的代码,有需要的可以参考。

// stdafx.h : include file for standard system include files,
//      or project specific include files that are used frequently,
//      but are changed infrequently

#if !defined(AFX_STDAFX_H__BEFA6204_943D_4000_A02D_1B4055713E42__INCLUDED_)
#define AFX_STDAFX_H__BEFA6204_943D_4000_A02D_1B4055713E42__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define STRICT

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501 
//_WIN32_WINNT 0x0501 的说明
//注意:从 Visual C++ 2008 开始,Visual C++ 不支持面向 Windows 95、Windows 98、Windows ME 或 Windows NT。
//有效值包括 0x0501(用于 Windows XP)、0x0502(用于 Windows Server 2003)和 0x0600(用于 Windows Vista)
//之前的失效版本: WIN32_WINNT 0x0400 (用于Windows NT 4.0)
#endif

#define _ATL_APARTMENT_THREADED

#include <afxwin.h>
#include <afxdisp.h>

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__BEFA6204_943D_4000_A02D_1B4055713E42__INCLUDED)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值