mex — Build a native C, C++, or Fortran MEX module for RunMat.
mex(source, options...) compiles supported native translation units and publishes a RunMat-loadable module and deterministic artifact manifest. It is a build command with no returned value. It does not call the resulting gateway.
Syntax
mex(arguments)Inputs
arguments- Type
Any- Required
- Variadic
- Default
- —
- Description
- Native source files and MEX options as scalar text; RunMat also converts scalar numeric and logical arguments to text.
Errors
RunMat:MEX:UnsupportedTarget- When
- The current target cannot load native MEX modules.
- Message
MEX modules are unavailable on this target.
RunMat:MEX:InvalidBuildArguments- When
- The MEX command receives no source file or an invalid build option.
- Message
mex: invalid build arguments.
RunMat:MEX:BuildFailed- When
- The selected compiler cannot produce the requested MEX module.
- Message
mex: C MEX compilation failed.
RunMat:MEX:TooManyOutputs- When
- The caller requests more results than the MEX build or target query returns.
- Message
Too many MEX output arguments.
Sources and options
Pass at least one C, C++, or Fortran source path. Relative paths and output directories resolve from the current process working directory. -output and -outdir select the artifact; -I, -D, -L, -l, selected compiler assignments, and -v/-verbose/-silent are parsed by the shared native MEX builder. Unsupported assignments and conflicting ABI switches raise InvalidBuildArguments; compiler and filesystem failures retain their underlying build cause.
-R2017b and -R2018a select the compatibility API. -compatibleArrayDims and -largeArrayDims are additional supported API selectors, with builder-owned conflict rules and distinct dimension contracts. The selected ABI is recorded in the artifact manifest; RunMat checks the manifest before loading. The bundled mex.h and MEX SDK are prepared by the native build adapter, not by this builtin.
The current RunMat call boundary converts scalar numeric and logical arguments to text as well as accepting scalar strings and one-row character vectors. This is existing RunMat coercion, not a claim of MATLAB argument parity. Other values fail before invoking the compiler.
Execution and ownership
Compilation runs on a supported native target with a compatible C-family or Fortran toolchain and writable output directory. The command may read environment/toolchain configuration, source and SDK files, write modules/manifests, and print progress. It is not a GPU kernel, fusion candidate, or distributable partition operation. Browser/Worker execution cannot create a native MEX module.
Building a MEX module does not itself transfer or copy MATLAB array payloads. At invocation time the native MEX adapter validates its manifest, ABI and callback contract; zero-copy views remain owner-bound and copy-on-write mutation rules apply as described in the MEX execution guide. Build success alone does not prove callback isolation or later module execution.
The current compiler call blocks the caller and has no invocation cancellation or finite host-resource admission. A cancelled caller cannot assume an in-flight compiler was stopped; source, SDK, compiler, and output allocations are not governed by a complete-call memory budget.
Examples
Build a C gateway with the R2018a API
mex('-R2018a', '-output', 'gateway', 'gateway.c');
module = strcat('gateway.', mexext());
assert(isfile(module));Reject a missing source argument
mex();Using mex with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how mex changes the result.
Run a small mex example, explain the result, then change one input and compare the output.
FAQ
Does mex run the new module?⌄
No. It compiles and publishes the module and artifact manifest; calling the resulting function is a separate operation.
Which API switch should I use?⌄
Use -R2018a for interleaved-complex C Matrix API compatibility or -R2017b for the separate-complex compatibility path. The chosen API is pinned in the artifact manifest.
Can it compile on a browser Worker?⌄
No. The current builder and native module loader require a supported native target and toolchain.
Related Interop functions
Native
calllib · clibgen.buildInterface · libfunctions · libisloaded · libpointer · libstruct · loadlibrary · setdatatype · unloadlibrary
Java
isjava · javaaddpath · javaArray · javaclasspath · javaMethod · javaMethodEDT · javaObject · javaObjectEDT · javarmpath · jenv · usejava
Related guides
Source you can read
Every RunMat function's source is public. Read exactly how mex is executed, line by line, in Rust.
- View the source for mex in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.