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
Write MATLAB functions you want to deploy.
Use the
compiler.build.cppSharedLibrary
function or the Library Compiler app to create deployable C++ shared libraries that contain MATLAB functions.
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 by executing:
In addition to configuring a C++ compiler, thembuild -setup
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.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.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)
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.
Access project properties by right-clicking the project node in Solution Explorer and choosing Properties.
Verify that the Platform is set to
x64
in the project property page.From the left pane of the project property page, configure project properties.
Configuration Step If You Use MATLAB Data Array If 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:
Additionally, add the directory containing the generated header fileC:\Program Files\MATLAB\MATLAB Runtime\R2024b\extern\include
<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:
Additionally, add the directory containing the generated header fileC:\Program Files\MATLAB\MATLAB Runtime\R2024b\extern\lib\win64\microsoft
<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 DLLs Under Linker > Input, add the following library to the Delay Loaded Dlls field: libMatlabDataArray.dll
Not applicable.
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:
With your project open in Visual Studio, go to Project > Export Template... from the main menu. The Export Template Wizard will open.
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 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:
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:
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%
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
Download and install Visual Studio Code.
Download and install CMake (3.14 or higher recommended).
Install a C++ compiler.
Windows: MinGW or Microsoft C++ Build Tools.
Linux®: GCC
macOS: Xcode Command Line Tools.
For a list of supported compilers, see https://www.mathworks.com/support/requirements/supported-compilers.html.
Install Visual Studio Code Extensions.
C/C++ by Microsoft: This extension provides features like IntelliSense, debugging, and code browsing.
CMake Tools by Microsoft: This extension provides CMake integration and build support.
Configure Visual Studio Code
Open your terminal or command prompt.
Navigate to the location where you want to create your project and create a new directory.
mkdir MyCppMatlabProject cd MyCppMatlabProject
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 thecode
executable.You can create new source files directly from the terminal or inside Visual Studio Code.
Configure the IntelliSense engine to pick up MATLAB specific libraries by editing the
c_cpp_properties.json
file.To set the
includePath
setting in yourc_cpp_properties.json
file, add the paths to theincludePath
array. Format it as follows:Replace"includePath": [ "${workspaceFolder}/**", "C:\\Program Files\\MATLAB\\R2024b\\extern\\include", "path\\to\\v2\\generic_interface\\header\\file\\directory" ]
"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.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 yourlaunch.json
file, add an entry to theenvironment
array specifying the path to your archive (.ctf
file) directory. Format it as follows:Replace"environment": [ { "name": "CPPSHARED_BASE_CTF_PATH", "value": "path\\to\\v2\\generic_interface\\ctf\\file\\directory" } ]
"path\\to\\v2\\generic_interface\\ctf\\file\\directory"
with the actual directory path.
Configure CMake
Create a CMakeLists.txt
file in the project directory
with the following content:
Build
Press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.
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
MATLAB Runtime
|
Linux | MATLAB
MATLAB Runtime
|
macOS | MATLAB
MATLAB Runtime
|
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.