Supported platform: Windows®, Linux®, Mac
This example shows how to create a C++ shared library from a MATLAB® function. You can integrate the generated library into a C++ application. This example also shows how to call the C++ shared library from a C++ application. The target system does not require a licensed copy of MATLAB to run the application.
In MATLAB, examine the MATLAB code that you want packaged. For this example, open
addmatrix.m located in
At the MATLAB command prompt, enter:
addmatrix([1 4 7; 2 5 8; 3 6 9], [1 4 7; 2 5 8; 3 6 9])
The output is:
ans = 2 8 14 4 10 16 6 12 18
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler.
Alternatively, you can open the Library Compiler app from the MATLAB command prompt by entering:
In the Type section of the toolstrip, click C++ Shared Library.
In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.
In the Exported Functions section of the toolstrip, click .
In the Add Files window, browse to the example folder, and select the function you want to package. Click Open.
The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.
For this example, navigate to
In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options:
Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the filename of the installer.
Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the filename of the installer.
The first time you select this option, you are prompted to download the MATLAB Runtime installer.
The Library Name field is automatically
addmatrix as the name of the
packaged shared library. Rename it as
The same name is followed through in the implementation of the shared
Add MATLAB files to generate the sample C++ driver files. Although C++ driver files are not necessary to create shared libraries, they are used to demonstrate how to Implement the C++ MATLAB Data API Shared Library with a Driver Application.
In the Samples section, select Create
New Sample, and click
Edit the MATLAB file that
% Sample script to demonstrate execution of function a = addmatrix(a1, a2) a1 = [1 4 7; 2 5 8; 3 6 9]; % Initialize a1 here a2 = a1; % Initialize a2 here a = addmatrix(a1, a2);
Save the file, and return to the Library Compiler app. For more information and limitations, see Sample Driver File Creation.
Select the type of API for the generated C++ shared libraries. In the API selection section at the bottom, select Create interface that uses the MATLAB Data API. For more information, see API Selection for C++ Shared Library.
You can customize the installer, customize your application, and add more information about the application as follows:
Library information — Information about the deployed application. You can also customize the appearance of the application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.
Additional installer options — Default installation path for the generated installer and custom logo selection. See Change the Installation Path.
Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.
Files installed for your end user — Files that are installed with your application.
To generate the packaged application, click Package.
In the Save Project dialog box, specify the location to save the project.
In the Package dialog box, verify that Open output folder when process completes is selected.
When the packaging process is complete, examine the generated output.
Three folders are generated in the target folder location:
For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
PackagingLog.txt — Log file generated by
If you have already created a C++ shared library using the Library Compiler app, you can skip this section. However, if you want to know how to create a C++ shared library from the MATLAB command window using a programmatic approach, follow these instructions.
Save the path to the
addmatrix.m file located in
appFile = fullfile(matlabroot,'extern','examples','compilersdk','c_cpp','matrix','addmatrix.m');
Save the following code in a sample file named
a1 = [1 4 7; 2 5 8; 3 6 9]; a2 = a1; a = addmatrix(a1, a2);
Build the C++ shared library using the
compiler.build.cppSharedLibrary function. Use name-value
arguments to add a sample file and specify the library name.
buildResults = compiler.build.cppSharedLibrary(appFile,... 'LibraryName','libmatrix',... 'SampleGenerationFiles','addmatrixSample1.m');
buildResults contains information on the build
type, generated files, and build options.
This syntax generates the following files within a folder named
libmatrixcppSharedLibrary in your current working
samples\addmatrixSample1_mda.cpp — C++
sample driver file.
Component technology file that contains the deployable
v2\generic_interface\readme.txt — Text
file that contains packaging information.
GettingStarted.html — HTML file that
contains information on integrating your shared
mccExcludedFiles.log — Log file that
contains a list of any toolbox functions that were not
included in the application. For information on
non-supported functions, see MATLAB Compiler
readme.txt — Text file that contains
packaging and interface information.
requiredMCRProducts.txt — Text file that
contains product IDs of products required by MATLAB Runtime to run the application.
unresolvedSymbols.txt — Text file that
contains information on unresolved symbols.
The generated library does not include MATLAB Runtime or an installer.
Additional options can be specified in the
compiler.build command by using one or more of
the following name-value arguments.
'AdditionalFiles' — Path to additional files
to include in the shared library.
'AutoDetectDataFiles' — Flag to automatically
include data files.
'DebugBuild' — Flag to enable debug
'Interface'— Interface API, specified
'matlab-data' (default) or
'mwarray'. For more information, see
API Selection for C++ Shared Library.
'LibraryName'— Name of the generated
'OutputDir' — Path to the output
directory that contains generated files.
'SampleGenerationFiles'— MATLAB sample files used to generate sample C++ library
files. For more information, see Sample Driver File Creation.
'Verbose'— Flag to display progress
information indicating compiler output during the build
After packaging your C++ shared libraries, you can call them from a C++ application. The C++ application that you create uses the sample C++ driver code generated during packaging. The C++ driver code calls the C++ shared libraries, and it is based on the sample MATLAB file you selected in previous setup steps.
Copy and paste the generated C++ driver code file from the
samples folder into the
v2\generic_interface folder created when you
created the shared library.
Use the system command line to navigate to the
generic_interface folder where you copied the
sample C++ driver code file.
Compile and link the application using
mbuild at the
system command prompt.
Run the application from the system command prompt.
For information on how to retrieve a struct array, a cell array, or a character
vector from an
feval call, see the files