MATLAB Function Reference Previous page   Next Page
mex

Compile MEX-function from C or Fortran source code

Syntax

Description

mex options filenames compiles a MEX-function from the C, C++, or Fortran source code files specified in filenames. Include both file name and file extension in each filename. All nonsource code filenames passed as arguments are passed to the linker without being compiled.

All valid options are shown in the MEX Script Switches table. These options are available on all platforms except where noted.

MEX's execution is affected both by command-line options and by an options file. The options file contains all compiler-specific information necessary to create a MEX-function. The default name for this options file, if none is specified with the -f option, is mexopts.bat (Windows) and mexopts.sh (UNIX).

On UNIX, the options file is written in the Bourne shell script language. The mex script searches for the first occurrence of the options file called mexopts.sh in the following list:

mex uses the first occurrence of the options file it finds. If no options file is found, mex displays an error message. You can directly specify the name of the options file using the -f switch.

Any variable specified in the options file can be overridden at the command line by use of the <name>=<def> command-line argument. If <def> has spaces in it, then it should be wrapped in single quotes (e.g., OPTFLAGS='opt1 opt2'). The definition can rely on other variables defined in the options file; in this case the variable referenced should have a prefixed $ (e.g., OPTFLAGS='$OPTFLAGS opt2').

On Windows, the options file is written in the Perl script language. The default options file is placed in your user profile directory after you configure your system by running mex -setup. The mex script searches for the first occurrence of the options file called mexopts.bat in the following list:

mex uses the first occurrence of the options file it finds. If no options file is found, mex searches your machine for a supported C compiler and uses the factory default options file for that compiler. If multiple compilers are found, you are prompted to select one.

No arguments can have an embedded equal sign (=); thus, -DFOO is valid, but -DFOO=BAR is not.

Remarks

mex compiles and links source files into a shared library, called a MEX-file, that is executable from within MATLAB. You can create MEX-files from a common source file on different platforms. The resulting file is given the platform-dependent file extension shown in the table below:

System Type
MEX File Extension
Sun Solaris
.mexsol
HP-UX
.mexhpux
Linux on PC
.mexglx
Linux on AMD Opteron
.mexa64
Linux on Intel Itanium2
.mexi64
MacIntosh
.mexmac
Windows
.dll

See Also

dbmex, mexext, inmem


Previous page  methodsview mexext Next page

© 1994-2005 The MathWorks, Inc.