Set Up C++ Development Environment
To integrate MATLAB® functions into C++ applications, you need to set up your C++ development environment.
You can use the MATLAB desktop to create your MATLAB functions, write C++ application code, and integrate the two. The MATLAB desktop environment can be used across platforms.
You can also use an integrated development environment (IDE), such as Microsoft® Visual Studio® or Xcode.
Set Up MATLAB Desktop for C++ Development (Windows, Linux, and macOS)
At the MATLAB command prompt, configure a C++ compiler for use with your C++ application.
mex -setup -client engine C++
Author your C++ application code in the MATLAB Editor.
Compile and link your C++ application code using the
mex
function.mex -client engine cppApplicationSourceCode.cpp
Set Up Microsoft Visual Studio for C++ Development (Windows Only)
Create a new C++ project in Visual Studio. Select Console App if you want to create a C++ console application. Otherwise, select the appropriate project type.
Access project properties by right-clicking the project node in Solution Explorer and selecting Properties.
Verify that Platform is set to
x64
on the project property page.In the left pane of the project property page, under C/C++ > General, add the following directory to the Additional Include Directories field:
matlabroot\extern\include
Under Linker > General, add the following directory to the Additional Library Directories field:
matlabroot\extern\lib\win64\microsoft
Under Linker > Input, add the following libraries to the Additional Dependencies field:
libMatlabEngine.lib; libMatlabDataArray.lib;
If you want to use the MATLAB Data API for C++, under Linker > Input, add the following library to the Delay Loaded Dlls field:
libMatlabDataArray.dll
Include the appropriate header files in your C++ application code. All engine applications require this directive:
If you want to use the MATLAB Data API for C++, include this directive:#include "MatlabEngine.hpp"
If you want to use a strongly typed interface, include the header file created by the#include "MatlabDataArray.hpp"
matlab.engine.typedinterface.generateCPP
function.
Export Setup Template in Visual Studio (Optional)
Once you have set up your project with all necessary configurations and dependencies, you can export it as a template to be reused in the future. To export a template, follow these steps:
With your project open in Visual Studio, go to Project > Export Template from the main menu. The Export Template Wizard opens.
Choose Project Template as the type of template to be created, and click Next.
Choose the current project to export as a template, and click Next.
Provide a template name, description, icon, and preview image. You can also choose the template output location and decide whether to automatically import the template into Visual Studio. After filling in these details, click Finish.
Visual Studio creates your project template as a ZIP file. If you chose to automatically import the template, then the template is available in the New Project dialog box under Visual C++ > My Templates.
The template includes all project files and configurations, but it does not include external dependencies.
Other Development Environments
To use another IDE, such as Xcode, to write your source code, set up your environment for building C++
engine applications using the following libraries and include files. It is
recommended that you first compile your C++ application using the
mex
function in MATLAB using the verbose mode. This mode displays all the files you need to
include in other development environments.
Header files contain function declarations with prototypes for the functions that
you access in the API libraries. These header files are in the
folder and are the same for Windows®, macOS, and Linux® systems. Include these header files in your C++ engine
applications:matlabroot
/extern/include
MatlabEngine.hpp
— Definitions for the MATLAB Engine API for C++MatlabDataArray.hpp
— Definitions for a generic interface between C++ and MATLAB data
Link the corresponding libraries:
libMatlabEngine
— Engine librarylibMatlabDataArray
— MATLAB Data library
To link these libraries, specify their paths for your development environment
based on your platform. In the following path specifications, replace
with the path
returned by the MATLAB
matlabroot
matlabroot
command.
Windows Libraries
In these path specifications, replace compiler
with
either microsoft
or mingw64
.
Engine library —
matlabroot
\extern\lib\win64\compiler
\libMatlabEngine.libMATLAB Data library —
matlabroot
\extern\lib\win64\compiler
\libMatlabDataArray.lib
macOS Libraries
Replace macos
with either maca64
for macOS with Apple silicon or maci64
for macOS with Intel®.
Engine library —
matlabroot
/extern/bin/macos
/libMatlabEngine.dylibMATLAB Data library —
matlabroot
/extern/bin/macos
/libMatlabDataArray.dylib
Linux Libraries
Engine library —
matlabroot
/extern/bin/glnxa64/libMatlabEngine.soMATLAB Data library —
matlabroot
/extern/bin/glnxa64/libMatlabDataArray.so
Additional library — pthread
For example, to build myEngineApp.cpp
, use these
libraries.
g++ -std=c++11 -I matlabroot/extern/include/ -L matlabroot/extern/bin/glnxa64/
-pthread myEngineApp.cpp -lMatlabDataArray -lMatlabEngine