Main Content

Call Functions in C++ Compiled Library

The publisher of your MATLAB® interface to a C++ library provides you with instructions for installing the interface file and any dependent library files, if required. The publisher might give you dependent library files, ask you to install libraries from an external source, or provide a link to all relevant files. If the publisher created a toolbox using MATLAB Add-Ons, then this information might be found in the Getting Started Guide available through the Options menu for the toolbox in the Add-On Manager. If you need more information or cannot find a Getting Started Guide, then contact the publisher. For details about add-ons, see Manage Add-Ons.

The name of the interface file for library libname is libnameInterface.ext, where ext is platform-specific — .dll on Windows®, .so on Linux®, or .dylib on macOS.

Set Run-Time Path

MATLAB looks for the interface file on the MATLAB path and the dependent library files on the system path or run-time search path (rpath). If the publisher gives you dependent library files, you can put them in the same folder as the interface file. Alternatively, to add them to the system path, see Set Run-Time Library Path for C++ Interface. For information about locating dependent libraries, see Missing or Incorrectly Installed Run-Time Libraries.

Set MATLAB Path

Call addpath on the folder containing the interface file.

Display Help

The MATLAB help and doc functions provide help for members of the library. For example, to display help for function funcname in library libname, type:

help clib.libname.funcname

Call Function

To call a function funcname in C++ library libname with input arguments arg1,arg2,... and output argument retVal, use the MATLAB clib package. MATLAB automatically loads the library when you type:

retVal = clib.libname.funcname(arg1,arg2,...)

After MATLAB loads the library, you can use tab completion to view the members of the clib package.

Call Function with Default Arguments

If a C++ function is defined with default arguments, then you can call the function without providing one or more trailing arguments. The function help shows the default value. For example, if the type of arg is double and its default value is 100, then help displays:

clib.libname.funcname(arg)
    Input Arguments
      arg             double = 100

These statements produce the same result:

clib.libname.funcname
clib.libname.funcname(100)

This statement is also correct, although your result might be different:

clib.libname.funcname(99)

MATLAB supports default arguments for scalar integer and floating point types.

Related Topics