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

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 platform-specific extensions. The following table lists the platform-specific extensions for MEX files.

MEX File Platform-Dependent Extension

PlatformBinary MEX File Extension

Linux (64-bit)

mexa64

Apple Mac (64-bit)

mexmaci64

Windows (64-bit)

mexw64

See Also

|

Related Topics