Main Content

Generate Code for Multiple Entry-Point Functions

An entry-point function is a top-level MATLAB® function from which you generate code. For many applications, you may only need to generate code for a single entry-point function. You can also generate C/C++ code from multiple entry-point functions at the same time. By using multiple entry-point functions, you can:

  • Generate multi-functional C/C++ libraries that contain larger levels of functionality than if you were to generate independent libraries for each entry-point function.

  • Generate code that shares code more efficiently when multiple entry-point functions rely on the same subfunctions.

  • Generate library functions that can communicate using shared memory, for example, when they use the same global variables.

As a best practice, generate a MEX function to validate entry-point interactions in MATLAB before generating a C/C++ library.

Generating Code for Multiple Entry-Point Functions

To generate code for more than one entry-point function, use the syntax from the codegen reference page. By default, for MEX code generation, codegen:

  • Generates a MEX function in the current folder. Only a single MEX function is generated when you specify multiple entry-point functions. To call a single entry-point function from a generated MEX function, see Call a Single Entry-Point Function from a MEX Function.

  • Names the MEX function name_mex. name is the name of the first entry-point function from an alphabetical order.

  • Stores generated files in the subfolder codegen/mex/subfolder. subfolder is the name of the first entry-point function from a left-to-right order (as they are entered after the codegen command).

You can specify the output file name and subfolder name using the -o option:

codegen -o myOutputFileName fun1 fun2

In this case, codegen generates a MEX function named myOutputFileName in the current folder and stores generated files in the subfolder codegen/mex/myOutputFileName.

Example: Generating Code for Two Entry-Point Functions

Generate a MEX function for two entry-point functions, ep1 and ep2. Function ep1 takes one input and ep2 takes two inputs. Using the -o option, name the generated MEX function mySharedMex:

codegen -o mySharedMex ep1 -args {single(0)} ep2 -args {0,zeros(1,1024)}

codegen generates a MEX function named mySharedMex.mex in the current folder and stores generated files in the subfolder codegen/mex/mySharedMex.

To generate and compile standalone library code, use the -config:lib option.

codegen -config:lib -o mySharedLib ep1 -args single(0) ep2 -args {0,zeros(1,1024)}

The codegen command generates the C/C++ library code in the codegen/lib/mySharedLib folder.

To use the output type from one entry-point function as the input type to another, see Pass an Entry-Point Function Output as an Input. For information on viewing entry-point functions in the code generation report, see Code Generation Reports.

Call a Single Entry-Point Function from a MEX Function

Suppose that you have a MEX function myMex generated from multiple entry-point functions, fun1, fun2, …, funN. You can call a single entry-point function, fun_i, by using this syntax:

myMex('fun_i',param1,..,paramM)

Here the MATLAB function signature for fun_i is fun_i(param1,..,paramM).

For example, consider the MEX function, mySharedMex, that has entry-point functions ep1 and ep2. To call ep1 with an input parameter u, enter:

mySharedMex('ep1',u)

To call ep2 with input parameters v and x, enter:

mySharedMex('ep2',v,x)

Generate Code for More Than One Entry-Point Function Using the MATLAB Coder App

This example shows how to generate code for multiple entry-point functions using the MATLAB Coder™ app.

Create the Entry-Point Functions

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

    function y = ep1(u) %#codegen
    y = u;

  2. In the same local writable folder, create a MATLAB file, ep2.m, that contains:

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

Create the Test File

In the folder that contains ep1.m and ep2.m, create a MATLAB file, ep_test.m, that calls ep1 and ep2 with example inputs.

function [y, y1] = ep_test
y = ep1(single(2));
y1 = ep2(double(3), double(4));

Open the MATLAB Coder App

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

Specify Source Files

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

    The app creates a project with the default name ep1.prj in the current folder. To avoid code generation errors, you must store the project file and all entry-point MATLAB function files in the same folder.

  2. To add ep2 to the list of entry-point functions, click Add Entry-Point Function. Type or select the name of the entry-point function ep2.

  3. To go to the Define Input Types step, click Next. The app analyzes the functions 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 a test file that MATLAB Coder can use to automatically define types:

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

  2. Click Autodefine Input Types.

    The test file, ep_test.m, calls the entry-point functions ep1 and ep2 with the example input types. MATLAB Coder infers that for ep1, input u is single(1x1). For ep2, u and v are double(1x1).

  3. To go to the Check for Run-Time Issues step, click Next.

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 ep_test, the test file that you used to define the input types.

  2. Click Check for Issues.

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

  3. To go to the Generate Code step, click Next.

Generate MEX Function

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

  2. Set Build type to MEX.

  3. Verify that the Output file name is ep1_mex. By default, the app uses the name of the alphabetically first entry-point function.

  4. Click Generate.

    MATLAB Coder builds the project. It generates a MEX function, ep1_mex, in the current folder. MATLAB Coder also generates other supporting files in a subfolder called codegen/mex/ep1_mex. MATLAB Coder uses the name of the MATLAB function as the root name for the generated files. It creates a platform-specific extension for the MEX file, as described in Naming Conventions.

    You can now test your MEX function in MATLAB. See Call a Single Entry-Point Function from a MEX Function.

    If you generate a static library for ep1 and ep2, MATLAB Coder builds the project and generates a C library, ep1, and supporting files in the default folder, codegen/lib/ep1.

See Also

|

Related Topics