Generating C/C++ Static Libraries from MATLAB Code

Generate a C Static Library Using the MATLAB Coder App

This example shows how to generate a C static library from MATLAB® code using the MATLAB Coder™ app.

In this example, you create a MATLAB function that adds two numbers. You use the app to create a MATLAB Coder project and generate a C static library for the MATLAB code.

Create the Entry-Point Function

In a local writable folder, create a MATLAB file, mcadd.m, that contains:

function y = mcadd(u,v) %#codegen
y = u + v;

Create the Test File

In the same local writable folder, create a MATLAB file, mcadd_test.m, that calls mcadd with example inputs. The example inputs are scalars with type int16.

function y = mcadd_test
y = mcadd(int16(2), int16(3));

Open the MATLAB Coder App

On the MATLAB Toolstrip Apps tab, under Code Generation, click the MATLAB Coder app icon.

The app opens the Select Source Files page.

Specify Source Files

  1. On the Select Source Files page, type or select the name of the entry-point function mcadd.

    The app creates a project with the default name mcadd.prj in the current folder.

  2. Click Next to go to the Define Input Types step. The app analyzes the function for coding issues and code generation readiness. If the app identifies issues, it opens the Review Code Generation Readiness page where you can review and fix issues. In this example, because the app does not detect issues, it opens the Define Input Types page.

Define Input Types

Because C uses static typing, at compile time, MATLAB Coder must determine the properties of all variables in the MATLAB files. You must specify the properties of all entry-point function inputs. From the properties of the entry-point function inputs, MATLAB Coder can infer the properties of all variables in the MATLAB files.

Specify the test file mcadd_test.m that MATLAB Coder can use to automatically define types for u and v:

  1. Enter or select the test file mcadd_test.m.

  2. Click Autodefine Input Types.

    The test file, mcadd_test.m, calls the entry-point function, mcadd with the example input types. MATLAB Coder infers that inputs u and v are int16(1x1).

  3. Click Next to go to the Check for Run-Time Issues step.

Check for Run-Time Issues

The Check for Run-Time Issues step generates a MEX file from your entry-point functions, runs the MEX function, and reports issues. This step is optional. However, it is a best practice to perform this step. You can detect and fix run-time errors that are harder to diagnose in the generated C code.

  1. To open the Check for Run-Time Issues dialog box, click the Check for Issues arrow .

    The app populates the test file field with mcadd_test, the test file that you used to define the input types.

  2. Click Check for Issues.

    The app generates a MEX function. It runs the test file replacing calls to mcadd with calls to the MEX function. If the app detects issues during the MEX function generation or execution, it provides warning and error messages. Click these messages to navigate to the problematic code and fix the issue. In this example, the app does not detect issues.

  3. Click Next to go to the Generate Code step.

Generate C Code

  1. To open the Generate dialog box, click the Generate arrow .

  2. In the Generate dialog box, set Build type to Static Library (.lib) and Language to C. Use the default values for the other project build configuration settings.

  3. Click Generate.

    The app indicates that code generation succeeded. It displays the source MATLAB files and generated output files on the left side of the page. On the Variables tab, it displays information about the MATLAB source variables. On the Target Build Log tab, it displays the build log, including compiler warnings and errors. By default, in the code window, the app displays the C source code file, mcadd.c. To view a different file, in the Source Code or Output Files pane, click the file name.

    MATLAB Coder generates a standalone C static library mcadd in the codegen\lib\mcadd folder. It generates the minimal set of #include statements for header files required by the selected code replacement library.

  4. To view the code generation report, click View Report.

  5. Click Next to open the Finish Workflow page.

Review the Finish Workflow Page

The Finish Workflow page indicates that code generation succeeded. It provides a project summary and links to generated output.

Generate a C Static Library at the Command Line

This example shows how to generate a C static library from MATLAB code at the command line using the codegen function.

  1. In a local writable folder, create a MATLAB file, mcadd.m, that contains:

    function y = mcadd(u,v) %#codegen
    y = u + v;

  2. Using the config:lib option, generate C library files. Using the -args option, specify that the first input is a 1-by-4 vector of unsigned 16-bit integers and that the second input is a double-precision scalar.

    codegen -config:lib mcadd -args {zeros(1,4,'uint16'),0}

    MATLAB Coder generates a C static library with the default name, mcadd, and supporting files in the default folder, codegen/lib/mcadd. It generates the minimal set of #include statements for header files required by the selected code replacement library.

Was this topic helpful?