How to create a Mex project using Visual Studio 2008

By | June 23, 2016

This method for creating Matlab Mex executable file has been tested using Matlab R2011b & Microsoft Visual Studio 2008 & 64 bit Windows 7 OS system.

1. Creation of an empty project

  • Create a simple project under VC++ (File / New Project), name it as you wish

1

  • On the left hand side, click on “Application Settings” (or equivalently click on the “Next” button) and make sure you select “DLL” and check “Empty project”.

2

  • Creation of the definition file

In order to run your code Matlab needs to know where the “entry point” is. In any M-files the execution starts at the first line of the first function found in the file. Other functions may exist in the file but they are executed only when called. In the case of MEX files, Matlab will always try to run a function called “mexFunction”. Therefore we need to tell the compiler that this function needs to be accessible from outside. You can do as follows :

* Create a new empty file with the extension “.def”. In this case I have added the file “keepPos.def”.

(An easy way to do this is to create a simple header file and rename it “Right click on the project name / Add / New Item / Header File and update the name”)

* Move the file to the root of the project.

This file is called a definition file. As explained before this file is used by the compiler/linker to know which functions are accessible from outside.

Here is the syntax to follow:

LIBRARY KEEPPOS # here could be your project name 
EXPORTS mexFunction

3

2. Configuration of Compiler

Now we need to define some important properties of the project to ensure that the output file is fully compatible with Matlab. Open project properties (Right click on the project name / Properties). Start with the “Configuration Properties / C/C++” tab

  • Under “General / Additional Include Directories ”add Matlab external include directory : (I am also using Intel IPP, so Intel IPP include directory is also added here)

4

  • Then add the following string “MATLAB_MEX_FILE” to the list of Preprocessor definitions

5

  • Make sure that under “Advanded”, the convention “_cdecl” is chosen.

3. Configuration of Linker

Now look at the “Configuration Properties / Linker” properties

  • Under “General”, define the name of the “Output file”: remember the extension must be .mexw32 (or .mexw64 under 64bit Windows) and the file name will be the name of the function in Matlab.

6

  • In the same screen we now add the additional Matlab library directories (Also I am using Intel IPP, so Intel IPP lib directory is added here)

7

As we use a Microsoft Visual C++ compiler in this example, we select versions compatible with Windows.

  • We now add the libraries in “Additional Dependencies” ( “libmat.lib”, “libmex.lib” and “libmx.lib” for Matlab), corresponding libraries for IPP and another libraries used in the source code.
  • Last we define the “Definition file” for the library

8

4. Adding Matlab ressource file (not necessary)

Add the MEX Resource file: “Right click / Add / Existing Item”, add the file “mexversion.rc” from the directory “{$Matlab}/extern/include”.

I actually spent lots of time trying to find this resource file in my system, however I ain’t be able to get it. Apparently, Matlab decided not to keep this file any longer for their new Matlab release (at least for R2011b under which I am testing). What this file does it basically tells the system about information of the matlab version and Mex script version (you can tell from file name:). After struggling hours on the internet trying to find a solution, there seems to be no easy way to explicitly avoid this (maybe you can find one:). Finally I found a Chinese forum where people tested without this file and it worked. So I decided to be blind to this file, it proves that they were right, this file is not necessary for compiling and linking. Matlab may have made a smart way to implicitly avoid this file, or this information of this file is not needed in my code?!. For your information, I add a sample of this file I found from the internet here:

/////////////////////////////////////////////////////////////////////////////
//  Copyright 1997-2008 The MathWorks, Inc.
//
//  Mex versioning resource file
//  This should be compiled with the ARRAY_ACCESS_INLINING
//  define if it applies.
//
//  DO NOT EDIT - this is for MATLAB use only
/////////////////////////////////////////////////////////////////////////////

//  Define strings,
//  ML_VERSION is the version of Matlab that the Mex file is built against.
//  INLINING determines in inlining was switched on or not.
#define ML_VERSION       100
#define INLINING     101
/////////////////////////////////////////////////////////////////////////////
//
// String Table
//
STRINGTABLE
BEGIN
ML_VERSION,          "MATLAB R14 native"
#ifdef ARRAY_ACCESS_INLINING
INLINING,               "inlined"
#else
INLINING,               "not inlined"
#endif
END

5. mexFunction

Add a main C/C++ file that contains the body of “mexFunction” with the following code snippet ( do not forget to include the “Mex.h” header file to give the prototype of the mexFunction), of course, header files have to be put at the front of source file, such as

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <Mex.h> // Mex header
#include "matrix.h"
#include <ipp.h> // Intel IPP header
#ifdef MULTITHREADING_OMP
#include <omp.h> // OpenMP header
#endif

9

6. Turn on OpenMP

If your code is using the multi-threading functions and directives from OpenMP (supported by Visual Studio), you have to turn on options for compiler to recognize your implementation. Go to Project Properties-> C/C++ -> Language, change OpenMP Support to “Yes”.

10

7. Solution Platform

If you are compiling the Mex file under 64bit Windows system, one more step has to be taken. Go to the Project Property-> Configuration Manager. Change the Active solution platform to x64 instead of Win32.

11

Click <New> and choose x64 from drop menu.

12

13

8. Building

Now if you build the project (Right click / “Build”) you will get a mexw32 file (or mexw64) which can be executed in Matlab.

Note that at any time if a MEX function is running Matlab keeps a handle on the file where it is defined. Therefore if you want to rebuild your file you need to free the handle by using a simple “clear keepPos” command in Matlab prompt.

9. Conclusion

After all the steps, you should be able to compile your Mex file in Visual Studio as what I did. I tried this approach to compile source Mex C code called by Matlab GUI application, and it worked just fine. Also I managed to compile the same code using Matlab default Mex script, and just noticed sometimes you may need to change couple lines to satisfy different compiling. Also compatibility conflicts may arise when you compile Mex files using different tools. I would recommend to compile using default Maltab Mex script to avoid aforementioned problems for small project. That being said, take your own risk to compile this way and enjoy the power of Mex.

Also check “How to create a Mex project using Linux Eclipse” for compiling Mex in Linux.