编译器:Microsoft Windows SDK 7.1(C/C++) 和 GNU编译器(有MinGW(Minimalist GNU for Windows,仅32位)、MinGW64(64位和32位)、TMD-GCC(非官方的编译器包)、CygWin等等,它们间的区别与联系参见GCC_Cygwin_MinGW_TDM-GCC) , f6 m) g" _- |& @8 F
O+ ?. ^5 v4 p) I1 k) |2 e4 T5 T' L1 v- n, y
Matlab混合编程( \+ L, z' J" {4 Z: V! u
下载与安装编译器 W5 B# S9 [; r# e4 v, ]* _) x6 Q5 N4 |Microsoft Windows SDK 7.1(C/C++) - t# v% Z. a: s2 h3 o点此Microsoft Windows SDK for Windows 7 and .NET Framework 4 (ISO)打开下载页面,是镜像文件,选择64-bit版,文件大小500M+,全部安装需占磁盘近2G。! L5 Z$ Y# V' Q/ K. y
6 _" ]; v0 K9 P) G下载完成后根据提示安装即可,如果出现问题,安装失败,参考:Problem&Solution 部分。 1 R+ K# D; Y1 J( B1 A Y! I7 a7 Q9 q% @ 5 f) }' n1 e( x7 g" ATDM-GCC(gcc/g++)! m& Q) a9 `# | b5 n& C' E
For Matlab 2015 + g- P$ B) N. u' GMatlab 2015已经开始支持GNU编译器,具体参见:Supported and Compatible Compilers for R2015b,安装方法参见:Install MinGW-w64 Compiler。4 \( A8 |1 |$ p2 D
/ n, M6 E- Z* b$ z- Y, s& {
% w9 e' y# @, L- l3 ^, U# {最后的战斗: , d! l- a" r$ J0 V4 J+ {因为Matlab不支持GNU的编译器,还得做点事,下面的方法来自Using GCC (MinGW) as MATLAB’s MEX compiler 中的Bogdan的解答:8 @) s' h+ w2 u4 h6 B. y* W6 P
" t5 b7 W0 W* j% x, s, F! z2 v下面的代码是从Bogdan主页上下载的mexopts.bat文件代码,使用时需要做一点小小的改动。 2 G; \, i2 }4 e! l1 a8 O3 O* ` $ E- Z$ ]8 G; _9 B5 s在下面的代码中找到:set MINGWPATH=p:\mingw64这句代码,将其中的路径p:\mingw64替换成你的TMD-GCC-64的安装目录,如我的是:E:\devtools\TDM-GCC-64,然后保存并将mexopts.bat文件copy到计算机如下路径(复制下面的路径地址,粘贴到资源管理器窗口地址栏,回车即可):( C4 v& X+ @/ ?# l
, V" y1 R+ i+ t/ V4 m
%USERPROFILE%\AppData\Roaming\Mathworks\MATLAB\R2014a\ : a! i! w+ V2 P" s1 k. I3 H8 ~3 r" G1 Z7 r$ Q6 Y. N: v
下面是代码: ! u3 A/ _5 t3 G. g ( ]0 s4 [3 Q) v@Echo off5 r) U6 ~, Y: ~* i6 M O
4 a5 [+ }$ u: N9 x' Z
:: NOTE: this is actually not a proper .bat file executed by Windows. MEX 3 K* q8 X3 _6 y! f; X1 z:: parses it and only understands a very reduced set of commands:6 A! }/ `* @) x. ?( w# |2 _) Z. C
:: "set" and "rem" apparently, everything else is ignored (behaves as / Q8 q$ ^* u; W( M' ?:: "rem"), so don't do any fancy batch stuff in here. There are some' h+ ?6 ?/ w8 g( q: n% L0 ^
:: undocumented special vars you can set here that will trigger MEX/ b* S5 a0 K5 m3 y; Z
:: to do fancy stuff. 8 ?2 C! y, o2 a- B% U* }, I/ N) g% N7 ~3 x; V( x
:: You can use MinGW64 builds (win32 threads + seh unwinding) from here:+ B4 k$ |" c! m' X% C
:: http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/. j, x2 D* V; b
' C0 B7 a4 U7 I1 \
:: Tested with the following:; D) i6 G7 r2 O- }, }# k% [
:: 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 1 L3 R' f' j' z9 c2 \ / ], J* N5 ~ r3 `1 P* ~:: Set this to your Mingw64 top folder, where you extracted the above, s9 t& S: P) _2 o
set MINGWPATH=p:\mingw64 ( x% [) u- _8 Q _: n. x : B6 ~6 V4 D. l# O% ?3 q:: Leave these alone unless you know what you're doing. ! U6 G+ _# z% w oset PATH=%MINGWPATH%\bin;%PATH% : R1 }: G. p( C1 {- }set PRELINK_CMDS=echo.>%TEMP%\mexstaticlibs; {& j; n k7 I7 `% ]( A- J
, U$ ~- E9 c& D* _* s3 [2 Y- ?; f:: You can have MEX run some commands before calling the linker. & _# o S: W2 A6 H( s" J" {1 [:: The two examples below will cause gcc to output the full path to some / s* A( B& r D. J2 j:: static libraries so you can link statically to them (see the: {5 D9 c$ x# f6 n# c' W1 b/ I, b
:: LINGFLAGSPOST special var below). You can set any command here, however. 4 g% I% u$ D; e. krem set PRELINK_CMDS1=gcc -print-file-name=libwinpthread.a >> %TEMP%\mexstaticlibs0 @% q5 s* U/ _
rem set PRELINK_CMDS2=gcc -print-file-name=libquadmath.a >> %TEMP%\mexstaticlibs! f& {% d! u% p! Y c
rem set PRELINK_CMDS3=... + W( g- o3 n* ]! c7 S& G5 m m * z0 A$ p) k, k:: You can have MEX run some commands also after calling the linker5 {4 z# q1 O( A8 K* _, R. T
:: (e.g. upx compress the output .mex) & h& T$ Q' N+ z( ^ L; F# Vrem set POSTLINK_CMDS1=upx -9 "%OUTDIR%%MEX_NAME%%MEX_EXT%" " o$ j$ B- D% K w- srem set POSTLINK_CMDS2=...1 c2 y7 x9 Y. u9 n! O( D
; ]6 ^7 a+ M/ Y( w& F:: You can change these if you really need to.1 }( J V% |2 D7 n V
set COMPILER=g++5 u, h/ J6 H, X* E. L1 C9 u
set COMPFLAGS=-c -I"%MATLAB%\extern\include" -DMATLAB_MEX_FILE ) f$ [$ ~( F! p& l% Aset OPTIMFLAGS=-O3 -funroll-loops -DNDEBUG1 T; U. s7 m I+ J. \+ O
set DEBUGFLAGS=-g, Z0 T: T4 T" s, I
set NAME_OBJECT=-o * D0 _. R' y8 {) @& P6 A; e$ R/ M4 _
set LINKER=g++# u+ C @) h% q$ B2 }
set LINKFLAGS=-shared -static-libstdc++ -static-libgcc -L"%MATLAB%\bin\win64" -L"%MATLAB%\extern\lib\win64\microsoft" -lmex -lmx -leng -lmat -lmwlapack -lmwblas 6 \+ j# F+ ^3 Z: z0 p' A X: _& @ L7 |/ {9 [1 C& _9 W8 G; o
set LINKFLAGSPOST=@%TEMP%\mexstaticlibs 6 N' K4 ~7 W4 q$ \$ E! W$ D' [set NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%" , Q1 m, r+ {4 @; b& e! ^/ u1 t' F- K; h3 Z7 n9 H8 ~4 c7 n7 p
8 ]% b1 j) k; J* q7 L J:: EXAMPLES 6 b: i8 S1 L# d1 [* C# g& z: M# Q, v5 c:: ========4 D0 g: {" Y# b; p" B* W
6 f5 m/ b( P4 V5 D* @8 B8 A8 V
:: You can compile simple files using "mex file.cpp". To support more than 2^32 elements, use; A- ]2 g% ^* G M
:: "mex -largeArrayDims file.cpp" ... use this by default, unless you know what you're doing.) R& M- {( `( W" Z* B0 Q( ]
/ W8 c. h' }3 s( |$ \:: To add include dirs, lib dirs, or compile/link flags, do: ' t, A% C: j4 W4 ~' v2 `+ w' b, s:: mex COMPFLAGS="$COMPFLAGS -std=gnu99 -Ix:/include/dir" LINKFLAGS="$LINKFLAGS -Lx:/libs/dir -lmylib" -largeArrayDims file.cpp I: ?8 t i$ y# \8 { $ w3 B: P: J; U0 Q1 b' N# _0 n 6 w* ?4 C" X* _6 d选择编译器 - n" ^ a0 W* W; H查看可用编译器:使用mex -setup查看可选择的编译器,如果没有需要自己安装,如果已安装且只安装了编译器Microsoft Windows SDK 7.1(C/C++),显示如下结果: 2 M# z) v7 j4 O* w$ j, z" V