VC10.0(VS2010) 与 matlab混编工程配置

转自网络,亲自验证过。

环境:VS2010、MatlabR2010b、XP 32bit

1、VS2010里新建一个工程,Win32 Console Application,默认。

2、菜单:Project → Properties... 选择 Configuration Properties下的 VC++ Directories。

在 Include Directories 栏目中添加matlab相关的include文件。例如..\MATLAB\R2010b\extern\include和..\MATLAB\R2010b\extern\include\win32

在 Library Directories 栏目中添加matlab相关的lib文件。例如:...\MATLAB\R2010b\extern\lib\win32\microsoft。

3、菜单:Project → Properties... 选择 Configuration Properties下的 Linker → Input。

在 Additional Dependencies栏目中输入项目需要引用matlab的的lib文件库。例如:libeng.lib libmx.lib  libmex.lib

4、菜单:Project → Properties... 选择 Configuration Properties下的 Linker → General。

 

在 Additional Library Directories 栏目中,添加库所在的目录名称...\MATLAB\R2010b\bin\win32

5、把代码换成如下测试代码,编译。

#include "stdafx.h"
#include "engine.h" // 包含引擎函数的头文件
#include <iostream>
int main(int argc, char *argv[])
{
    char buf[1024];
    Engine *ep = engOpen(NULL); // 启动Matlab
    engSetVisible(ep, true); // 设置窗口不可见
    engOutputBuffer(ep, buf, 1024);
    mxArray *mxN = mxCreateDoubleScalar(3);
    engPutVariable(ep, "N", mxN);
    // 将变量放置到Matlab空间中
    engEvalString(ep, "D=rand(N, N)");
    // 在Matlab环境中执行代码
    mxArray *mxData = engGetVariable(ep, "D");
    // 获得返回的结果
    std::cout << buf << std::endl;
    engClose(ep); // 关闭Matlab
    system("pause");
    return 0;
}

 

 

posted @ 2012-11-05 15:47  Craftor  阅读(2323)  评论(0编辑  收藏  举报