MEX functions built with the C Matrix API support all C++ language standards. This topic discusses specific C++ language issues to consider when creating and using MEX files. MATLAB® also provides an API that uses modern C++ semantics and design patterns, the MATLAB Data API. To use this API to build MEX functions, see C++ MEX Applications.
You can use the functions in the C Matrix API in your C++ applications. You can also
copy MATLAB C code examples into C++ applications. For example, see the
mexcpp.cpp file that contains both C and C++
The MATLAB C++ source code examples use the
extension. The extension
.cpp is unambiguous and recognized by
C++ compilers. Other possible extensions include
To build a C++ MEX file, type:
filename is the name of the source file.
You can run a C++ MEX file only on systems with the same version of MATLAB that the file was compiled on.
Do not use the
in a C++ destructor of a class used in a MEX-function. If the MEX-function
throws an error, MATLAB cleans up MEX-file variables, as described
in Automatic Cleanup of Temporary Arrays.
If an error occurs that causes the object to go out of scope, MATLAB calls the C++ destructor. Freeing memory directly in the destructor means both MATLAB and the destructor free the same memory, which can corrupt memory.
cout or the C-language
does not work as expected in C++ MEX files. Use the