Create COM Component Using COM Component Compiler App
Supported platforms: Windows®, Linux®, Mac
This example shows how to use the COM Component Compiler app to package a MATLAB® function into a COM component. You can call the function using a COM application.
Before R2025a: Create a COM component using the Library Compiler as shown in Integrate Magic Square into a COM Application (R2024b).
Prerequisites
Verify that you have the Windows 10 or later SDK kit installed. For details, see Windows SDK.
Verify that you have MinGW-w64 installed. To install it from the MathWorks® File Exchange, see MATLAB Support for MinGW-w64 C/C++/Fortran Compiler.
To ensure that MATLAB detects the Windows 10 SDK kit and MinGW-w64, use this 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 Download and Install MATLAB Runtime.
For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.
Create MATLAB Function
In MATLAB, write MATLAB program that you want to deploy.
For this example, write a function magicsquare.m
as follows.
function y = magicsquare(x)
y = magic(x);
At the MATLAB command prompt, enter magicsquare(5)
.
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
Create Project and Compiler Task
Create a compiler task for your function using the COM Component Compiler. Compiler tasks allow you to compile files in a project for a specific deployment target.
To open the app, on the Apps tab, expand the Apps gallery. In the Application Deployment section, click COM Component Compiler.
You can also open the app using the comComponentCompiler
function
at the MATLAB Command Window.
After you open the app, the Create Compiler Task dialog box prompts
you to add a compiler task to a new or an existing MATLAB project. For this example, select Start a new project and create a compiler task and create a new project named MakesquareProject
.
For more information on creating and using MATLAB projects, see Create Projects.
A new compiler task named COMComponent1
opens in the Editor. You can
compile code for other deployment targets by opening the Compiler Task Manager
or going to the Manage Tasks tab and creating a new compiler task.
Specify Build Options
You can specify options for the COM component before packaging to customize the building and packaging process. For instance, you can obfuscate the MATLAB code.
For this example, in the Exported Functions section of the compiler
task, click Add Exported Function and select
magicsquare.m
. In the Project panel, the file now has the labels
Design
and Exported Function File
.
In the Package Info section, replace the string
MyCOMComponent
with the name for your COM component,
MagicSquareComp
.
View Code and Package COM Component
From the Deployment tab, you can show the deployment script code,
open deployment settings, and package your component. To view code that contains
instructions on building and packaging your component, click the Show
Code button. On the right, a window displays a deployment script with the
compiler.build.comComponent
and compiler.package.installer
functions that corresponds to your build options.
You can convert this code to a MATLAB script file by clicking the Export Build Script button.
Running the generated build script is equivalent to clicking the
Package button.
To create the COM component, click Package.
The compiler generates files in the
folder in your project
folder. To choose a different output location for the generated files, update the paths in
the Output Locations section.<compiler_task_name>
/output
Integrate into COM Application
To integrate your COM component into an application, see Creating the Microsoft Visual Basic Project.