Documentation

Calling Shared Libraries in Deployed Applications

The loadlibrary function in MATLAB® allows you to load shared library into MATLAB.

Loading libraries using header files is not supported in compiled applications. Therefore, to create an application that uses the loadlibrary function with a header file, follow these steps:

  1. Create a prototype MATLAB file. Suppose that you call loadlibrary with the following syntax.

    loadlibrary(library, header)

    Run the following command in MATLAB only once to create the prototype file:

    loadlibrary(library, header, 'mfilename', 'mylibrarymfile'); 
    This creates mylibrarymfile.m in the current folder. If you are on Windows®, another file named library_thunk_pcwin64.dll is also created in the current folder.

  2. Change the call to loadlibrary in your MATLAB to the following:

    loadlibrary(library, @mylibrarymfile)

  3. Compile and deploy the application.

    • If you are integrating the library into a deployed application, specify the library’s .dll along with library_thunk_pcwin64.dll, if created, using the -a option of mcc command. If you are using Application Compiler or Library Compiler apps, add the .dll files to the Files required for your application to run section of the app.

    • If you are providing the library as an external file that is not integrated with the deployed application, place the library .dll file in the same folder as the compiled application. If you are on Windows, you must integrate library_thunk_pcwin64.dll into your compiled application.

      The benefit of this approach is that you can replace the library with an updated version without recompiling the deployed application. Replacing the library with a different version works only if the function signatures of the function in the library are not altered. This is because mylibrarymfile.m and library_thunk_pcwin64.dll are tied to the function signatures of the functions in the library.

Note

You cannot use loadlibrary inside MATLAB to load a shared library built with MATLAB. For more information on loadlibrary, see Limitations to Shared Library Support (MATLAB).

Note

Operating systems have a loadlibrary function, which loads specified Windows operating system module into the address space of the calling process.

See Also

Related Topics