Set Up C++ Development Environment - MATLAB & Simulink (original) (raw)
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 asMicrosoft® 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 inSolution Explorer and selectingProperties.
- Verify that Platform is set to
x64
on the project property page. - In the left pane of the project property page, underC/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 theDelay Loaded Dlls field:
- Include the appropriate header files in your C++ application code. All engine applications require this directive:
#include "MatlabEngine.hpp"
If you want to use the MATLAB Data API for C++, include this directive:
#include "MatlabDataArray.hpp"
If you want to use a strongly typed interface, include the header file created by the 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 clickNext.
- 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, clickFinish.
Visual Studio creates your project template as a ZIP file. If you chose to automatically import the template, then the template is available in theNew 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 themex
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_`matlabroot`_/extern/include
folder and are the same for Windows®, macOS, and Linux® systems. Include these header files in your C++ engine applications:
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_`matlabroot`_
with the path returned by the MATLABmatlabroot command.
Windows Libraries
In these path specifications, replace compiler
with either microsoft
or mingw64
.
- Engine library —
_`matlabroot`_\extern\lib\win64\_`compiler`_\libMatlabEngine.lib
- MATLAB 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.dylib
- MATLAB Data library —
_`matlabroot`_/extern/bin/_`macos`_/libMatlabDataArray.dylib
Linux Libraries
- Engine library —
_`matlabroot`_/extern/bin/glnxa64/libMatlabEngine.so
- MATLAB 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