Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

C/C++ Shared Library Integration

Integrate compiled MATLAB® functions into C or C++ applications

MATLAB Compiler SDK™ enables you to create C/C++ shared libraries from MATLAB functions. You have three options to create shared libraries:

  • Create C shared libraries that use the mxArray API.

  • Create C++ shared libraries that use the mwArray API.

  • Create C++ shared libraries that use the MATLAB Data API.

Shared libraries that use the mwArray API have an interface that uses C-style functions to initialize the MATLAB Runtime, load the compiled MATLAB functions into the MATLAB Runtime, and manage data that is passed between the C++ code and the MATLAB Runtime. The interface supports only C++03 functionality. For an example, see Integrate a C++ mwArray API Shared Library into an Application.

Shared libraries that use the MATLAB Data API have a generic interface that uses modern C++ semantics. The interface supports C++11 functionality and has several advantages over the mwArray API including type-safety and multithread-safety. The interface provides functions that ensure correct initialization and termination of applications. It can be run either in-process or out-of-process and can call functions asynchronously. For more information, see Workflow to Integrate with a C++ Shared Library that Uses the MATLAB Data API. For an example, see Integrate a C++ MATLAB Data API Shared Library into an Application.

Note

The MATLAB Data API was introduced in R2017b.

Functions

mbuildCompile and link source files against MATLAB generated shared libraries

C/C++ API

expand all

mclmcrInitializeInitializes the MATLAB Runtime proxy library
mclInitializeApplicationSet up application state shared by all MATLAB Runtime instances created in current process
mclTerminateApplicationClose MATLAB Runtime-internal application state
<library>Initialize[WithHandlers]Initialize MATLAB Runtime instance associated with library
<library>TerminateFree all resources allocated by MATLAB Runtime instance associated with library
mclRunMainMechanism for creating identical wrapper code across all platforms
mclIsMCRInitializedDetermine if MATLAB Runtime has been properly initialized
mclWaitForFiguresToDieEnable deployed applications to process graphics events, enabling figure windows to remain displayed
mclGetLastErrorMessageLast error message from unsuccessful function call
mclGetLogFileNameRetrieve name of log file used by MATLAB Runtime
mclIsJVMEnabledDetermine if MATLAB Runtime was launched with instance of Java Virtual Machine (JVM)
mclIsNoDisplaySetDetermine if -nodisplay mode is enabled
mwArrayClass used to pass input/output arguments to C++ functions generated by MATLAB Compiler SDK
mwExceptionException type used by the mwArray API and the C++ interface functions
mwStringString class used by the mwArray API to pass string data as output from certain methods
matlab::cpplib::initMATLABApplicationStart the MATLAB Runtime and initialize its application state
matlab::cpplib::runMainExecute a function with its input arguments within the main function
matlab::cpplib::convertUTF8StringToUTF16StringConvert UTF-8 string to UTF-16 string
matlab::cpplib::convertUTF16StringToUTF8StringConvert UTF-16 string to UTF-8 string
matlab::cpplib::initMATLABLibraryInitialize a library of MATLAB functions packaged in a deployable archive file
matlab::cpplib::initMATLABLibraryAsyncInitialize a library of MATLAB function asynchronously
matlab::cpplib::MATLABLibrary::fevalExecute a MATLAB function from a deployable archive
matlab::cpplib::MATLABLibrary::fevalAsyncExecute a MATLAB function from a deployable archive asynchronously
matlab::cpplib::MATLABLibrary::waitForFiguresToCloseWait for all figures to close

Examples and How To

Integrate a C Shared Library into an Application

Example on how to call a C shared library built with MATLAB Compiler SDK from a C application.

Integrate a C++ mwArray API Shared Library into an Application

Example on how to call a C++ mwArray API shared library built with MATLAB Compiler SDK from a C++ application.

Integrate a C++ MATLAB Data API Shared Library into an Application

Example on how to call a C++ MATLAB Data API shared library built with MATLAB Compiler SDK from a C++ application.

Workflow to Integrate with a C++ Shared Library that Uses the MATLAB Data API

Workflow to integrate with a C++ shared library that uses the MATLAB Data API.

Writing C++ Driver Code using the Generic Interface

Guidelines on writing C++ driver code using the generic interface for integration with C++ shared libraries that use the MATLAB Data API.

Call a Shared Library

Workflow to integrate with a shared library that uses the mwArray API.

Integrate C Shared Libraries

Process to create and distribute C shared libraries.

Integrate C++ Shared Libraries

Process to create and distribute C++ shared libraries.

Call MATLAB Compiler SDK API Functions from C/C++

Process to call MATLAB Compiler SDK API functions from C/C++.

Use Multiple Shared Libraries in Single Application

How to use multiple generated shared libraries in a single C/C++ application.

Configure the mbuild Options File

How to configure the mbuild options file.

Call COM Objects in Visual C++ Programs

How to integrate a COM object into a Visual C++® program

Write Applications for macOS

Guidelines for writing deployable C++ applications specifically for macOS

Concepts

MATLAB Runtime Path Settings for Development and Testing

Path settings for machines where you want to develop and test applications that contain compiled MATLAB code

Memory Management and Cleanup

Recommendations on memory management

MATLAB Runtime Startup Options

Set MATLAB Runtime options, such as -nojvm, -nodisplay, or -logfile

Understand the mclmcrrt Proxy Layer

All application and software components generated by MATLAB Compiler™ and MATLAB Compiler SDK need to link against only one MATLAB library, mclmcrrt.

MATLAB Runtime Component Cache and Deployable Archive Embedding

How to override the default archive embedding behavior and how to use MATLAB Runtime component cache

Was this topic helpful?