最简单的例子,找不到的源、库文件可以从/MATLAB701/extern/里面拷贝,已验证
1 、启动MATLAB7,新建m文件,输入:
function myfun(n)
t = 0 :n;
y = sin(t);
plot(y);
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 ;
}
//
#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 、编译运行程序。