Main Content

Set Up C++ Development Environment

To set up a C++ development environment that integrates MATLAB® functions, users need several tools. First, MATLAB is required to write the functions. Next, MATLAB Compiler SDK™ is necessary to create deployable functions from these MATLAB functions. For integrating these deployable functions with C++ applications, users need a C++ development environment. Options include using MATLAB itself for cross-platform C++ development, Microsoft® Visual Studio® on Windows®, or Visual Studio Code and CMake for development across multiple platforms.

Prerequisites

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. From the left pane of the project property page, configure project properties.

    Configuration StepIf You Use MATLAB Data ArrayIf You Use mwArray
    Additional Include Directories

    Under C/C++ > General, add the following directory to the Additional Include Directories field:

    If setting up with MATLAB:

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

    If setting up with MATLAB Runtime:

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

    Under C/C++ > General, add the following directory to the Additional Include Directories field:

    If setting up with MATLAB:

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

    If setting up with MATLAB Runtime:

    C:\Program Files\MATLAB\MATLAB Runtime\R2024b\extern\include
    Additionally, add the directory containing the generated header file <compilerSDKGeneratedLibrary>.h.

    Additional Library Directories

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

    If setting up with MATLAB:

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

    If setting up with MATLAB Runtime:

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

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

    If setting up with MATLAB:

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

    If setting up with MATLAB Runtime:

    C:\Program Files\MATLAB\MATLAB Runtime\R2024b\extern\lib\win64\microsoft
    Additionally, add the directory containing the generated header file <compilerSDKGeneratedLibrary>.h.

    Additional Dependencies

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

    delayimp.lib
    libMatlabCppSharedLib.lib
    libMatlabDataArray.lib

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

    <compilerSDKGeneratedLibrary>.lib
    mclmcrrt.lib

    Delay Loaded DLLsUnder Linker > Input, add the following library to the Delay Loaded Dlls field:
    libMatlabDataArray.dll

    Not applicable.

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

Set Environment Variables in Visual Studio

Set CPPSHARED_BASE_CTF_PATH Environment Variable

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

Set PATH Environment Variable

To ensure that your application can find the necessary MATLAB or MATLAB Runtime libraries, you need to set the PATH environment variable in Visual Studio. This is essential if you want to run your application from within Visual Studio for debugging purposes.

In Visual Studio, follow these steps:

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

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

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

    For MATLAB:

    PATH=C:\Program Files\MATLAB\R2024b\runtime\win64;%PATH%

    For MATLAB Runtime:

    PATH=C:\Program Files\MATLAB\MATLAB Runtime\R2024b\runtime\win64;%PATH%

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

Set Up Visual Studio Code and CMake for C++ Development (Windows, Linux, and macOS)

Prerequisites

Configure Visual Studio Code

  1. Open your terminal or command prompt.

  2. Navigate to the location where you want to create your project and create a new directory.

    mkdir MyCppMatlabProject
    cd MyCppMatlabProject
  3. Launch Visual Studio Code with the current directory opened.

    code .

    If you receive an error saying code is not recognized, you may need to add Visual Studio Code to your system PATH or use the full path to the code executable.

  4. You can create new source files directly from the terminal or inside Visual Studio Code.

  5. Configure the IntelliSense engine to pick up MATLAB specific libraries by editing the c_cpp_properties.json file.

    To set the includePath setting in your c_cpp_properties.json file, add the paths to the includePath array. Format it as follows:

    "includePath": [
        "${workspaceFolder}/**",
        "C:\\Program Files\\MATLAB\\R2024b\\extern\\include",
        "path\\to\\v2\\generic_interface\\header\\file\\directory"
    ]
    
    Replace "path\\to\\v2\\generic_interface\\header\\file\\directory" with the actual directory path to your header files. This ensures the compiler can locate the necessary header files.

     c_cpp_properties.json

  6. Configure the debugger settings by editing the launch.json file and specify the path to the archive file (.ctf file).

    To set the CPPSHARED_BASE_CTF_PATH environment variable in your launch.json file, add an entry to the environment array specifying the path to your archive (.ctf file) directory. Format it as follows:

     "environment": [
                    {
                        "name": "CPPSHARED_BASE_CTF_PATH",
                        "value": "path\\to\\v2\\generic_interface\\ctf\\file\\directory"
                    }
                ]
    Replace "path\\to\\v2\\generic_interface\\ctf\\file\\directory" with the actual directory path.

     launch.json

Configure CMake

Create a CMakeLists.txt file in the project directory with the following content:

 CMakeLists.txt

Build

  1. Press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.

  2. Type CMake: Build and select it from the list.

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\R2024b\extern\include

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

MATLAB Runtime

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

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

Linux

MATLAB

  • /usr/local/MATLAB/R2024b/extern/include

MATLAB Runtime

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

macOS

MATLAB

  • /Applications/MATLAB_R2024b.app/extern/include

MATLAB Runtime

  • /Applications/MATLAB/MATLAB_Runtime/R2024b/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