MATLAB Libraries Using loadlibrary

    Note:   It is important to understand the difference between the following:

    • MATLAB loadlibrary function — Loads shared library into MATLAB®.

    • Operating system loadlibrary function — Loads specified Windows® operating system module into the address space of the calling process.

With MATLAB Compiler™ version 4.0 (R14) and later or MATLAB Compiler SDK™, you can use MATLAB file prototypes as described below to load your library in a compiled application. Loading libraries using H-file headers is not supported in compiled applications. This behavior occurs when loadlibrary is compiled with the header argument as in the statement:

loadlibrary(library, header)

In order to work around this issue, execute the following at the MATLAB command prompt:

loadlibrary(library, header, 'mfilename', 'mylibrarymfile'); 
where mylibrarymfile is the name of a MATLAB file you would like to use when loading this library. This step only needs to be performed once to generate a MATLAB file for the library.

In the code that is to be compiled, you can now call loadlibrary with the following syntax:

loadlibrary(library, @mylibrarymfile, 'alias', alias)

It is only required to add the prototype .m file and .dll file to the deployable archive of the deployed application. There is no need for .h files and C/C++ compilers to be installed on the deployment machine if the prototype file is used.

Once the prototype file is generated, add the file to the deployable archive of the application being compiled. You can do this with the -a option (if using the mcc command) or by dragging it under Other/Additional Files (as a helper file) if using one of the compiler apps.

With MATLAB Compiler versions 4.0.1 (R14+) and later or MATLAB Compiler SDK, generated MATLAB files will automatically be included in the deployable archive as part of the compilation process. For MATLAB Compiler versions 4.0 (R14), include your library MATLAB file in the compilation with the -a option with mcc.

Restrictions on Using MATLAB Function loadlibrary with MATLAB Compiler

You can not use loadlibrary inside of MATLAB to load a shared library built with MATLAB. For complete documentation and up to date restrictions on loadlibrary, please reference the MATLAB documentation.

Was this topic helpful?