Main Content

Setting Up C++ Development Environment

To integrate MATLAB® functions within C++ applications, you need to set up your C++ development environment.

  • You can use the MATLAB desktop to create your deployable MATLAB functions, write C++ application code, and integrate the two. The MATLAB desktop environment can be used across platforms.

  • On Windows® systems, you can use Microsoft® Visual Studio® as your development environment.

Prerequisite

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 by executing:

    mbuild -setup
    In addition to configuring a C++ compiler, the mbuild utility compiles and links applications that integrate MATLAB Compiler SDK™ generated C++ shared libraries. Its options file specifies the compiler and linker settings used to build the application.

  2. Use the MATLAB Editor to author the C++ application code. Use the compiler.build.cppSharedLibrary function or the Library Compiler app to create deployable C++ shared libraries from MATLAB functions.

  3. Use the mbuild function to compile and link your C++ application code against MATLAB Compiler SDK C++ shared libraries.

    mbuild <cppApplicationSourceCode>.cpp -v                                            (MATLAB Data API)
    mbuild <cppApplicationSourceCode>.cpp <compilerSDKGeneratedLibrary>.lib -v          (mwArray API)

Set Up Microsoft Visual Studio for C++ Development (Windows Only)

  1. Create a new C++ project in Visual Studio. Select Console App if you are creating a C++ console application. Otherwise, pick the appropriate project type.

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

  3. Verify that the Platform is set to x64 in 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:

    If setting up with MATLAB:

    C:\Program Files\MATLAB\R2024a\extern\include

    If setting up with MATLAB Runtime:

    C:\Program Files\MATLAB\MATLAB Runtime\R2024a\extern\include

    If you are working with the mwArray API, you must also add the directory that the generated header file <compilerSDKGeneratedLibrary>.h is in.

  5. Under Linker > General, add the following directory to the Additional Library Directories field:

    If setting up with MATLAB:

    C:\Program Files\MATLAB\R2024a\extern\lib\win64\microsoft

    If setting up with MATLAB Runtime:

    C:\Program Files\MATLAB\MATLAB Runtime\R2024a\extern\lib\win64\microsoft

    If you are working with the mwArray API, you must also add the directory that the generated header file <compilerSDKGeneratedLibrary>.h is in.

  6. Under Linker > Input, add the following libraries to the Additional Dependencies field.

    • If you are using MATLAB Data API, add:

      delayimp.lib
      libMatlabCppSharedLib.lib
      libMatlabDataArray.lib
    • If you are using the mwArray API, add:

      <compilerSDKGeneratedLibrary>.lib
      mclmcrrt.lib

  7. If you are using the MATLAB Data API, under Linker > Input, add the following library to the Delay Loaded Dlls field:

    libMatlabDataArray.dll

  8. Include the header file in your C++ application code.

    • If you are using MATLAB Data API, add:

      #include "MatlabCppSharedLib.hpp"
    • If you are using the mwArray API, add:

      #include "<compilerSDKGeneratedLibrary>.h"

Export Set Up As Template in Visual Studio(Optional)

Once you've set up your project with all necessary configurations and dependencies, you can export it as a template to be reused in future. The steps to do this are:

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

  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 a .zip file which is your project template. This template is available in the New Project dialog under Visual C++ > My Templates, if you chose to automatically import the template.

Note that the template will include all project files and configurations, but it does not include external dependencies.

Setting Environment Variable in Visual Studio

Ensure you follow this procedure only after generating a MATLAB code archive (.ctf file). When running a C++ application that uses a MATLAB code archive, it's necessary for the application to identify the archive's location. You can make this location known by setting the CPPSHARED_BASE_CTF_PATH environment variable, directing it to the directory that stores the .ctf file.

In Visual Studio, follow these steps:

  • Open the Property Pages dialog by right-clicking on your project in the Solution Explorer and selecting Properties.

  • Select Debugging under Configuration Properties in the left-hand column.

  • Find the Environment field in the right-hand column and enter:

    CPPSHARED_BASE_CTF_PATH=<path to directory containing .ctf file>

  • Click Apply and then OK to close the dialog.

    These steps ensure that the environment variable is set every time you run or debug your application in Visual Studio.

Other Development Environments

In order to use other C++ development environments, you need to know which additional files and libraries to include during compilation. It is recommended that you first compile your C++ application using the mbuild function in MATLAB using the verbose mode. This will display all the files you need to include in other development environments.

Location of Relevant Files

Windows

MATLAB

  • C:\Program Files\MATLAB\R2024a\extern\include

  • C:\Program Files\MATLAB\R2024a\extern\lib\win64\<compiler>

MATLAB Runtime

  • C:\Program Files\MATLAB\MATLAB Runtime\R2024a\extern\include

  • C:\Program Files\MATLAB\MATLAB Runtime\R2024a\extern\lib\win64\<compiler>

Linux®

MATLAB

  • /usr/local/MATLAB/R2024a/extern/include

MATLAB Runtime

  • /usr/local/MATLAB/MATLAB_Runtime/R2024a/extern/include

macOS

MATLAB

  • /Applications/MATLAB_R2024a.app/extern/include

MATLAB Runtime

  • /Applications/MATLAB/MATLAB_Runtime/R2024a/extern/include

Testing Environment

You can test your C++ application against the installed version of MATLAB that was used to generate the C++ artifacts or against MATLAB Runtime. However, during deployment, you must use MATLAB Runtime.

See Also

Related Topics