Main Content

Create a C Shared Library with MATLAB Code

Supported platform: Windows®, Linux®, Mac

This example shows how to create a C shared library using a MATLAB® function. The target system does not require a licensed copy of MATLAB.

Create Functions in MATLAB

  1. In MATLAB, examine the MATLAB code that you want packaged.

    For this example, copy the matrix folder that ships with MATLAB to your work folder.


    Navigate to the new matrix subfolder in your work folder.

  2. Examine and test the functions addmatrix.m, multiplymatrix.m, and eigmatrix.m.


    At the MATLAB command prompt, enter addmatrix([1 4 7; 2 5 8; 3 6 9], [1 4 7; 2 5 8; 3 6 9]).

    The output is:

     ans =
         2     8    14
         4    10    16
         6    12    18


    At the MATLAB command prompt, enter multiplymatrix([1 4 7; 2 5 8; 3 6 9], [1 4 7; 2 5 8; 3 6 9]).

    The output is:

     ans =
        30    66   102
        36    81   126
        42    96   150


    At the MATLAB command prompt, enter eigmatrix([1 4 7; 2 5 8; 3 6 9]).

    The output is:

     ans =

Create a C Shared Library Using the Library Compiler App

Build a C shared library using the Library Compiler app. Alternatively, if you want to create a C shared library from the MATLAB command window using a programmatic approach, see Create C Shared Library Using

  1. On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler. In the MATLAB Compiler project window, click C Shared Library.

    Library Compiler App with the C Shared Library type selected and addmatrix.m, eigmatrix.m, and multiplymatrix.m in the exported functions section

    Alternately, you can open the Library Compiler app by entering libraryCompiler at the MATLAB prompt.

  2. In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.

    1. In the Exported Functions section of the toolstrip, click Add exported function to the project.

    2. In the Add Files window, browse to the example folder, and select the function you want to package. Click Open.

    The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.

    Add addmatrix.m, multiplymatrix.m, and eigmatrix.m to the list of main files.

  3. In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options:

    • Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the file name of the installer.

    • Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer.


      The first time you select this option, you are prompted to download the MATLAB Runtime installer.

  4. In the Library Name field, rename the packaged shared library as libmatrix. The same name is followed through in the implementation of the shared library.

Customize the Application and Its Appearance

In the Library Compiler app, you can customize the installer, customize your application, and add more information about the application.

  • Library information — Information about the deployed application. You can also customize the appearance of the application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.

  • Additional installer options — Default installation path for the generated installer and custom logo selection. See Change the Installation Path.

  • Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.

  • Files installed for your end user — Files that are installed with your application.

    See Specify Files to Install with Application.

Library information section with the Library Name set to libmatrix

Package the Application

When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.

  1. Click Package.

    In the Save Project dialog box, specify the location to save the project.

  2. In the Package dialog box, verify that Open output folder when process completes is selected.

    When the packaging process is complete, examine the generated output in the target folder.

    • Three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing.

      For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.

    • The log file PackagingLog.html contains packaging results.

Create C Shared Library Using

As an alternative to the Library Compiler app, you can create a C shared library using a programmatic approach. If you have already created a library using the Library Compiler, see Implement C Shared Library in C Application.

  • Build the C shared library using the function. Use name-value arguments to specify the library name and enable verbose output.

    buildResults =["addmatrix.m", ...
    "eigmatrix.m","multiplymatrix.m"], ...
    'LibraryName','libmatrix', ...

    You can specify additional options in the command by using name-value arguments. For details, see

    The object buildResults contains information on the build type, generated files, included support packages, and build options.

    The function generates the following files within a folder named libmatrixcSharedLibrary in your current working directory:

    • GettingStarted.html — HTML file that contains information on integrating your shared library.

    • includedSupportPackages.txt — Text file that lists all support files included in the library.

    • libmatrix.c — C source code file.

    • libmatrix.def — Module-definition file that provides the linker with module information.

    • libmatrix.dll — Dynamic-link library file.

    • libmatrix.exports — Exports file that contains all nonstatic function names.

    • libmatrix.h — C header file.

    • libmatrix.lib — Import library file. The file extension is .dylib on Mac and .so on Linux.

    • mccExcludedFiles.log — Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see MATLAB Compiler Limitations.

    • readme.txt — Text file that contains packaging information.

    • requiredMCRProducts.txt — Text file that contains product IDs of products required by MATLAB Runtime to run the application.

    • unresolvedSymbols.txt — Text file that contains information on unresolved symbols.


    The generated library does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, see compiler.package.installer.

Implement C Shared Library in C Application

After packaging your C shared library, you can call it from a C application. The C application code calls the functions included in the shared library.

  1. Locate the matrix.c file located in matlabroot\extern\examples\compilersdk\c_cpp\matrix or your work folder.


    Copy and paste this file into the folder that contains your C library file (libmatrix.lib on Windows, libmatrix.dylib on Mac, on Linux). If you used the Library Compiler, this file is in the for_testing folder.

  2. At the MATLAB command prompt, navigate to the folder where you copied matrix.c.

  3. Use mbuild to compile and link the application.

    mbuild matrix.c libmatrix.<ext>
  4. To run the application on Linux or Mac systems, you must first add MATLAB Runtime to the library path. For details, see Set MATLAB Runtime Path for Deployment.

  5. From the system command prompt, run the application. On Windows, replace the forward slash with a backslash (\).

    The sum of the matrix with itself is: 
    2.00		8.00		14.00	 
    4.00		10.00		16.00	 
    6.00		12.00		18.00	 
    The product of the matrix with itself is: 
    30.00		66.00		102.00	 
    36.00		81.00		126.00	 
    42.00		96.00		150.00		 
    The eigenvalues of the original matrix are: 
    16.12		-1.12		-0.00


    You may need to give the application executable permissions on Linux or Mac systems.

    chmod u+x matrix

See Also

| | |

Related Topics