Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Compile COM Components from Command Line

A MATLAB® class cannot be directly compiled into a COM object. You can, however, use a user-generated class inside a MATLAB file and build a COM object from that file. You can use the MATLAB command-line interface instead of the Library Compiler app to create COM objects. Do this by issuing the mcc command with options. If you use mcc, you do not create a project.

The following table provides an overview of some mcc options related to components, along with syntax and examples of their usage.

Using the Command Line to Create COM Components

Action to Performmcc Option to UseDescription
Create component that has one class. -W com

The W option with com as the type controls the generation of wrapper files, which you can use to support components.

Syntax

mcc -W 'com:<component_name>[,<class_name>[,<major>.<minor>]]'

An unspecified <class_name> defaults to <component_name>, and an unspecified version number defaults to the latest version built or 1.0, if there is no previous version.

Example

mcc -W 'com:mycomponent,myclass,1.0' -T link:lib foo.m bar.m

The example creates a COM component called mycomponent, which contains a single COM class named myclass with methods foo and bar, and a version of 1.0.

Add additional classes to a COM component.

Not needed

A separate COM named <class_name> is created for each class argument that is passed.

Following the <class_name> parameter is a comma-separated list of source files that are encapsulated as methods for the class.

Syntax

class{<class_name>:[file, [file,...]]}

Example

mcc -B 'ccom:mycomponent,myclass,1.0' foo.m bar.m class{myclass2:foo2.m, bar2.m}

The example creates a COM component named mycomponent with two classes: myclass has methods foo and bar, and myclass2 has methods foo2 and bar2. The version is version 1.0.

Simplify the command-line input for components.-B ccom: Uses the bundle.

Syntax

mcc -B '<bundle>'[:<a1>,<a2>,...,<an>]

Example

mcc -B 'ccom:mycomponent,myclass,1.0' foo.m bar.m

Control how each COM class uses the MATLAB Runtime.-S

By default, a new MATLAB Runtime instance is created for each instance of each COM class in the component. Use -S to change the default.

This option tells the compiler to create a single MATLAB Runtime at the time when the first COM class is instantiated. This MATLAB Runtime is reused and shared among all subsequent class instances, resulting in more efficient memory usage and eliminating the MATLAB Runtime startup cost in each subsequent class instantiation.

When using -S, note that all class instances share a single MATLAB workspace and share global variables in the MATLAB files used to build the component. Therefore, properties of a COM class behave as static properties instead of instance-wise properties.

Note

The default behavior dictates that a new MATLAB Runtime be created for each instance of a class, so when the class is destroyed, the MATLAB Runtime is destroyed as well. If you want to retain the state of global variables (such as those allocated for drawing figures, for instance), use the -S option.

Example

mcc -S -B 'ccom:mycomponent,myclass,1.0' foo.m bar.m

The example creates a COM component called mycomponent containing a single COM class named myclass with methods foo and bar, and a version of 1.0.

When multiple instances of this class are instantiated in an application, only one MATLAB Runtime is initialized, and it is shared by each instance.

Create subfolders needed for deployment and copy associated files to them.-d The \src and \distrib subfolders are needed to package components.

Syntax

-d foldername

Was this topic helpful?