Main Content

Deploy Generated Code

Deployment is the process of using the generated code in an application that runs outside of the MATLAB® environment. Many topics and considerations are relevant to the deployment process.

Main Function

To create an application, create or use a C/C++ main function to call the C/C++ entry-point functions generated from your MATLAB functions. The main function specifies input, output, and other functionality that your MATLAB algorithms do not specify. The code generator produces an example main function by default. Use the generated example main as a starting point for creating a new main function. The example main provides a clear example for how to pass input to and output from the generated code. For more information and examples, see:

Your C/C++ code must call an initialize function and a terminate function that are generated in addition to your C/C++ entry-point functions. By default, the generated C/C++ entry-point function calls the initialize function. The generated example main function calls the terminate function. As you create and edit your own main function, ensure that both initialize and terminate functions are called. For more information, see:

Generated Function Interfaces

To write a main function, you must be familiar with the generated function interfaces.

Data Types

The generated C/C++ function prototypes use data types that correspond to the types that you use in your MATLAB code. See Mapping MATLAB Types to Types in Generated Code. With Embedded Coder®, you can customize the appearance and style of generated data types. See Code Appearance (Embedded Coder).

Argument Passing Behavior

C/C++ entry-point functions generated from MATLAB Coder™ follow these conventions:

  • Pass input arrays by reference.

  • Return output arrays by reference.

  • Pass input scalars by value.

  • Return scalars by value for single-output functions.

  • Return scalars by reference:

    • For functions with multiple outputs.

    • When you use the same variable as input and output.

If you use the same variable as input and output in your MATLAB code, the generated code passes the scalar by reference. See Avoid Data Copies of Function Inputs in Generated Code.

Array Definition

Fixed-size and variable-size arrays are represented by different data types in the generated C/C++ code. For more information, see Use C Arrays in the Generated Function Interfaces.

Executable Applications

After you generate code and write a main file that uses the generated code, then you must build your code into an executable by using either MATLAB Coder or other build tools. You might want to run the executable application on your MATLAB platform, the host platform, or on a different platform, the target platform. To package the required elements of the generated code into an exportable zip file that you can manually transfer to a target platform, use the packNGo function.

The code generation folder does not necessarily contain all files used by the generated code. The folder can also contain supporting files that are not used by the generated code. Use packNGo to package and move generated code files rather than moving and including the entire code generation folder contents.

Binary Deployment

You can generate binaries directly by using the codegen command or the MATLAB Coder app by selecting a build type of static library, dynamic library, or executable (lib, dll, or exe). By default, the generated binaries are functional for the host platform hardware and operating system. To build an executable, you must specify or provide a main file. For an example, see Generating Standalone C/C++ Executables from MATLAB Code. If you set the GenerateExampleMain property of a configuration object to 'GenerateCodeAndCompile', the code generator builds an executable by using the generated example main file.

If you want to deploy your code to another platform, then you can use hardware support packages that provide support for generating and building the binary code for that platform. This support includes specific toolchains and code generation configuration settings that the target hardware requires. For a list of support packages provided for MATLAB Coder, see MATLAB Coder Supported Hardware. Many additional hardware support packages are available for Embedded Coder. See Embedded Coder Supported Hardware (Embedded Coder). If you want to specify a custom toolchain for build that is not available from a hardware support package, you can register your own toolchain. See Custom Toolchain Registration.

In the MATLAB Coder app, select a hardware support package during the Generate Code step from the Hardware Board drop-down list. From the command line, specify a hardware support package by using the coder.hardware function.

Source Code Deployment

In certain cases, you might choose to generate source code, and then manually build the source code for your project. Manually build the source code when:

  • Your generated source code is easy to build. For example, your generated code does not require linking against additional libraries.

  • You want to create an executable for custom hardware for which you do not have a hardware support package.

  • You are knowledgeable in building C/C++ source code or the build system for the target platform is already configured.

The code generator produces a buildInfo object that enables you to view and modify build information that MATLAB Coder uses to create binary outputs. You can use this information for understanding how to manually build your generated code. See Build Process Customization and RTW.BuildInfo (Embedded Coder).

The code generator produces a makefile that shows build information such as compile and link flags. Find this makefile in the code generation folder. The generated makefile is specific to the target platform that you specify by selecting a hardware support package or the host platform, if no hardware support package is specified. If you manually build your source code, you can use this makefile to identify and troubleshoot build requirements, such as compiling and linking flags.

To see how to manually configure code generation and build for a target platform, see Deploy Generated C Code to External Hardware: Raspberry Pi Examples.

Static and Dynamic Libraries

When you want to use generated code functionality in an existing C/C++ project, you can generate a static library or dynamic library. Libraries can provide a more modular interface than generated source code. When MATLAB Coder generates a static library or dynamic library:

  • The library is suitable for the platform that you are working on, unless you specify an alternative platform through a hardware support package.

  • The generated header files for C code explicitly declare the exported functions as extern "C" to simplify integration of the library into C++ applications.

  • The generated library file extensions correspond to the MATLAB host platform operating system.

    Operating SystemStatic LibraryDynamic Library
    Windows®.lib.dll and .lib for corresponding import library
    macOS.a.dylib
    Linux®.a.so

You must compile and link against libraries when you build an executable. When an executable that uses a dynamic library runs, the library must be on the system path or in the executable folder. For examples of using a generated library, see:

Loading generated dynamic libraries into MATLAB by using the loadlibrary function is not recommended and can result in incorrect behaviors or crashes.

Generated File Structure

By default, MATLAB Coder produces one C code file for each MATLAB code file. You can choose to partition the generated code into one single file and generate code with customized output folders and binary names. See How MATLAB Coder Partitions Generated Code.

With Embedded Coder, you can customize generated file names. See Customize C/C++ File Names Generated from MATLAB Code (Embedded Coder).

Code Verification

Before you deploy generated code for execution outside the MATLAB environment, you can verify it inside the MATLAB environment. The primary workflow for verification with MATLAB Coder is the generation and execution of C/C++ MEX functions. MEX functions run inside the MATLAB environment and provide run-time error checking and diagnostics. See Code Verification.

Embedded Coder offers deep additional functionality for code verification and testing. You can use software-in-the-loop (SIL) and processor-in-the-loop (PIL) execution to test the behavior of the generated code on software and hardware outside of the MATLAB environment. See Verification (Embedded Coder).

Custom Hardware Considerations

If your target supports only single data types and not double data types, you can generate single-precision code by using the codegen -singleC option. This option requires Fixed-Point Designer™. If your target supports only integer data types, use the -float2fixed option. See codegen.

Other Deployment Strategies

MATLAB Coder generates readable and portable C/C++ code for a subset of the MATLAB language. If you want to generate a standalone executable application for the host platform that uses the MATLAB Runtime libraries, but runs without a MATLAB license, then use MATLAB Compiler SDK™. For a product comparison, see https://www.mathworks.com/matlabcentral/answers/223937-should-i-use-matlab-compiler-sdk-or-matlab-coder-to-integrate-my-matlab-applications-with-c-c

See Also

|

Related Topics

External Websites