Call Generated C/C++ Functions

Conventions for Calling Functions in Generated Code

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.

How to Call C/C++ Functions from MATLAB Code

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
Here, the MATLAB function 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:

Calling Initialize and Terminate Functions

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 FunctionWhen to Call
primary_function_name_initializeBefore you call your C/C++ executable or library function for the first time
primary_function_name_terminateAfter 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.

Calling C/C++ Functions with Multiple Outputs

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.

Calling C/C++ Functions that Return Arrays

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.

Was this topic helpful?