Skip to content

mex

miscellaneous · scripts/miscellaneous/mex.m

: mex [options] file …
: mexBuildOutput = mex (…)
: mex ("-setup")
: mex ("-setup", compiler)

Compile C, CXX or Fortran source files into a MEX binary.

The arguments are the options of the compiler driver followed by the source files; all of them are character strings and are passed on to the nexus_mex driver (and from there into the underlying compiler / linker invocation). The output name uses mexext (.mexa64 / .mexw64), matching MATLAB R2026a. An error is raised when the driver reports a failure and no output argument is requested.

With an output argument, mex returns a mexBuildOutput struct with fields ExitCode and Messages (compiler/driver text). A non-zero ExitCode means the build failed; the call does not raise an error in that case.

mex ("-setup") lists the compilers found on PATH and writes a mexopts configuration under prefdir. mex ("-setup", "g++") selects that compiler in mexopts.

Options match MATLAB R2026a mex command syntax, including:

-v

Echo the compiler and linker commands (verbose).

-g

Keep debugging information (passed as -g).

-O

Optimise the build (passed as -O).

-Dname[=value]

Define a preprocessor macro for the compile step.

-Ipathname

Add pathname to the include search path.

-Lpathname

Add pathname to the library search path.

-lname

Link against library name.

-R2018a

Interleaved complex API (defines MX_HAS_INTERLEAVED_COMPLEX to 1).

-R2017b

Separate complex API (defines MX_HAS_INTERLEAVED_COMPLEX to 0).