Generate Code for Multiple Entry-Point Functions - MATLAB & Simulink (original) (raw)
An entry-point function is a stable interface to the generated code. For many applications, you only need to generate code for a single entry-point function. However, you can also generate C/C++ code for multiple entry-point functions simultaneously. By using multiple entry-point functions, you can:
- Generate shared C/C++ libraries that have greater functionality than independent libraries for each entry-point function
- Generate code that more efficiently shares subfunctions used by multiple entry-point functions
- Generate library functions that can communicate using shared memory, such as when using the same global or persistent variables.
As a best practice, generate a MEX function to validate entry-point interactions in MATLAB® before generating a C/C++ library. See Check for Issues in MATLAB Code Using MEX Functions.
Specify Multiple Entry-Point Functions
To specify multiple entry-point functions, use one of these approaches:
- If you use the MATLAB Coderâ„¢ app, on the Select page, select multiple entry-point functions.
- If you generate code at the command line by using the codegen command, specify the entry-point functions followed by function arguments in a space-delimited list. For example, to generate code for entry-point functions
myMult
andmyAdd
, each of which takes two scalar double inputs, use this command:
codegen myMult -args {0,0} myAdd -args {0,0}
For more information about input-type specification, see Specify Types of Entry-Point Function Inputs.
Create Shared MEX Function
When you generate a MEX function for multiple entry-point functions, the code generates a single, shared MEX function. The default name and location of the shared MEX function depends on whether you generate code at the command line or by using the MATLAB Coder app.
Using the MATLAB Coder App
If you generate a MEX function by using the MATLAB Coder app, the app generates the MEX function in the same location as theMATLAB Coder project file. The app names the MEX function according to theOutput file name property on the Generate page.
At the Command Line
If you generate a MEX function at the command line using thecodegen
command, the code generator saves the MEX function in the working directory. The code generator names the MEX function using the name of one of the entry-point functions. For example, generate code for functionmyMult
and function myAdd
in namespacemyNamespace
. The code generator creates a shared MEX function named myMult_mex
in the working directory.
codegen myMult -args {0,0} myNamespace.myAdd -args {0,0}
The code generator creates a shared MEX function named myMult_mex
.
You can specify the name of the generated MEX function by using the-o
option of the codegen
command. For example, generate code for function myFun
and functionmyAdd
in namespace myNamespace
, and specify the name myMath
for the shared MEX function. The code generator creates a shared MEX function named myMath
in the working directory.
codegen -o myMath myMult -args {0,0} myNamespace.myAdd -args {0,0}
Use Shared MEX Functions
To call an individual entry-point function from a shared MEX function, pass the name of the entry-point function to the shared MEX function, followed by the required inputs. If the entry-point function is inside a namespace, call the function by using dot notation.
For example, to call the MEX function generated for myMult
with sample inputs, use this syntax:
To call the MEX function generated for myAdd
in the namespacemyNamespace
with sample inputs, use this syntax:
myMath("myNamespace.myAdd",4,6)
To learn more about code generation for functions in namespaces, see Code Generation for Entry-Point Functions in Namespaces.
Example: Generate Code for Multiple Entry-Point Functions Simultaneously
This example shows how to generate a multisignature MEX function and standalone C code for two functions simultaneously.
Define Functions
Define the function myAdd
in the namespace additionFunctions
. This function adds two inputs.
type(fullfile("+additionFunctions","myAdd.m"))
function out = myAdd(a,b) out = a+b; end
Define the function myMult
in the base namespace. This function multiplies two inputs.
function out = myMult(a,b) out = a*b; end
Test the functions in MATLAB by using sample inputs.
additionFunctions.myAdd(4,5)
Generate a Shared MEX Function
Generate a shared MEX function for the two entry-point functions by using the codegen
command. Specify two input arguments of type double
for each function. Use the -o
option to name the shared MEX function myMath
. The code generator creates the MEX function myMath
in the working directory. It generates the source, header, and object files in the folder codegen/mex/myMath
.
codegen -o myMath additionFunctions.myAdd -args {0 0} myMult -args {0 0}
Code generation successful.
Call the shared MEX function for each entry-point function. You must specify the namespace when calling myAdd
. Pass the same arguments you used to test the functions in MATLAB.
myMath("additionFunctions.myAdd",4,5)
Generate C Code
Generate C code for additionFunctions.myAdd
and myMult
simultaneously. Use the -config:lib
option to specify a C static library target. Use the -o
option to instruct the code generator to use the name myMath
when naming the shared source, header, and object files.
codegen -config:lib -o myMath additionFunctions.myAdd -args {0 0} myMult -args {0 0}
Code generation successful.
Examine the generated files. The code generator stores the generated files in the folder codegen/mex/myMath
. The code generator begins the names of shared source, header, and object files with myMath
, but it generates separate source and header files for each entry-point function.
type(fullfile("codegen","lib","myMath","additionFunctions_myAdd.c"))
/*
- File: additionFunctions_myAdd.c
- MATLAB Coder version : 24.2
- C/C++ source code generated on : 22-Jan-2025 23:09:43 */
/* Include Files */ #include "additionFunctions_myAdd.h"
/* Function Definitions / /
- Arguments : double a
double b
- Return Type : double */ double additionFunctions_myAdd(double a, double b) { return a + b; }
/*
- File trailer for additionFunctions_myAdd.c
- [EOF] */
type(fullfile("codegen","lib","myMath","myMult.c"))
/*
- File: myMult.c
- MATLAB Coder version : 24.2
- C/C++ source code generated on : 22-Jan-2025 23:09:43 */
/* Include Files */ #include "myMult.h"
/* Function Definitions / /
- Arguments : double a
double b
- Return Type : double */ double myMult(double a, double b) { return a * b; }
/*
- File trailer for myMult.c
- [EOF] */