>> mex -setup
Please choose your compiler for building external interface (MEX)
files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:\PROGRA~1\MATLAB\R2008a\sys\lcc\bin
[2] Microsoft Visual C++ 6.0 in D:\Program Files\Microsoft Visual
Studio
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C++ 6.0
Location: D:\Program Files\Microsoft Visual Studio
Are these correct [y]/n? y
Trying to update options file: C:\Documents and
Settings\hk\Application
Data\MathWorks\MATLAB\R2008a\mexopts.bat
From
template: D:\PROGRA~1\MATLAB\R2008a\bin\win32\mexopts\msvc60opts.bat
Done . . .
3、安装MATLAB Component Runtime环境
运行MATLAB安装目录下的···\toolbox\compiler\deploy\win32\MCRInstaller.exe,开始自动安装组件运行环境。
3、使用MATLAB编译器和VC6.0实现混合编程(通过例子说明)
(1)、MATLAB中的操作
创建mccdemol.m文件,内容如下:
在MATLAB命令行下,将mccdemol.m编译成C++动态链接库文件。可以执行如下命令:(带图形的和不带图形的有区别,这里主要是带图形的编译)
>> mcc -W cpplib:libmccdemol -T link:lib
mccdemol.m
命令执行完成后,将在工作目录产生使用MATLAB编译生成库文件所需的相关文件。
(2)、建立VC对话框工程
创建一个基于MFC对话框的工程,工程名称为MccDemol,如下图所示:
设计对话框界面如图:
在文件MccDemolDlg.cpp
: implementation file中包含头文件,如下所示:
#include "libmccdemol.h"
修改对话框的初始函数如下:
BOOL CMccDemolDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//
Add "About..." menu item to system menu.
//
IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX
& 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX
< 0xF000);
CMenu*
pSysMenu = GetSystemMenu(FALSE);
if
(pSysMenu != NULL)
{
CString
strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if
(!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX, strAboutMenu);
}
}
//
Set the icon for this dialog.The
framework does this automatically
//when
the application's main window is not a dialog
SetIcon(m_hIcon,
TRUE);//
Set big icon
SetIcon(m_hIcon,
FALSE);//
Set small icon
//
TODO: Add extra initialization here
bool
res = false;
res
= mclInitializeApplication(NULL,0);
if
(!res)
{
MessageBox("初始化Application错误!");
}
res
= libmccdemolInitialize();
if
(!res)
{
MessageBox("初始化Lib错误!");
}
return
TRUE;//
return TRUEunless
you set the focus to a control
}
为对话框添加WM_DESTROY消息,使用快捷键“Ctrl+W”,调出MFC
Class Wizard对画框,如下图所示:
添加如下消息响应函数:
void CMccDemolDlg::OnDestroy()
{
CDialog::OnDestroy();
//
TODO: Add your message handler code here
bool
res;
res
= mclTerminateApplication();
if
(!res)
{
MessageBox("结束程序错误!");
}
libmccdemolTerminate();
}
为确定按钮添加消息响应函数
void CMccDemolDlg::OnButtonRun()
{
//
TODO: Add your control notification handler code here
mccdemol();
mclWaitForFiguresToDie(NULL);
}
本例中用到了MATLAB图形功能,所示使函数“mclWaitForFiguresToDie(NULL)”,在没有用到MATLAB的图形功能的库文件时,不需要此函数。
(3)VC中的设置
将在MATLAB环境下编译后的生成文“libmccdemol.ctf”、“libmccdemol.h”、“libmccdemol.dll”和“libmccdemol.lib”,复制到当前工程目录。
为工程添加库文件“libmccdemol.lib”和“mclmcrrt.lib”,如下图所示。
编译运行工程,可以看到运行结果,单击确定按钮,弹出Figure窗口。这与MATLAB环境下直接运行的结果相同。