Main Content

Generate C++ Interface

R2026b

Configure and generate MATLAB interface to C/C++ library

Since R2023a

Description

The Generate C++ Interface task lets you configure and generate an interface definition object for a MATLAB® interface to a C/C++ library. This task accomplishes one step in the workflow to publish an interface. The Generate C++ Interface task automatically generates MATLAB code for your live script.

The task generates MATLAB code that you can reuse or modify as part of a programmatic interface definition workflow.

Using this task, you can, for example:

  • Specify the type of files in the C/C++ library.

  • Select a C/C++ compiler.

  • Name the interface library. For example, to call functions in library libname from MATLAB, use the clib.libname namespace.

  • Generate an interface definition object.

  • Display contents of the interface.

These configuration inputs can also be specified programmatically when generating interfaces.

Use Generate C++ Interface to generate an interface for your current platform. To generate interfaces for other platforms, use this task on the specific platform.

Open the Task

Use the Generate C++ Interface task as part of the workflow to publish a MATLAB interface for a C/C++ library. One approach to use this task is to call the clibPublishInterfaceWorkflow function, which incorporates this Live Editor task into the steps of the publish workflow. This task is typically used in combination with the steps to generate, build, and test the interface.

Parameters

expand all

Select files

Type of C/C++ files that compose your library. This selection determines which file inputs are required.

  • Header and compiled library files — Library is completely defined by:

  • Header-only — Library is completely defined by:

    • One or more header files

    • Header files in include path folders (optional)

  • Headers and source files — Library is completely defined by:

  • Custom file combination — Library is defined by any combination of:

    • Header files and/or one or more source files

    • Header files in include path folders (optional)

    • Compiled library files (optional)

    • Supporting source files (optional)

Root folder containing all C/C++ files for your library. When set, the task replaces this path value with the <startpath> tag in the selected file paths, making the configuration portable across platforms.

When publishing for multiple platforms, the <startpath> tag enables portability. Files tagged with <startpath> are automatically copied to new platform tabs. On each platform, set the library start path to the platform-specific root folder — you do not need to reselect individual files

Select configuration

The C/C++ compiler used to parse headers and build the interface. The selected compiler matches the output of:

mex -setup cpp

A supported C/C++ compiler is required. If your library includes a compiled library file, you must use the same compiler that built it. If your library is source-only (no compiled library file), any supported compiler works.

If the C++ compiler parameter is empty, then you must install a supported C/C++ compiler. For information on supported compilers, see Supported and Compatible Compilers. After installing a compiler, restart the Generate C++ Interface task.

To change the compiler, select one of the options from the C++ compiler list. This action changes your default compiler for all C/C++ applications in MATLAB.

Name used as the clib namespace for calling library functions from MATLAB. Must be a valid MATLAB identifier. For example, if the name is libname, call functions as:

clib.libname.myfunc

The task derives a default name from the first header file, but you can override it.

Specify optional C++ library settings

By default (off), the task parses .h files as C++ headers. Set to on to parse .h files as C headers. Use on when your library is defined by:

  • C header and library files.

  • C header and source files.

When on, the generated interface wraps C headers in extern "C" blocks, which prevents C++ name mangling when linking against C source files or a C library.

Preprocessor macros to define when parsing header files and compiling source files. Each macro has an identifier (characters 0–9, a–z, A–Z, '_' and must not begin with a digit) and an optional value.

Example: -D RELEASE

Example: -D LOGLEVEL = 2

Preprocessor macros to exclude while parsing header files and compiling source files. The macro identifier contains characters 0–9, a–z, A–Z, '_' and must not begin with a digit.

Example: -U DEBUG

Additional flags passed verbatim to the compiler during the generate and build steps. No validation is performed. You must ensure the flags are compatible with your selected compiler.

Example: -std=c++20

Additional flags passed verbatim to the linker during the build step. No validation is performed.

Example: /PDB:o.pdb

Specify optional definition configurations

  • Leave undefined—Object pointer shapes are undefined. Use the InterfaceDefinition object to programmatically complete the definition.

    Before R2026b: Define the SHAPE parameter manually in the definition file.

  • Set to scalar—All object pointers are treated as scalars.

  • Leave undefined—Size and MATLABType properties for const char * arguments are left unspecified. Use the InterfaceDefinition object to programmatically complete the definition.

    Before R2026b: Define the SHAPE and MLType parameters manually in the definition file.

  • Set to null-terminated C string—All const char * arguments are treated as null-terminated C strings, where Size is nullTerminated and MATLABType is a string.

  • C array—Arrays are returned as clib.array.* types, which provide direct access to the underlying C++ memory.

  • MATLAB array—Arrays are returned as native MATLAB arrays, which are copied from C++ memory but integrate naturally with MATLAB operations. Choose MATLAB array when interoperability with MATLAB functions is more important than avoiding data copies.

More About

expand all

Version History

Introduced in R2023a

expand all