When generating code, MATLAB® Coder™ uses the following calling conventions:
Passes arrays by reference as inputs.
Returns arrays by reference as outputs.
Unless you optimize your code by using the same variable as both input and output, passes scalars by value as inputs. In that case, MATLAB Coder passes the scalar by reference.
Returns scalars by value for single-output functions.
Returns scalars by reference:
For functions with multiple outputs.
When you use the same variable as both input and output.
For more information about optimizing your code by using the same variable as both input and output, see Eliminate Redundant Copies of Function Inputs.
You can call the C/C++ functions generated for libraries as
custom C/C++ code from MATLAB functions that are suitable for
code generation. For static libraries, you must use the coder.ceval
function to wrap the function
calls, as in this example:
function y = callmyCFunction %#codegen y = 1.5; y = coder.ceval('myCFunction',y); end
callmyCFunction
calls
the custom C function myCFunction
, which takes
one input argument.For dynamically-linked libraries, you can also use coder.ceval
.
There are additional requirements for calling C/C++ functions from the MATLAB code in the following situations:
You want to call generated C/C++ libraries or executables from a MATLAB function. Call housekeeping functions generated by MATLAB Coder, as described in Calling Initialize and Terminate Functions.
You want to call C/C++ functions that are generated from MATLAB functions that have more than one output, as described in Calling C/C++ Functions with Multiple Outputs.
You want to call C/C++ functions that are generated from MATLAB functions that return arrays, as described in Calling C/C++ Functions that Return Arrays.
When you convert a MATLAB function to a C/C++ library function or a C/C++ executable, MATLAB Coder automatically generates two housekeeping functions that you must call along with the C/C++ function.
Housekeeping Function | When to Call |
---|---|
primary_function_name_initialize | Before you call your C/C++ executable or library function for the first time |
primary_function_name_terminate | After you call your C/C++ executable or library function for the last time |
From C/C++ code, you can call these functions directly. However,
to call them from MATLAB code that is suitable for code generation,
you must use the coder.ceval
function. coder.ceval
is
a MATLAB Coder function, but is not supported by the native MATLAB language.
Therefore, if your MATLAB code uses this function, use coder.target
to disable these calls in MATLAB and
replace them with equivalent functions.
Although MATLAB Coder can generate C/C++ code from MATLAB functions
that have multiple outputs, the generated C/C++ code cannot return
multiple outputs directly because the C/C++ language does not support
multiple return values. Instead, you can achieve the effect
of returning multiple outputs from your C/C++ function by using coder.wref
with coder.ceval
.
Although MATLAB Coder can generate C/C++ code from MATLAB functions
that return values as arrays, the generated code cannot return arrays by
value because the C/C++ language is limited to returning
single, scalar values. Instead, you can return arrays from your C/C++
function by reference as pointers by using coder.wref
with coder.ceval
.