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. The target system does not require a licensed copy of MATLAB.

Create Functions inMATLAB

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

    For this example, Copy thematrixfolder that ships with MATLAB to your work folder.

    copyfile(fullfile(matlabroot,'extern','examples','compilersdk','c_cpp','matrix'),'matrix')

    Navigate to the newmatrixsubfolder in your work folder.

  2. Examine and test the functionsaddmatrix.m,multiplymatrix.m, andeigmatrix.m.

    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.

    When the packaging process is complete, examine 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.

Create C Shared Library Usingcompiler.build.cSharedLibrary

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, seeImplement C Shared Library in C Application.

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

    buildResults = compiler.build.cSharedLibrary(["addmatrix.m",..."eigmatrix.m","multiplymatrix.m"],...'LibraryName','libmatrix',...'Verbose','on');

    You can specify additional options in thecompiler.buildcommand by using name-value arguments. For details, seecompiler.build.cSharedLibrary.

    Thecompiler.build.ResultsobjectbuildResultscontains information on the build type, generated files, included support packages, and build options.

    函数生成以下文件a folder namedlibmatrixcSharedLibraryin 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.dylibonMacand.soon UNIX®.

    • 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, seeMATLAB Compiler Limitations.

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

    • requiredMCRProducts.txt— Text file that contains product IDs of products required byMATLAB Runtimeto run the application.

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

    Note

    The generated library does not includeMATLAB Runtimeor an installer. To create an installer using thebuildResultsobject, seecompiler.package.installer.

Implement C Shared Library in C Application

To implement your shared library using the provided C application code, seeImplement a C Shared Library with a Driver Application.

See Also

||

Related Topics