I have a Visual Studio 2010 solution that contains a library of functions, and I would like to be able to use MATLAB as one of several possible front-ends to this library. Therefore, I would like Visual Studio to automatically generate a mex file when I build the solution, without having to export all my build options and paths to mexopts.bat and open MATLAB to build the file from there. I have seen several suggestions to achieve something similar, for example in these posts:
Matlab 7.1+ and Visual Studio 2005
Compiling a MEX file with Visual Studio
How to use CMake and Visual Studio 2010 (64 bit) to build a MATLAB R2011a (64 bit) mex file?
However, they either seem a bit outdated (making references to files that are no longer to be found) or make use of external tools (eg. CMake). Does anyone know how to set up a new project (within the existing solution) in Visual Studio (2010 and newer) that will build a mex file for contemporary MATLAB releases?
For building/linking/compiling, automate visual studio with an extension or macro to
I have automated this for visual studio 2010. This way, you work with your mex-wrapper entirely from the visual studio IDE, have 4 extra pushbuttons for debugging etc. Compile errors are echoed from a matlab terminal window instead of within Visual Studio. Find the Macros uploaded here:
http://www.mathworks.se/matlabcentral/fileexchange/39549-visual-studio-toolbar-for-mex-interface-with-video-tutorial
Quickly Setting up Visual Studio Projects for MEX files with a Property Sheet
All of the settings can be applied via property sheets, a mechanism for rapidly applying Visual Studio project configurations.
Steps:
MATLAB_ROOT
for your 64-bit MATLAB installation, andMATLAB32_ROOT
for any 32-bit MATLAB installations (e.g.C:\Program Files\MATLAB\R2014b\
). This folder has the subdirectories bin, extern, sys, etc. Restart VS if it's opened.That's it!
Just remember that when going between MATLAB to use your MEX file and Visual Studio to build a new version, it will be necessary to run a
clear mex
orclear specificMEXFileName
to be able to overwrite it. I build almost all my MEX files this way.UPDATE (05/22/15): The file MATLAB.props now supports the Parallel Computing Toolbox for using
mxGPUArray
objects. If the toolbox path and library (gpu.lib) exist on your machine, they can be used. Just include the CUDA SDK "Build Customization" (that should be installed if you've installed the CUDA SDK and installed the Visual Studio integrations) to include cuda_runtime.h, etc. Finally, link with cudart_static.lib (but keep Inherit... checked or you will get other linker errors).Property Sheet Details
There are only a few important settings in the property sheet:
$(MATLAB_ROOT)\extern\include
to theAdditionalIncludeDirectories
paths (with inherited paths from parent configurations) -- the location of mex.h.$(MATLAB_ROOT)\extern\lib\win64\microsoft
to theAdditionalLibraryDirectories
paths -- the location of libmex.lib, etc.libut.lib;libmx.lib;libmex.lib;libmat.lib
.mexFunction
(it's a shared library):/EXPORT:mexFunction
..mexw64
for x64).Not necessary, but it also specifies an output manifest that is NOT embedded in the library, sets
MATLAB_MEX_FILE
, and turns on generation of data required for profiling.For completeness, note that there is a more formal "build configuration" system for project configuration, which includes a property sheet, but a loose property sheet is sufficient for setting up a simple MEX project.
A Note About
-largeArrayDims
The
-largeArrayDims
option is a switch to themex
command in MATLAB that simply indicates not to defineMX_COMPAT_32
. So, in Visual Studio, you don't have to do anything since this is not defined by default. If you want the opposite behavior (-compatibleArrayDims
), then defineMX_COMPAT_32
in the Preprocessor section.What's libut.lib for?
I include libut.lib, which provides a few nice functions for detecting a break (CTRL-C) from within a MEX file. The relevant declarations (although this is getting off topic):
After some experimenting with guidance from this page mentioned in the question, it seems like starting with an empty C++ project the following settings in the project's Property Pages are necessary and sufficient to build a working .mexw64 from Visual Studio 2010:
$(MATLAB_ROOT)
is the path to Matlab's root folder, eg. C:\Program Files\MATLAB\R2013a.So far this has only been tried from a solution created from scratch and built for Matlab 2013a 64-bit. I assume that to build for 32-bit one only needs to change both occurrences of 64 to 32. I will update the post when I have confirmed that this works for an existing solution.
EDIT: As expected this works for projects added to existing solutions. Remember to set the new project to be dependent on the project that creates the library.
Edit 2: Following this question I can confirm that the above steps work in Visual Studio 2012, 2013, and 2017 too.