Main Content

Build C++ MEX Programs

Build your C++ MEX application using the MATLAB® mex to set up your environment and compile the C++ source code.

Supported Compilers

Use compilers that support C++11. For an up-to-date list of supported compilers, see the Supported and Compatible Compilers website.

Build .cpp File with mex Command

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++

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 matlabroot/extern/include folder and are the same for Windows®, Mac, and Linux® systems. C++ MEX files use these header files:

  • mex.hpp — Definitions for the C++ MEX API

  • mexAdapter.hpp — Utilities required by the C++ MEX function operator


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

PlatformBinary MEX File Extension





macOS with Apple silicon


macOS with Intel®



MEX files built on macOS with Intel are not supported on the Apple silicon platform using Rosetta 2.

See Also


Related Topics