External Interfaces Previous page   Next Page

Custom Building on Windows

There are three stages to MEX-file building for both C and Fortran on Windows - compiling, prelinking, and linking.

Compile Stage

For the compile stage, a mex options file must:

Prelink Stage

The prelink stage dynamically creates import libraries to import the required function into the MEX, MAT, or engine file:

MATLAB and each DLL have corresponding .def files of the same names located in the <matlab>\extern\include directory.

Link Stage

Finally, for the link stage, a mex options file must:

Linking DLLs to MEX-Files

To link a DLL to a MEX-file, list the DLL's .lib file on the command line.

Versioning MEX-Files

The mex script can build your MEX-file with a resource file that contains versioning and other essential information. The resource file is called mexversion.rc and resides in the extern\include directory. To support versioning, there are two new commands in the options files, RC_COMPILER and RC_LINKER, to provide the resource compiler and linker commands. It is assumed that:

Compiling MEX-Files with the Microsoft Visual C++ IDE

To build MEX-files with the Microsoft Visual C++ integrated development environment:

  1. Create a project and insert your MEX source and mexversion.rc into it.
  2. Create a .DEF file to export the MEX entry point. For example
  3. Add the .DEF file to the project.
  4. Locate the .LIB files for the compiler version you are using under matlabroot\extern\lib\win32\microsoft. For example, for version 6.0, these files are in the msvc60 subdirectory.
  5. From this directory, add libmx.lib, libmex.lib, and libmat.lib to the library modules in the LINK settings option.
  6. Add the MATLAB include directory, MATLAB\EXTERN\INCLUDE to the include path in the Settings C/C++ Preprocessor option.
  7. Add MATLAB_MEX_FILE to the C/C++ Preprocessor option by selecting Settings from the Build menu, selecting C/C++, and then typing ,MATLAB_MEX_FILE after the last entry in the Preprocessor definitions field.
  8. To debug the MEX-file using the IDE, put MATLAB.EXE in the Settings Debug option as the Executable for debug session.

If you are using a compiler other than the Microsoft Visual C/C++ compiler, the process for building MEX files is similar to that described above. In step 4, locate the .LIB files for the compiler you are using in a subdirectory of matlabroot\extern\lib\win32. For example, for version 5.4 of the Borland C/C++ compiler, look in matlabroot\extern\lib\win32\borland\bc54.


Previous page  Custom Building on UNIX Troubleshooting Next page

© 1994-2005 The MathWorks, Inc.