1、啟動MATLAB7,建立m檔案,輸入:
function myfun(n)
t=0:n;
y=sin(t);
plot(y);
儲存檔案為:myfun.m
2、在Matlab 的Command Window 下輸入命令:mcc -B csglsharedlib:mylib myfun 產生動態連結程式庫DLL。Matlab 會產生一系列檔案, 其中mylib.h mylib.lib mylib.dll mylib.ctf是我們這裡需要的。
3、 在VC++中使用Matlab 產生的動態連結程式庫( *.DLL)
以上面建立的TestDllApp工程為例。將mylib.h mylib.lib mylib.dll mylib.ctf檔案拷貝至TestDllApp工程目錄下並添加入工程。修改TestDllApp.cpp檔案:
// TestDllApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include "mylib.h"
#include "mclmcr.h"
int opp(int n);
void main(int argc, char* argv[])
{
mylibInitialize();
double x=64;
mxArray *t;
t=mxCreateDoubleMatrix(1,1,mxREAL);
memcpy(mxGetPr(t),&x,sizeof(double));
mlfMyfun(t);
mxDestroyArray(t);
mylibTerminate();
printf("This is a test!\n");
printf("%d\n",opp(16));
}
int opp(int n){
return n/4*4+4;
}
4、通過菜單工程/設定,開啟工程設定屬性頁面,進入Link頁面,在Object/library modules編輯框中,添加檔案名稱libmx.lib libmat.lib libeng.lib。
5、編譯運行程式。