Example of how to build a Matlab mex file using the Odin programming language.
Tested in Matlab r2025b on Windows (11) & Linux Mint (22.1), but I think it'll work with Matlab versions starting from 2018 when the interleaved complex array api was introduced.
addemup.odin contains the required mexFunction() entry point (it just adds 2
identically-sized double real arrays & returns the result), the other .odin files
define the Matlab library interfaces & were generated using
odin-c-bindgen & very
slightly tweaked (turns out the functions are all defined in libmx, although they're
declared in the matrix.h header).
Run the bindit.m Matlab script to build the mex file, it:
-
Generates a file called mex_version.odin containing the required
mexfilerequiredapiversion()function. The API version is hard-coded as 800 (the aforementioned version with interleaved complex arrays), the Matlab version is extracted from theversion()function. This function is normally defined in MATLAB/{version}/extern/version/c_mexapi_version.c -
Invokes
odin build .(with some additional arguments) to compile the mex file.