Compiling mex files with Visual Studio 2013

Configuration: Matlab 2015b, Visual Studio 2013, Intel 64bit machine.

In Visual Studio do the following:

1) File -> New Project; Select location and name; in the project type, select Templates -> Visual C++ -> Win32 -> Win32 Console Application -> OK;

2) In the Win32 Application Wizard, click Next, in the Application Type choose DLL, then click Finish.

3) Project -> Properties -> Configuration Manager -> Active Solution Platform -> New -> Type or Select the New Platform choose x64 -> OK -> OK;

4) Project -> Properties -> Configuration Manager -> Active Solution Platform -> x64 -> Ok;

5) Project -> Properties -> Configuration Manager -> Active Solution Configuration -> Release -> OK;

6) Project -> Properties -> Configuration Properties -> General -> Target Extension -> change .dll to .mexw64;

7) Project -> Properties -> Configuration Properties -> VC++ Directories -> Include Directories -> Add: C:\Program Files\MATLAB\R2015b\extern\include;

7a – optional – only if needed) Project -> Properties -> Configuration Properties -> C/C++ -> Additional Include Directories -> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include;

7b – optional – only if needed) Project -> Properties -> Configuration Properties -> C/C++ -> Additional Include Directories -> FFTW directory;

8) Project -> Properties -> Configuration Properties -> Linker -> General -> Additional Library Directories -> C:\Program Files\MATLAB\R2015b\extern\lib\win64\microsoft;

8a – optional – only if needed) Project -> Properties -> Configuration Properties -> Linker -> General -> Additional Library Directories -> D:\FFTW64;

9) Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> libmx.lib;libmex.lib;libmat.lib;

9a – optional – only if needed) Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> libfftw3-3.lib; libfftw3f-3.lib;

10) Project -> Properties -> Configuration Properties -> Linker -> Command Line -> Additional Options -> Add /export:mexFunction;

11) Add the C/C++ source code for the mex file; remember that the .cpp files need #include “stdafx.h”.

12) After the compilation, the mex file projectTitle.mexw64 will be available at …\projectRootDirectory\x64\Release; remember to add this directory in the Matlab script file; after that, you can call the C/C++ mex routine as a function whose name is the project name; remember to terminate the Matlab script with “clear mex” so that you can newly compile the project after any modification without having Matlab keeping the .mexw64 file busy.

We expect that this procedure will work also for Visual Studio 2010 and Visual Studio 2012 and for other Matlab versions.

Leave a Reply

Your email address will not be published. Required fields are marked *