Compiling Mex in Matlab for WARPLab

MEX is short for "Matlab EXecutable" and allows users to create programs within C or C++ that can be executed from and share data with Matlab. For more information on MEX, please refer to the Matlab Documentation on MEX.

Within WARPLab we have implemented some functionality in MEX, like our WARPLab MEX UDP Transport. Since we cannot support the many different system configurations of our users, it therefore becomes necessary to set up an environment where you can compile MEX code on your system.

This how-to will go through the steps to allow your Windows machine to compile MEX code. As of WARPLab version 7.3.0, the WARPLab MEX UDP Transport only supports the Windows operating system and relies on Windows libraries for socket and timing support.

Windows Operating Systems

NOTE: These steps were completed on a Windows 7 Professional 64-bit system. There may be some variances for other versions of Windows. If you find any discrepancies, please let us know.

Installing Microsoft Visual C++ Express 2010 and Windows SDK

In order to compile MEX, we recommend installing Microsoft Visual C++ Express 2010 (This tutorial should work with Microsoft Visual C++ Express 2012 but might have minor differences) and the Windows SDK (version 7.1). For additional tool installations, please refer to the Recommended Tools for WARP v3.

Please follow the procedures from Microsoft for downloading and installing Microsoft Visual C++ Express 2010 and the Windows SDK.

One common error that we have seen is Return Code 5100. This indicates that there were existing installs of redistributable Microsoft Visual C++ (for example, MATLAB will install a redistributable Microsoft Visual C++ program as part of its install) and that the installation could not proceed. In that case, you need to uninstall existing Visual C++ redistributable installations. You can see a good summary of the solution:

For example, you can see that on one of our machines, there are many distributions of Microsoft Visual C++ installed on the computer as part of installs from other programs (see "Control Panel" -> "Programs and Features"):

which can lead to issues when trying to install the Windows SDK.

Setting up the Matlab environment for compiling MEX

To setup MATLAB to compile MEX, you first need to run

mex -setup

This will invoke the MATLAB utility to setup the default compiler:

Welcome to mex -setup.  This utility will help you set up  
a default compiler.  For a list of supported compilers, see 
Please choose your compiler for building MEX-files: 
Would you like mex to locate installed compilers [y]/n? 

Select "Yes" and MATLAB should locate the compiler you installed above.

Select a compiler: 
[1] Microsoft Visual C++ 2010 Express in C:\Program Files (x86)\Microsoft Visual Studio 10.0 
[0] None 

Select "Microsoft Visual C++ 2010 Express" as the compiler to use. Then verify your selection:

Please verify your choices: 
Compiler: Microsoft Visual C++ 2010 Express  
Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0 
Are these correct [y]/n? 

Select "Yes" and you should see a couple of warnings:

  Warning: MEX-files generated using Microsoft Visual C++ 2010 require 
           that Microsoft Visual Studio 2010 run-time libraries be  
           available on the computer they are run on. 
           If you plan to redistribute your MEX-files to other MATLAB 
           users, be sure that they have the run-time libraries. 
Trying to update options file: C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB\R2011a\mexopts.bat 
From template:              C:\MATLAB\R2011a\bin\win64\mexopts\msvc100freeopts.bat 
Done . . . 
  Warning: The MATLAB C and Fortran API has changed to support MATLAB 
           variables with more than 2^32-1 elements.  In the near future 
           you will be required to update your code to utilize the new 
           API. You can find more information about this at: 
           Building with the -largeArrayDims option enables the new API. 

If you have any issues with compiling MEX, you can look at the compiler options by running:

cc = mex.getCompilerConfigurations()

by exploring the "Details" field, you can see all of the Compiler and Linker flags. By looking at mex -help, you can see how to override the default compiler and linker options.

Compiling MEX

To see all the options when compiling MEX, please run:

mex -help

Some parameters that are commonly used (ie you can see them as part of the compile command for wl_mex_udp_transport.c) are:

  1. This provides debug information that allows you to debug your MEX C / C++ code within Microsoft Visual C++ Express 2010.
               Create a MEX-file containing additional symbolic information for
               use in debugging. This option disables MEX's default behavior of
               optimizing built object code (see the -O option).
  2. This optimizes your code, even if the debug options are turned on. Early in your debug process, you should leave this option out so that you can better understand what the code is doing.
               Optimize the object code. Optimization is enabled by default and
               by including this option on the command line. If the -g option
               appears without the -O option, optimization is disabled.
  3. If you need any external libraries, like the Winsock Library, then you need to specify the object library as part of the compile.
               Link with object library. On Windows, name expands to
               "<name>.lib" or "lib<name>.lib" and on UNIX, to 
               "lib<name>.LIBEXT", where LIBEXT is platform dependent.  Do not
               add a space after this switch.

One example of a compile command used for the MEX UDP Transport within WARPLab is:

mex -g -O wl_mex_udp_transport.c -lwsock32 -lKernel32 -DWIN32

Debugging MEX in MATLAB

To debug the MEX code, first open Microsoft Visual C++ Express 2010 that you installed above.

Next, open the MEX C file within Microsoft Visual C++ Express 2010:

"File"->"Open"->"File" (Ctrl+O)

For example, you can open wl_mex_udp_transport.c from the WARPLab release.

Once the file is open, you need to have MATLAB open and ready to run your MEX code. Now, select:

"Tools" -> "Attach to Process ..." (Ctrl+Alt+P)

If you don't see this menu option, please select

"Tools" -> "Settings" -> "Expert Settings"

After you select "Attach to Process" a dialog will appear that will allow you to select MATLAB.exe as the process. Once Visual C++ Express 2010 attaches to a process, the screen should change to the "Debug" view:

Once you attach to the process, you should then be able to debug your code (ie set breakpoints, etc) using the standard Visual C++ Express 2010 methods.

For example, once you have set a breakpoint, then you can run your code within MATLAB and have the program halt at the breakpoint to see the various variables and other watchpoints:

Last modified 10 years ago Last modified on Dec 22, 2014, 12:28:48 PM

Attachments (4)

Download all attachments as: .zip