Build C++ MEX Programs
Build your C++ MEX application using the MATLAB®
mex to set up your environment and
compile the C++ source code.
Select Supported Compiler
Use compilers that support C++11. For an up-to-date list of supported compilers, see the Supported and Compatible Compilers website.
If you have installed one of the supported compilers, set up the compiler for C++
MEX applications using the mex command. When provided with an
option to select a compiler, select an installed compiler that the MATLAB C++ MEX supports.
mex -setup C++
To set up the complier to build C++ applications that call MATLAB, see Requirements to Build C++ Engine Applications.
Build .cpp File with mex Command
Build your C++ MEX program using the MATLAB
mex command.
mex MyMEXCode.cpp MEX Include Files
Include the following header files in your C++ source code. Header files contain
function declarations with prototypes for the routines that you access in the API
libraries. These files are in the
folder
and are the same for Windows®, Mac, and Linux® systems. C++ MEX files use these header files:matlabroot/extern/include
mex.hpp— Definitions for the C++ MEX APImexAdapter.hpp— Utilities required by the C++ MEX function operator
Note
Include mexAdapter.hpp only once with the
MexFunction class definition in MEX applications that
span multiple files.
File Extensions
MEX files are platform-specific. MATLAB identifies MEX files by these platform-specific file extensions.
MEX File Platform-Dependent Extension
| Platform | Binary MEX File Extension |
|---|---|
Windows |
|
Linux |
|
macOS with Apple silicon |
|
See Also
mex | matlab::engine::MATLABEngine