Main Content

Create C/C++ Shared Libraries from Command Line

You can package C/C++ applications at the MATLAB® prompt or your system prompt using either of these commands.

  • deploytool invokes the Application Compiler app to execute a saved compiler project.

  • mcc invokes the MATLAB Compiler™ to create a deployable application at the command prompt.

Execute Compiler Projects with deploytool

The deploytool command has two flags that invoke one of the compiler apps to package an already existing project without opening a window.

  • -build project_name — Invoke the correct compiler app to build the project but not generate an installer.

  • -package project_name — Invoke the correct compiler app to build the project and generate an installer.

For example, deploytool -package magicsquare generates the binary files defined by the magicsquare project and packages them into an installer that you can distribute to others.

Package a Shared Library with mcc

The mcc command invokes MATLAB Compiler to create a deployable application at the command prompt and provides fine-level control while packaging the application. It does not package the results in an installer.

To invoke the compiler to generate a library, use the -l flag with mcc. The -l flag creates a C/C++ shared library that you can integrate into applications developed in C or C++.

Use the following mcc options to package a shared library.

OptionDescription
-W lib:libname -T link:lib

Generate a C shared library. Equivalent to using -l.

The -W lib:<libname> option tells the compiler to generate a function wrapper for a shared library and call it libname. The -T link:lib option specifies the target output as a shared library. Note the directory where the product puts the shared library because you will need it later on.

-W cpplib:libname -T link:lib

Generate a C++ shared library.

The -W lib:<libname> option tells the compiler to generate a function wrapper for a shared library and call it libname. The -T link:lib option specifies the target output as a shared library. Note the directory where the product puts the shared library because you will need it later on.

-a filePathAdd the file or files on the path to the generated binary.
-d outFolderSpecify the folder for the packaged applications.

Distribute the following files to integrate a C/C++ shared library in an application:

  • Function signatures of the deployed MATLAB functions

  • libraryName.lib/.dylib/.so — generated library

  • libraryName.h — generated header file

Differences Between Compiler Apps and Command Line

You perform the same functions using the compiler apps, a compiler.build function, or the mcc command-line interface. The interactive menus and dialog boxes used in the compiler apps build mcc commands that are customized to your specification. As such, your MATLAB code is processed the same way as if you were packaging it using mcc.

If you know the commands for the type of application you want to deploy and do not require an installer, it is faster to execute either compiler.build or mcc than go through the compiler app workflow.

Compiler app advantages include:

  • You can perform related deployment tasks with a single intuitive interface.

  • You can maintain related information in a convenient project file.

  • Your project state persists between sessions.

  • You can load previously stored compiler projects from a prepopulated menu.

  • You can package applications for distribution.

See Also

|

Related Topics