Matlab loadlibrary C interface

Matlab loadlibrary is a straightforward way to use existing C libraries from Matlab. In contrast, Matlab C++ interface takes several steps to setup and distribute, and the C++ interfaces may not be necessary, considering one may be able to use an existing unmodified C library directly from Matlab. Other options include MEX C++ interface code, where the developer writes a wrapper function accessible from Matlab, this is relatively high performance and straightforward as well. However, loadlibrary can be the least amount of effort and the end user doesn’t need to compile C/C++ code.

Consider the modest example interfacing to the C++ <filesystem> library from Matlab. Compare that with a similar MEX <filesystem> interface. One doesn’t need to build a Matlab class like Ffilesystem.m for loadlibrary. The loadlibrary approach is more general and can be used with many C libraries having C types amenable to Matlab types.

Observe in Ffilesystem.m that the user provides the path to the shared library (.dll, .so, or .dylib) that could be distributed by GitHub Release or built by the user. Pointer in / out arguments are handled by libpointer. The “delete” destructor unloads the library to avoid resource leaks.