0

I am looking for a simple way to compile Unix MEX files on a Windows 7 computer.

The MEX files compile smoothly in MATLAB 2014a on Mac OS X 10.9 (using "XCode with Clang" as the compiler). Some of the people that I work with, however, are having trouble compiling them in Windows 7 using the C compiler from the Windows 7.1 SDK.

I understand that I might be able to avoid these errors if I use GCC to compile MEX files in MATLAB. I am wondering if anyone knows how. I am happy to download and edit whatever files are necessary so that I can a) compile MEX files within MATLAB by using the "MEX" command and b) guarantee that "-I" and "-L" instructions will also passed to the MEX compiler.

Note, my issue is very similar to this post from 2+ years ago. That said, I have put up a new post since a) MATLAB/MinGW/MEX have all had significant updates since then (not even sure if MinGW is the easiest way out of this mess); b) there is a 64 bit thing (not sure if it's a problem) and c) the "-I" and "-L" options are important.

Community
  • 1
  • 1
Berk U.
  • 7,018
  • 6
  • 44
  • 69
  • ok, but did you try any of the suggested solutions in that question? If so what was the problem? – Amro Oct 15 '14 at 22:04
  • @Amro Yes! I tried [this](http://stackoverflow.com/a/14238515/568249) solution first, but actually ended up ruining the MATLAB MEX setup installation (I suspect because gnumex is simply too old). I also tried to adapt [this solution](http://stackoverflow.com/a/12450944/568249) but could not find the mexopts.bat file in the directory. I thought it was worth asking a new question since the setup is different in multiple ways / enough time had passed for there to be new solutions. – Berk U. Oct 15 '14 at 22:14
  • ok I posted some instructions for MinGW-w64. But using the free Windows SDK is just as easy (perhaps even easier because `mex -setup` can detect it out of the box). – Amro Oct 15 '14 at 22:45

1 Answers1

2
  • Start by downloading MinGW-w64 compiler toolchain. We'll be using the x64 version. Here's the link to the latest binaries as of this moment (GNU GCC 4.9.1).

  • Extract the 7z archive to some location (preferably without spaces), say C:\MinGW-w64\mingw64.

  • Add the bin folder to your PATH environment variable, so something like set PATH=C:\MinGW-w64\mingw64\bin;%PATH% but do it system-wide.

  • Create the following file (feel free to add compiler switches like -std=c++11 if you want C++11 support):

    mexopts_mingw64.bat

    @echo off
    
    set MATLAB=%MATLAB%
    set MW_TARGET_ARCH=win64
    set MINGWROOT=C:\MinGW-w64\mingw64
    set PATH=%MINGWROOT%\bin;%PATH%
    
    set COMPILER=x86_64-w64-mingw32-g++
    set COMPFLAGS=-c -m64 -mwin32 -mdll -Wall -DMATLAB_MEX_FILE
    set OPTIMFLAGS=-DNDEBUG -O2
    set DEBUGFLAGS=-g
    set NAME_OBJECT=-o
    
    set LINKER=x86_64-w64-mingw32-g++
    set LIBLOC=%MATLAB%\extern\lib\%MW_TARGET_ARCH%\microsoft
    set LINKFLAGS=-shared -L"%LIBLOC%" -L"%MATLAB%\bin\%MW_TARGET_ARCH%"
    set LINKFLAGSPOST=-lmx -lmex -lmat
    set LINKOPTIMFLAGS=-O2
    set LINKDEBUGFLAGS=-g
    set LINK_FILE=
    set LINK_LIB=
    set NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%"
    
    set RC_COMPILER=
    set RC_LINKER=
    
  • Now we use it to compile a sample MEX-file:

    >> mex -f mexopts_mingw64.bat -v -largeArrayDims "C:\Program Files\MATLAB\R2014a\extern\examples\mex\yprime.c"
    
    >> yprime(1,1:4)
    ans =
        2.0000    8.9685    4.0000   -1.0947
    

Note: If you are compiling C++ code and you want to distribute the binaries to other people, you might need to also include a couple of DLL files from MinGW which will be dependencies for the compiled MEX-file (stuff like libstdc++). Use Dependency Walker tool to list them all.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • note: `mexopts.bat` files were deprecated starting with R2014a, and are expected to be removed in future versions in favor of XML configuration files. – Amro Oct 15 '14 at 22:48
  • Thank you so much. This setup compiles 'yprime.c' just fine. However, it does not compile the C code that I had previously (which compiles fine in UNIX / Mac OS X). I guess I just want to make sure that compiling using gcc/MinGW dos not always work (to rule out silly mistakes). – Berk U. Oct 23 '14 at 23:32
  • @BerkU.: it should compile fine assuming you're only using standard C/C++ (some POSIX stuff is not portable to Windows, in which case you'll have to find the Win32 equivalent and use `#if/#else` macros around platform-specific functionality) – Amro Oct 23 '14 at 23:43