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:
- Download the property sheet (MATLAB.props) from this GitHib repo.
It's short and sweet. I'd actual urge you to make your own to learn what's involved in the process. See the Property Sheet Details section below for a description.
- Set the MATLAB root environment variables:
MATLAB_ROOT
for your 64-bit MATLAB installation, and MATLAB32_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.
- Create an empty DLL project in Visual Studio, optionally creating a x64 solution platform. Do this by choosing "Win32 Project" and selecting DLL as follows:
- In "Property Manager" (select from the View menu), for each project's build configuration, right click and choose "Add Existing Property Sheet...", and select the appropriate property sheet (32 or 64 bit). (See screenshot below)
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
or clear 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:
- Adding
$(MATLAB_ROOT)\extern\include
to the AdditionalIncludeDirectories
paths (with inherited paths from parent configurations) -- the location of mex.h.
- Adding
$(MATLAB_ROOT)\extern\lib\win64\microsoft
to the AdditionalLibraryDirectories
paths -- the location of libmex.lib, etc.
- Listing the libraries:
libut.lib;libmx.lib;libmex.lib;libmat.lib
.
- Exporting
mexFunction
(it's a shared library): /EXPORT:mexFunction
.
- Setting the output file extention (e.g.
.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 the mex
command in MATLAB that simply indicates not to define MX_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 define MX_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):
// prototype the break handling functions in libut (C library)
extern "C" bool utIsInterruptPending();
extern "C" void utSetInterruptPending(bool);