Supported platform: Windows®
This example shows how to create a generic COM component using a MATLAB® function and integrate it into an application. The target system does not require a licensed copy of MATLAB.
Verify that you have the Windows 10 SDK kit installed. For details, see Windows 10 SDK.
Verify that you have MinGW-w64 installed. To install it from the MathWorks File Exchange, see MATLAB Support for MinGW-w64 C/C++ Compiler.
To ensure that MATLAB detects the Windows 10 SDK kit and MinGW-w64, use the following command:
mbuild -setup -client mbuild_com
Verify that you have Microsoft® Visual Studio® installed.
End users must have an installation of MATLAB Runtime to run the application. For details, see Install and Configure MATLAB Runtime.
For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.
In MATLAB, examine the MATLAB code that you want packaged. For this example, open
makesquare.m located in
function y = makesquare(x) y = magic(x);
At the MATLAB command prompt, enter
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
Package the function into a COM component using the Library Compiler app. Alternatively, if you want to create a COM component from the MATLAB command window using a programmatic approach, see .
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler. In the MATLAB Compiler project window, click Generic COM Component.
Alternately, you can open the Library Compiler app by
libraryCompiler at the MATLAB prompt.
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.
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 file name of the installer.
Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer.
The first time you select this option, you are prompted to download the MATLAB Runtime installer.
In the Library Name field, replace
Verify that the function defined in
makesquare.m is mapped
In the Library Compiler app, you can customize the installer, customize your application, and add more information about the application.
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.
Additional runtime settings — Platform-specific options for controlling the generated executable. See Additional Runtime Settings.
When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.
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 in the target folder.
Three folders are generated:
For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
The log file
PackagingLog.html contains packaging
As an alternative to the Library Compiler app, you can create a COM component using a programmatic approach using the following steps. If you have already created a component using the Library Compiler, see Integrate into COM Application.
Save the path to the file
makesquare.m located in
For example, if you are using Visual Studio version 15, type:
appFile = fullfile(matlabroot,'toolbox','dotnetbuilder','Examples',... 'VS15','COM','MagicSquareExample','MagicSquareComp','makesquare.m');
Build the COM component using the
function. Use name-value arguments to specify the component name and class
buildResults = compiler.build.comComponent(appFile,... 'ComponentName','MagicSquareComp',... 'ClassName','Class1');
buildResults contains information on the build type,
generated files, and build options.
The function generates the following files within a folder named
MagicSquareCompcomComponent in your current working
Class1_com.cpp — C++ source code file that
defines the class.
Class1_com.hpp — C++ header file that defines
dlldata.c — C source code file that contains
entry points and data structures required by the class factory for
GettingStarted.html — HTML file that contains
steps on installing COM components.
includedSupportPackages.txt — Text file that
contains information on included support packages.
MagicSquareComp.def — Module definition file
that defines which functions to include in the DLL export
MagicSquareComp.rc — Resource script file that
describes the resources used by the component.
MagicSquareComp_1_0.dll — Dynamic-link library
MagicSquareComp_dll.cpp — C++ source code file
that contains helper functions.
MagicSquareComp_idl.h — C++ header file.
MagicSquareComp_idl.idl — Interface definition
MagicSquareComp_idl.tlb — Type library file
that contains information about the COM object properties and
MagicSquareComp_idl_i.c — C source code file
that contains the IIDs and CLSIDs for the IDL interface.
MagicSquareComp_idl_p.c — C source code file
that contains the proxy stub code for the IDL interface.
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
mwcomtypes.h — C++ header file that contains
the definitions for the interfaces.
mwcomtypes_i.c — C source code file that
contains the IIDs and CLSIDs.
mwcomtypes_p.c — C source code file that
contains the proxy stub code.
readme.txt — Text file that contains deployment
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 assembly does not include MATLAB Runtime or an installer. To create an installer using the
buildResults object, see
You can specify additional options in the
command by using one or more of the following name-value arguments:
'AdditionalFiles' — Path to additional files to
include in the component.
'AutoDetectDataFiles' — Flag to automatically
include data files.
'ClassName' — Name of the COM class.
'ComponentName'— Name of the generated
'ComponentVersion'— Version of the generated
'EmbedArchive'— Flag to embed the assembly
'OutputDir' — Path to the output directory
that contains generated files.
'Verbose'— Flag to display progress
information indicating compiler output during the build process.
To integrate your COM component into an application, see Creating the Microsoft Visual Basic Project.