Windows 64 + Matlab 64 MEX混合编程初步

说明Matlab混合编程下载与安装编译器Microsoft Windows SDK 7.1(C/C++)

点此Microsoft Windows SDK for Windows 7 and .NET Framework 4 (ISO)打开下载页面,是镜像文件,选择64-bit版,文件大小500M+,全部安装需占磁盘近2G。

下载完成后根据提示安装即可,如果出现问题,安装失败,参考: 部分。

TDM-GCC(gcc/g++)

点此tmd-gcc打开下载页面,选择64位版本,40M+,全部安装占用400M+磁盘空间。

根据以前使用经验,安装路径最好不要有空格中文。

安装完成后,开始 -> 运行 -> cmd打开Windows命令窗口,输入gcc -v,如下图所示,显示安装信息,说明安装成功:

最后的战斗: 因为Matlab不支持GNU的编译器,还得做点事,下面的方法来自Using GCC (MinGW) as MATLAB’s MEX compiler 中的的解答:

下面的代码是从Bogdan主页上下载的mexopts.bat文件代码,使用时需要做一点小小的改动。

在下面的代码中找到:set MINGWPATH=p:\mingw64这句代码,,将其中的路径p:\mingw64替换成你的TMD-GCC-64的安装目录,如我的是:E:\devtools\TDM-GCC-64,然后保存并将mexopts.bat文件copy到计算机如下路径(复制下面的路径地址,粘贴到资源管理器窗口地址栏,回车即可):

%USERPROFILE%\AppData\Roaming\Mathworks\MATLAB\R2014a\

下面是代码:

@echo off:: NOTE: this is actually not a proper .bat file executed by Windows. MEX::parses it and only understands a very reduced set of commands:::”set” and “rem” apparently, everything else is ignored (behaves as::”rem”), so don’t do any fancy batch stuff in here. There are some::undocumented special vars you can set here that will trigger MEX::to do fancy stuff.:: Set this to your Mingw64 top folder, where you extracted the aboveset MINGWPATH=p:\mingw64:: Leave these alone unless you know what you’re doing.set PATH=%MINGWPATH%\bin;%PATH%set PRELINK_CMDS=echo.>%TEMP%\mexstaticlibs:: You can have MEX run some commands before calling the linker.:: The two examples below will cause gcc to output the full path to some:: static libraries so you can link statically to them (see the:: LINGFLAGSPOST special var below). You can set any command here, however.rem set PRELINK_CMDS1=gcc -print-file-name=libwinpthread.a >> %TEMP%\mexstaticlibsrem set PRELINK_CMDS2=gcc -print-file-name=libquadmath.a >> %TEMP%\mexstaticlibsrem set PRELINK_CMDS3=…:: You can have MEX run some commands also after calling the linker:: (e.g. upx compress the output .mex)rem set POSTLINK_CMDS1=upx -9 “%OUTDIR%%MEX_NAME%%MEX_EXT%”rem set POSTLINK_CMDS2=…:: You can change these if you really need to.set COMPILER=g++set COMPFLAGS=-c -I”%MATLAB%\extern\include” -DMATLAB_MEX_FILEset OPTIMFLAGS=-O3 -funroll-loops -DNDEBUGset DEBUGFLAGS=-gset NAME_OBJECT=-oset LINKER=g++set LINKFLAGS=-shared -static-libstdc++ -static-libgcc -L”%MATLAB%\bin\win64″ -L”%MATLAB%\extern\lib\win64\microsoft” -lmex -lmx -leng -lmat -lmwlapack -lmwblasset LINKFLAGSPOST=@%TEMP%\mexstaticlibsset NAME_OUTPUT=-o “%OUTDIR%%MEX_NAME%%MEX_EXT%”:: EXAMPLES:: ========:: You can compile simple files using “mex file.cpp”. To support more than 2^32 elements, use:: “mex -largeArrayDims file.cpp” … use this by default, unless you know what you’re doing.:: To add include dirs, lib dirs, or compile/link flags, do::: mex COMPFLAGS=”$COMPFLAGS -std=gnu99 -Ix:/include/dir” LINKFLAGS=”$LINKFLAGS -Lx:/libs/dir -lmylib” -largeArrayDims file.cpp选择编译器

查看可用编译器:使用mex -setup查看可选择的编译器,如果没有需要自己安装,如果已安装且只安装了编译器Microsoft Windows SDK 7.1(C/C++),显示如下结果:

选择编译器:使用mex -setup lang选择编译器,如mex -setup C++选择C++编译器,如下图:

如果你也按上面 中讲的方法,配置了TDM-GCC-64位的编译器,那么,你会发现结果如下:

刚开始我也以为不能用,后来试着编译了一下,竟然可以!

DT.c文件中重复定义了无穷大,注释掉,就没有提示啦!

编写c/cpp文件

与普通的C文件的编写有两点不同:

包含mex.h头文件,即:#include “mex.h”;

编写mexFunction函数。

下面以一个求和的例子说明:

add(double x, double y){return x + y;}// MEX文件接口函数void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]){double *z;double x, y;plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);z = mxGetPr(plhs[0]);x = *(mxGetPr(prhs[0]));y = *(mxGetPr(prhs[1]));*z = add(x, y);}

Matlab命令窗口输入mex add.cpp,运行结果如下图:

mexFunction函数介绍

mexFunction函数接口如下:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

可见,是没有返回值的,其实是通过指针数组plhs传递的。它共有四个参数,即左边的输出参数及数目和右边的输入参数及数目,具体参数意义如下:

参数 意义 英文全称 类型

nlhs 左边输出参数的数目 number of left-hand side 整型(int)

plhs 指向输出参数的指针 pointer of left-hand side 指针数组

nrhs 右边输入参数的数目 number of right-hand side 整型(int)

prhs 指向输入参数的指针 pointer of right-hand side 指针数组

详情参考:

编译多个c/cpp文件

假设A.c调用B.c,B.c调用C.c 可以使用如下命令编译:

mex C.cmex B.cmex A.c

或者

mex A.c B.c C.cProblem&Solution找不到编译器或SDKProblem

提示:“未找到支持的编译器或 SDK”,如下图:

Solution

根据Matlab的提示,安装:Microsoft Windows SDK for Windows 7 and .NET Framework 4 (ISO),在下载页面,有三个ISO文件可以选择,如下表选择第三个,即amd64。

名称 版本

GRMSDK_EN_DVD.iso x86

GRMSDKIAI_EN_DVD.iso Itanium

GRMSDKX_EN_DVD.iso amd64

注意

如果安装Microsoft Windows SDK for Windows 7 and .NET Framework 4 (ISO)时出现如下错误:

A problem occurred while installing selected Windows SDK components.

Installation of the “Microsoft Windows SDK for Windows 7” product has reported the following error: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information.

Please attempt to resolve the problem and then start Windows SDK setup again. If you continue to have problems with this issue, please visit the SDK team support page at ?LinkId=130245.

Click the View Log button to review the installation log. To exit, click Finish.

如下图:

其实你已经错过了旅行的意义。

Windows 64 + Matlab 64 MEX混合编程初步

相关文章:

你感兴趣的文章:

标签云: