| External Interfaces/API | ![]() |
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:
COMPILER (e.g., Watcom), PATH, INCLUDE, and LIB environment variables. If your compiler always has the environment variables set (e.g., in AUTOEXEC.BAT), you can remark them out in the options file.
COMPILER environment variable, if needed.
COMPFLAGS environment variable.
exe is required.
-c switch (compile only; do not link) is recommended.
macro, with -D, MATLAB_MEX_FILE is required.
OPTIMFLAGS and DEBUGFLAGS. These are mutually exclusive: the OPTIMFLAGS are the default, and the DEBUGFLAGS are used if you set the -g switch on the mex command line.
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:
LINKER environment variable.
LINKFLAGS environment variable that must contain:
exe for stand-alone programs.
mexFunction for C or MEXFUNCTION@16 for DIGITAL Visual Fortran.
PRELINK_CMDS stage.
LINKEROPTIMFLAGS and LINKDEBUGFLAGS. As in the compile stage, these two are mutually exclusive: the default is optimization, and the -g switch invokes the debug switches.
LINK_FILE environment variable, if needed. For example, Watcom uses file to identify that the name following is a file and not a command.
LINK_LIB environment variable, if needed. For example, Watcom uses library to identify the name following is a library and not a command.
NAME_OUTPUT environment variable. The environment variable MEX_NAME contains the name of the first program in the command line. This must be set for -output to work. If this environment is not set, the compiler default is to use the name of the first program in the command line. Even if this is set, it can be overridden by specifying the mex -output switch.
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:
mexversion.rc into it.
.DEF file to export the MEX entry point. For example
LIBRARY MYFILE.DLL EXPORTS mexFunction <-- for a C MEX-file or EXPORTS _MEXFUNCTION@16 <-- for a Fortran MEX-file
.DEF file to the project.
.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.
libmx.lib, libmex.lib, and libmat.lib to the library modules in the LINK settings option.
include directory, MATLAB\EXTERN\INCLUDE to the include path in the Settings C/C++ Preprocessor option.
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.
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:
mex to use the selected Microsoft compiler and also installs the necessary add-in files in your Microsoft Visual C/C++ directories.
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:
C:\windows\system\conagent.exe in the Windows Explorer.
conagent.exe icon.
For additional information on the MATLAB add-in for Visual Studio:
MATLABAddin.hlp file in the <matlab>\bin\win32 directory, or
| Custom Building on UNIX | Troubleshooting | ![]() |