Main Content

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)

  1. At the MATLAB command prompt, configure a C++ compiler for use with your C++ application.

    mex -setup -client engine C++

  2. Author your C++ application code in the MATLAB Editor.

  3. 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)

  1. 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.

  2. Access project properties by right-clicking the project node in Solution Explorer and selecting Properties.

  3. Verify that Platform is set to x64 on the project property page.

  4. 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
  5. Under Linker > General, add the following directory to the Additional Library Directories field:

    matlabroot\extern\lib\win64\microsoft
  6. Under Linker > Input, add the following libraries to the Additional Dependencies field:

    libMatlabEngine.lib;
    libMatlabDataArray.lib;
  7. 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

  8. 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:

  1. With your project open in Visual Studio, go to Project > Export Template from the main menu. The Export Template Wizard opens.

  2. Choose Project Template as the type of template to be created, and click Next.

  3. Choose the current project to export as a template, and click Next.

  4. 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 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 library

  • libMatlabDataArray — 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 MATLAB matlabroot 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

Related Topics