点此Microsoft Windows SDK for Windows 7 and .NET Framework 4 (ISO)打开下载页面,是镜像文件,选择64-bit版,文件大小500M+,全部安装需占磁盘近2G。
下载完成后根据提示安装即可,如果出现问题,安装失败,参考:Problem&Solution 部分。
点此tmd-gcc打开下载页面,选择64位版本,40M+,全部安装占用400M+磁盘空间。
根据以前使用经验,安装路径最好不要有空格中文。
安装完成后,开始 -> 运行 -> cmd
打开Windows命令窗口,输入gcc -v
,如下图所示,显示安装信息,说明安装成功:
最后的战斗:
因为Matlab不支持GNU的编译器,还得做点事,下面的方法来自Using GCC (MinGW) as MATLAB’s MEX compiler 中的Bogdan的解答:
下面的代码是从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.
:: You can use MinGW64 builds (win32 threads + seh unwinding) from here:
:: http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/
:: Tested with the following:
:: http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/4.9.1/threads-win32/seh/x86_64-4.9.1-release-win32-seh-rt_v3-rev1.7z
:: Set this to your Mingw64 top folder, where you extracted the above
set 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%\mexstaticlibs
rem set PRELINK_CMDS2=gcc -print-file-name=libquadmath.a >> %TEMP%\mexstaticlibs
rem 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_FILE
set OPTIMFLAGS=-O3 -funroll-loops -DNDEBUG
set DEBUGFLAGS=-g
set NAME_OBJECT=-o
set LINKER=g++
set LINKFLAGS=-shared -static-libstdc++ -static-libgcc -L"%MATLAB%\bin\win64" -L"%MATLAB%\extern\lib\win64\microsoft" -lmex -lmx -leng -lmat -lmwlapack -lmwblas
set LINKFLAGSPOST=@%TEMP%\mexstaticlibs
set 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(gcc/g++) 中讲的方法,配置了TDM-GCC-64位的编译器,那么,你会发现结果如下:
刚开始我也以为不能用,后来试着编译了一下,竟然可以!
DT.c文件中重复定义了无穷大,注释掉,就没有提示啦!
与普通的C文件的编写有两点不同:
包含mex.h头文件,即:#include "mex.h"
;
编写mexFunction函数。
下面以一个求和的例子说明:
#include "mex.h" // 使用MEX文件必须包含的头文件
// 执行具体工作的C函数
double 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函数接口如下:
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 | 指针数组 |
详情参考:
假设A.c
调用B.c
,B.c
调用C.c
可以使用如下命令编译:
mex C.c
mex B.c
mex A.c
或者
mex A.c B.c C.c
提示:“未找到支持的编译器或 SDK”,如下图:
根据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 http://go.microsoft.com/fwlink/?LinkId=130245.
Click the View Log button to review the installation log.
To exit, click Finish.
如下图:
解决办法:
出现此问题,很可能是你的PC机已经安装了:Microsoft Visual C++ 2010,而在安装Microsoft Windows SDK for Windows 7 and .NET Framework 4 (ISO)时默认选择安装Microsoft Visual C++ 2010,如下图,造成的冲突,可以卸载已经安装的,注意32和64位的都要卸载。
卸载完成后从新安装即可!!!
提示错误:error C2143: syntax error : missing ‘;’ before ‘type’,如下图所示:
出现这种现象,很可能是你在用VC的编译器编译GNU的C语言写的代码,两个编译器中的C标准不一样。
一种方法是将文件后缀名.c
,改成.cpp
,重新编译,但可能还会有其它的错误。
如果源文件是GNU的C代码,建议安装相应的编译器,然后编译,可参考TDM-GCC(gcc/g++)
Windows 64 + Matlab 64 MEX混合编程初步
原文地址:http://blog.csdn.net/enjoyyl/article/details/46545263