Code Generation for Entry-Point Functions in Namespaces
MATLAB® namespaces are folders that can contain class folders, class definition files, function files, and other namespaces. Using a namespace helps you organize code and create more robust names for the items contained inside it. When you generate code for an entry-point function in a namespace, the names and locations of the generated files depend on the namespace. Additionally, the way the code generator maps the namespace to the generated code varies depending on whether you generate C or C++ code.
Generate Code for Entry-Point Function in Namespace
Generate code for an entry-point function in a namespace either by using the
MATLAB
Coder™ app or at the command line by using the codegen
command.
Using the MATLAB Coder app
In the MATLAB Coder app, browse to and select the entry-point function inside the namespace.
When generating code for entry-point functions in namespaces, the MATLAB Coder app does not support:
Numeric conversion, including converting floating-point MATLAB code to fixed-point C/C++ code and converting double-precision MATLAB code to single-precision C/C++ code.
Automatically defining entry-point input types. You must enter input types directly.
The Check for Run-Time Issues step. You must manually generate and execute a trial MEX function at the command line.
Code verification using software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation. You must manually execute the generated SIL or PIL MEX function at the command line. SIL and PIL verification requires an Embedded Coder® license.
Using the codegen
Command
At the command line, pass the entry-point function to the
codegen
command by using dot notation. For example, to
generate code for function myFun
in namespace
myNamespace
, use this
command:
codegen myNamespace.myFun
Code generation does not support the codegen
options
-double2single
, -float2fixed
, and
-singleC
for entry-point functions in namespaces.
Names and Locations of Generated Files
This table shows the names and locations of the generated files when you generate code
for entry-point function myFun
in the namespace
myNamespace
.
Types of Files | Location | File Name |
---|---|---|
Platform-specific MEX file | Current folder | myNamespace_myFun_mex |
C/C++ source, header, and object files |
| Generated file names begin with the name of the namespace. For example:
|
Mapping of MATLAB Namespaces to the Generated Code
Because entry-point functions are stable interfaces to the generated code, the code generator maps entry-point functions in namespaces to the generated code following specific rules that depend on whether you generate C++ or C code.
C++ Code
When you generate C++ code for an entry-point function in a MATLAB namespace, the code generator maps the MATLAB namespace to a C++ namespace by default. For example, consider
entry-point function myAdd
in namespace
myNamespace
.
function out = myAdd(a,b) out = a+b; end
Generate C++ code for myNamespace.myAdd
at the command line and
examine the generated function. The generated function is inside the C++ namespace
myNamespace
.
namespace myNamespace {
double myAdd(double a, double b)
{
return a + b;
}
}
You can customize how the code generator maps MATLAB namespaces to C++ namespaces by using code configuration parameters. See Organize Generated C++ Code into Namespaces.
C Code
Because C does not have a name scoping feature, when you generate C code for a
MATLAB function in a MATLAB namespace, the name of the generated C function begins with the name
of the MATLAB namespace. This naming pattern prevents naming conflicts when you
generate code for multiple functions with the same name in different MATLAB namespaces. For example, consider entry-point function
myAdd
in namespace myNamespace
.
function out = myAdd(a,b) out = a+b; end
Generate C code for myNamespace.myAdd
at the command line and
examine the generated function. The generated function name begins with the name of
the MATLAB
namespace.
double myNamespace_myAdd(double a, double b)
{
return a + b;
}
Troubleshooting
Naming Conflicts
When generating C/C++ code for entry-point functions in namespaces, the code
generator can rename the entry-point functions to construct the C/C++ function
signature. For example, if you generate C code for function myAdd
in namespace myNamespace
, the generated function name is
myNamespace_myAdd
. If the names of other functions or C++
classes in the generated code conflict with the renamed entry point, the code
generator produces an error similar to one of these:
Entry-point functions '
function1
' and
'function2
' attempt to use the same generated
file name ('namespace_function
'). Rename one of the
functions.
C++ interface class name must not match an entry-point function
name.
To resolve these errors, rename the functions or classes in conflict.
Issues Resolving Filename and Path
If you specify an entry-point function in a namespace by calling the
codegen
command with the relative path to the entry-point
function instead of by using dot notation, the code generator can produce a warning
or an error if it is unable to find an entry-point function that matches your
specifications. To avoid these errors and warnings, specify entry-point functions in
namespaces by using dot notation.
Example: Generate C++ Code for Entry-Point Functions in Namespaces
This example shows how to generate a shared MEX function and standalone C++ code for two functions in two different namespaces. The functions have the same name, but different settings, parameters, and functionality.
Define Functions in Different MATLAB Namespaces
Define the function calculateEnergy
in the namespace +classical
. This function takes the mass and velocity of an object, in kg and m/s, respectively, and returns the kinetic energy in J.
type(fullfile("+classical","calculateEnergy.m"))
function out = calculateEnergy(mass,velocity) out = 0.5*mass*velocity^2; end
Define the function calculateEnergy
in the namespace +quantum
. This function takes the frequency of a photon in Hz and returns quantum energy in J.
type(fullfile("+quantum","calculateEnergy.m"))
function out = calculateEnergy(frequency) h = 6.62607015e-34; % Planck's constant out = h*frequency; end
Test the functions in MATLAB with sample input values.
classical.calculateEnergy(10,5)
ans = 125
quantum.calculateEnergy(85)
ans = 5.6322e-32
Generate a Shared MEX Function
Generate a shared C MEX function for the two entry-point functions by using the codegen
command. Use the -o
option to name the shared MEX function energyCalculator
. The code generator creates the MEX function energyCalculator
in the working directory. To learn more about code generation for multiple entry points, see Generate Code for Multiple Entry-Point Functions.
codegen -o energyCalculator classical.calculateEnergy -args {0,0} quantum.calculateEnergy -args {0}
Code generation successful.
Call the shared MEX function for each entry-point function by using dot notation. Pass the same arguments you used to test the functions in MATLAB.
energyCalculator("classical.calculateEnergy",10,5)
ans = 125
energyCalculator("quantum.calculateEnergy",85)
ans = 5.6322e-32
Generate C++ Code for Each Entry-Point Function
Generate standalone C++ code for the two entry-point function simultaneously. Use the codegen
command with the -lang:c++
option to generate C++. Use the -o
option to use energyCalculator
as the base name and location of the shared generated files.
codegen -config:lib -lang:c++ -o energyCalculator classical.calculateEnergy -args {0,0} quantum.calculateEnergy -args {0}
Code generation successful.
Examine the generated C++ code. The code generator has created separate standalone C++ functions for the two versions of the calculateEnergy
function. Each function is within a C++ namespace that matches the MATLAB namespace.
type(fullfile("codegen","lib","energyCalculator","classical_calculateEnergy.cpp"))
// // File: classical_calculateEnergy.cpp // // MATLAB Coder version : 24.2 // C/C++ source code generated on : 05-Sep-2024 13:47:03 // // Include Files #include "classical_calculateEnergy.h" // Function Definitions // // Arguments : double mass // double velocity // Return Type : double // namespace classical { double calculateEnergy(double mass, double velocity) { return 0.5 * mass * (velocity * velocity); } } // namespace classical // // File trailer for classical_calculateEnergy.cpp // // [EOF] //
type(fullfile("codegen","lib","energyCalculator","quantum_calculateEnergy.cpp"))
// // File: quantum_calculateEnergy.cpp // // MATLAB Coder version : 24.2 // C/C++ source code generated on : 05-Sep-2024 13:47:03 // // Include Files #include "quantum_calculateEnergy.h" // Function Definitions // // Arguments : double frequency // Return Type : double // namespace quantum { double calculateEnergy(double frequency) { // Planck's constant return 6.62607015E-34 * frequency; } } // namespace quantum // // File trailer for quantum_calculateEnergy.cpp // // [EOF] //