Main Content

Call Functions in C Library Loaded with loadlibrary

A shared library is a collection of functions dynamically loaded by an application at run time. This interface for MATLAB® R2021b and earlier supports libraries containing functions defined in C header files. To call functions in either C or C++ libraries, see the interface described in Call C++ from MATLAB.

MATLAB supports dynamic linking on all supported platforms.

PlatformShared LibraryFile Extension

Microsoft® Windows®

dynamic link library file.dll


shared object

Apple macOS

dynamic shared library.dylib

A shared library needs a header file, which provides signatures for the functions in the library. A function signature, or prototype, establishes the name of the function and the number and types of its parameters. Specify the full path of the shared library and its header file.

You need an installed MATLAB-supported C compiler. For an up-to-date list of supported compilers, see Supported and Compatible Compilers.

MATLAB accesses C routines built into external, shared libraries through a command-line interface. This interface lets you load an external library into MATLAB memory and access functions in the library. Although types differ between the two language environments, usually you can pass types to the C functions without converting. MATLAB converts for you.

Details about using a shared library are in these topics.

If the library function passes arguments, you need to determine the data type passed to and from the function. For information about data, see these topics.

When you are finished working with the shared library, it is important to unload the library to free memory.

See Also

| |

Related Topics