Main Content

How to Publish a MATLAB Interface to a C/C++ Library

R2026b

You can programmatically publish an interface from the MATLAB® command line or by using a guided workflow script. To get started, see Requirements for Building a MATLAB Interface to C/C++ Libraries.

Before R2026b: Your options to publish an interface are to call clibPublishInterfaceWorkflow to use a workflow script or to create a definition file for the library using clibgen.generateLibraryDefinition. For more information, see How to Publish a MATLAB Interface to a C/C++ Library (R2026a).

Publish Programmatically

Programmatic workflows let you define, inspect, modify, and build a C/C++ interface using MATLAB objects and repeatable MATLAB code instead of manually editing generated files. Create a clibgen.api.InterfaceConfiguration object to specify library file inputs and interface settings, then create a clibgen.api.InterfaceDefinition object to define the interface. You can serialize (save) and deserialize (load) these objects to reuse and modify interface definitions across sessions.

  1. Create an InterfaceConfiguration object that specifies what to include, where to find the headers, and how to standardize names. You also can specify build options.

    icfg = clibgen.api.InterfaceConfiguration("libname");
    icfg.HeaderFiles = "mylibrary.hpp";
    icfg.IncludePath = "include";
    
  2. Create an InterfaceDefinition object from the configuration. Use this object to define classes, functions, and enumerations. MATLAB parses the headers and creates the interface.

    idef = clibgen.api.InterfaceDefinition(icfg);
  3. Use properties of the InterfaceDefinition object to find incomplete functions and to define incomplete Size, MATLABType, and Direction properties. For an example, see Publish MATLAB Interface Programmatically. For details about defining and modifying interface elements, see Define MATLAB Interface for C/C++ Library.

  4. (Optional) Customize the generated help text for functions and classes. For more information, see Publish Help Text for MATLAB Interface to C/C++ Library.

  5. Build the MATLAB interface to the library. You can repeatedly build the interface to work across platforms and library versions, and the interface can be used in continuous integration systems.

    build(idef)
    addpath libname
    
  6. Test the interface.

  7. To share your interface with MATLAB users, see Distribute MATLAB Interface to C/C++ Library.

Publish with Workflow Script

As an alternative, call clibPublishInterfaceWorkflow to open the workflow script Publish MATLAB interface to C++ library. The script guides you through the configure, define, build, and test steps. For a complete workflow example, see Publish MATLAB Interface Using Live Script Workflow.

Follow these steps in the live script. Each step has a code section to run.

    • Step 1: GENERATE — Generate an interface definition object using the Generate C++ Interface Live Editor task.

    • Step 2: DEFINE — Use properties of the clibgen.api.InterfaceDefinition object to find incomplete functions and to define incomplete Size, MATLABType, and Direction properties. For more information, see Define Missing Direction Property, Define Missing Size Property, Define Missing MATLABType Property.

    • Optionally, you can customize the library help. For more information, see Publish Help Text for MATLAB Interface to C/C++ Library..

      • 2a: Display summary — Display the MATLAB signatures for each supported construct of the library.

    • Step 3: BUILD — Build the MATLAB interface to the C++ library file from the clibgen.api.InterfaceDefinition object. For more information, see Build C/C++ Library Interface and Review Contents.

    • Step 4: TEST

      • 4a: Set up and copy run-time libraries — Copy run-time library files to the interface library folder. If your library has additional run-time dependencies, specify them in this section. For more information, see Set Up and Copy Run-Time Libraries.

      • 4b: Enable out-of-process execution mode — Set up to call the interface library out-of-process, which prevents a MATLAB restart if you need to change the interface definition. For more information, see Load Out-of-Process C/C++ Library.

      • 4c: Call help on interface library — Display help for the interface library.

      • 4d: Write code to call and test interface library — Call your library using the syntax clib.libraryName followed by the library construct.

        • If you need to modify a MATLAB signature of a function or the contents of a class, iterate back to Step2: Define.

        • If you see a function or class or other construct missing in the interface, go back to Step2: Define to include it or see if its a unsupported construct.

        • If the construct is missing in the InterfaceDefinition object, go back to Step1 to see if you need to include a header which contains the specific symbol.

      • 4e: Unload out-of-process library — Unload the out-of-process MATLAB library. For more information, see Unload Out-of-Process C/C++ Library.

To share your interface with MATLAB users, see Distribute MATLAB Interface to C/C++ Library.

See Also

Functions

Namespaces

Objects

Topics