Main Content

Create a C Shared Library withMATLABCode

Supported platform:Windows®, Linux®,Mac

This example shows how to create a C shared library using a MATLAB®function. You can then pass the generated package to the developer who is responsible for integrating it into an application. The target system does not require a licensed copy of MATLAB.

Create Functions inMATLAB

In MATLAB, examine the MATLAB code that you want packaged. For this example, openaddmatrix.m,multiplymatrix.m, andeigmatrix.mlocated inmatlabroot\extern\examples\compilersdk\c_cpp\matrix.

addmatrix.m

At the MATLAB command prompt, enteraddmatrix([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

multiplymatrix.m

At the MATLAB command prompt, entermultiplymatrix([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

eigmatrix.m

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

The output is:

ans = 16.1168 -1.1168 -0.0000

Create a C Shared Library Using the Library Compiler App

  1. On theMATLAB Apps选项卡,最右边的Appssection, click the arrow. InApplication Deployment, clickLibrary Compiler. In theMATLAB Compilerproject window, clickC Shared Library.

    Alternately, you can open theLibrary Compilerapp by enteringlibraryCompilerat the MATLAB prompt.

  2. In theLibrary Compilerapp project window, specify the files of the MATLAB application that you want to deploy.

    1. In theExported Functionssection of the toolstrip, clickAdd exported function to the project.

    2. In theAdd Fileswindow, browse to the example folder, and select the function you want to package. ClickOpen.

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

    Add all three functions to the list of main files.

  3. In thePackaging Optionssection of the toolstrip, decide whether to include theMATLAB Runtimeinstaller in the generated application by selecting one of the options:

    • Runtime downloaded from web— Generate an installer that downloads theMATLAB Runtimeand 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 theMATLAB Runtimeinstaller. You can specify the file name of the installer.

      Note

      The first time you select this option, you are prompted to download theMATLAB Runtimeinstaller.

  4. In theLibrary Namefield, rename the packaged shared library aslibmatrix. The same name is followed through in the implementation of the shared library.

Customize the Application and Its Appearance

In theLibrary Compilerapp, 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. SeeCustomize the Installer.

  • Additional installer options— Default installation path for the generated installer and custom logo selection. See改变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. SeeManage Required Files in Compiler Project.

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

    SeeSpecify Files to Install with Application.

Package the Application

When you are finished selecting your packaging options, save yourLibrary Compilerproject and generate the packaged application.

  1. ClickPackage.

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

  2. In thePackagedialog box, verify thatOpen output folder when process completesis selected.

    包装过程完成时,检查the generated output in the target folder.

    • Three folders are generated:for_redistribution,for_redistribution_files_only, andfor_testing.

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

    • The log filePackagingLog.htmlcontains packaging results.

See Also

||

Related Topics