compiling mex files with mingw64

Compiling mex files on 64-windows has always been a huge pain.  It used to require having a real copy of Visual Studio.  A year or so ago, there was a platform sdk that finally included a 64-bit compiler.

Now, the mingw64 project is sufficiently advanced to let us compile mex files.  This is all nicely packaged up in the cygwin repository too.

Don’t worry, the mingw compilers in the cygwin repository generate fully redistributable native windows code without any cygwin dependencies (a small caveat: I’m not sure about whether or not you need a libstdc++.dll file for g++ codes… comments?) 

  1. Get setup.exe from cygwin
  2. Search for mingw and mark all the mingw modules for installation
  3. Search for mingw64 and mark all the mingw64 modules for installation (yes, this is way more than strictly necessary, but its the easiest way to go)
  4. Install those modules and all dependencies to the default location: C:\cygwin
Now, in matlab, run
>> edit(fullfile(prefdir,’mexopts.bat’))
Create the file if it doesn’t exist, and paste the contents of the following gist into it.
That should let you compile and run 64-bit mex files
I’m still working on a way of building Fortran mexfiles.  You can use the gfortran compiler in windows, but I haven’t gotten things working yet.
If anyone that is knowledgeable about the following areas, I’d love to hear from you:
  • Using mingw64 compilers with gnumex.  Is this still necessary?  The above seems to work pretty nicely!
  • Why doesn’t -static-libstdc++ work under the cygwin version of x86_64-w64-mingw32-gcc-4.5.3 ?
  • How the heck can we get a single mexopts.bat file that’ll work with fortran codes too?
  • Can we get matlab to include a default config file for this compiler setup and support it?

Special thanks to Oren Livne and the dynare team for helping to figure this stuff out!  (The above mexopts.bat file is basically what they had with a few small changes that Oren and I discovered.)

This entry was posted in Uncategorized. Bookmark the permalink.

7 Responses to compiling mex files with mingw64

  1. piethonic says:

    Thanks! Unfortunately, I’m using GCC 4.5.3 in cygwin and the linker option “-static-libstdc++” is making life rather difficult at the moment:

    x86_64-w64-mingw32-g++: unrecognized option ‘-static-libstdc++’

  2. Eduardo says:

    Thank you! The IT at the place I’m working didn’t want to install a compiler on my computer. I downloaded the binaries from mingw64, added them to the path, and now I can compile mex files without problem!

  3. Eduardo says:

    By the way, I found a problem while compiling with mingw64, apparently if you use an unsupported compiler, it is likely that the function mexErrMsgTxt will crash Matlab. This has been reported in Linux: http://www.mathworks.co.uk/support/solutions/en/data/1-QBCS1/?solution=1-QBCS1. Is there any solution to this problem?

  4. steve says:

    I successfully followed a similar procedure but without the reliance on cygwin. You can download the mingw 64-bit compiler directly. You will need to setup the appropriate paths and change the mexopts.bat file to point to your mingw64 directory instead of the cygwin directory.

    • dgleich says:

      That’s great to hear! Do you want to post any of your notes here? download URLs and such?

    • Lorenzo says:

      I’ve done something like this on MatLab2009b and it works. On MatLab2013a it works, but when I try to run for example mex yprime.c it builds a file called yprime.mexw32. I expected to get a file called yprime.mexw64 instead. Is there anyone who knows how to fix this problem (if it is a problem)? By the way, even if the file is not called as I expected it works. I’m working with Windows 7 64bit, MatLab2013a 64bit, mingw64.

  5. Gnumex works with 32 bit and 64 bit MinGW-w64

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s