External Interfaces/API    

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. All MEX-files link against MATLAB only. MAT stand-alone programs link against libmx.dll (array access library) and libmat.dll (MAT-functions). Engine stand-alone programs link against libmx.dll (array access library) and libeng.dll for engine functions. 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.

Using the Add-In for Visual Studio

The MathWorks provides a MATLAB add-in for the Visual Studio® development system that lets you work easily within Microsoft Visual C/C++ (MSVC). The MATLAB add-in for Visual Studio greatly simplifies using M-files in the MSVC environment. The add-in automates the integration of M-files into Visual C++ projects. It is fully integrated with the MSVC environment.

The add-in for Visual Studio is automatically installed on your system when you run either mbuild -setup or mex -setup and select Microsoft Visual C/C++ version 5 or 6. However, there are several steps you must follow in order to use the add-in:

  1. To build MEX-files with the add-in for Visual Studio, run the following command at the MATLAB command prompt.
  1. Follow the menus and choose either Microsoft Visual C/C++ 5.0 or 6.0. This configures mex to use the selected Microsoft compiler and also installs the necessary add-in files in your Microsoft Visual C/C++ directories.

  1. To configure the MATLAB add-in for Visual Studio to work with Microsoft Visual C/C++:
    1. Select Tools -> Customize from the MSVC menu.
    2. Click on the Add-ins and Macro Files tab.
    3. Check MATLAB for Visual Studio on the Add-ins and Macro Files list and click Close. The floating MATLAB add-in for Visual Studio toolbar appears. The checkmark directs MSVC to automatically load the add-in when you start MSVC again.

Configuring on Windows 98 and Windows ME Systems

Windows 98.   To run the MATLAB add-in for Visual Studio on Windows 98 systems, add this line to your config.sys file.

Windows ME.   To run the MATLAB add-in for Visual Studio on Windows ME systems, do the following:

  1. Find C:\windows\system\conagent.exe in the Windows Explorer.
  2. Right click on the conagent.exe icon.
  3. Select Properties from the context menu. This brings up the CONAGENT.EXE Properties window.
  4. Select the Memory tab in the CONAGENT.EXE Properties window.
  5. Set the Initial Environment field to 4096.
  6. Click Apply.
  7. Click OK.

For additional information on the MATLAB add-in for Visual Studio:


  Custom Building on UNIX Troubleshooting