|On this page…|
You need a compiler and the mex function to build MEX-files. MATLAB® software supports many compilers and provides computer configuration files, called options files, designed specifically for these compilers. For an up-to-date list of supported compilers, see the Supported and Compatible Compilers Web page.
On 32-bit Microsoft® Windows® platforms, MATLAB provides a C compiler, Lcc. To view Help on using the Lcc compiler, type:
To help you configure your system using a sample MEX-file, see Overview of Building the timestwo MEX-File.
For information about creating MEX-files, see Creating a Source MEX-File.
If you have difficulty creating MEX-files, refer to Troubleshoot MEX-Files.
You can combine multiple source files, object files, and file libraries to build a binary MEX-file. To do this, list the additional files, with their file extensions, separated by spaces. The name of the MEX-file is the name of the first file in the list.
The following command combines multiple files of different types into a binary MEX-file called circle.ext, where ext is the extension corresponding to the current platform:
mex circle.c square.obj rectangle.c shapes.lib
For a Fortran files, type:
mex circle.F square.o rectangle.F shapes.o
You can use a software development tool like MAKE to manage MEX-file projects involving multiple source files. Create a MAKEFILE that contains a rule for producing object files from each of your source files, and then invoke the mex build script to combine your object files into a binary MEX-file. This method ensures that your source files are recompiled only when necessary.
MATLAB provides an example MEX-file, timestwo, for you to use to configure your system. This function takes a scalar input and doubles it.
The C source file is timestwo.c, and the Fortran source file is timestwo.F. These files are in matlabroot\extern\examples\refbook, where matlabroot is the MATLAB root folder, the value returned by the matlabroot command.
To work with these files, copy them to a local folder. For example:
cd('c:\work') copyfile([matlabroot '\extern\examples\refbook\timestwo.c']) copyfile([matlabroot '\extern\examples\refbook\timestwo.F'])
Use the mex function to build the binary MEX-file. If you are using a C/C++ compiler, type:
If you are using a Fortran compiler, type:
This command creates the file timestwo.ext, where ext is the value returned by the mexext function. You call timestwo as if it were a MATLAB function. For example, at the MATLAB command prompt, type:
ans = 8
For information about build options, see the mex script.
For information about the large-array-handling API, see Handling Large mxArrays.
Note: In a future version of MATLAB, the default mex function will change to use the large-array-handling API. This means the -largeArrayDims option will be the default and you must review your MEX-files, as described in Upgrade MEX-Files to Use 64-Bit API.