Understand and Control Partitioning of the Generated Code - MATLAB & Simulink (original) (raw)

By default, the code generator partitions the generated code to match your MATLAB® file structure. This one-to-one mapping lets you easily correlate the generated C/C++ files with your MATLAB code. Alternatively, you can select to generate all C/C++ functions into a single file. This option facilitates integrating your code with existing embedded software.

Control Partitioning Method

The partitioning of generated C/C++ code depends on the file partitioning method that you select, as well as your inlining settings. To set a file partitioning method, set the configuration parameter Generated file partitioning method to one of these settings:

However, if you do not disable inlining, the code generator might inline some MATLAB functions, even if you set Generated file partitioning method to Generate one file for each MATLAB file. To force the code generator to generate one C/C++ file for each top-level MATLAB function, you must also disable inlining. For more information about controlling inlining, see Control Inlining to Fine-Tune Performance and Readability of Generated Code.

Partitioning Generated Files with One C/C++ File Per MATLAB File

By default, for MATLAB functions that are not inlined, the code generator produces one C/C++ file for each MATLAB file. In this case, MATLAB Coderâ„¢ partitions generated C/C++ code so that it corresponds to your MATLAB files.

Partitioning of Entry-Point Functions

For each entry-point function, the code generator produces one C/C++ source, header, and object file with the same name as the MATLAB file.

For example, suppose you define a simple function foo that calls the function identity. Use coder.inline to instruct the code generator not to inline theidentity function.

function y = foo(u,v) %#codegen s = single(u); d = double(v); y = double(identity(s))+identity(d); end

function y = identity(u) %#codegen coder.inline("never") y = u; end

At the command line, use the -config:lib option to specify a static library build and specify that the input arguments u andv are scalar doubles by using the -args option. Use the -c option to generate source code only.

codegen -config:lib foo -c -args {0 0}

Alternatively, if you use the MATLAB Coder app, add foo as an entry-point function and specify that u and v are scalar doubles. Set the build type by clicking Build Type > Static Library (.lib) on theMATLAB Coder tab of the toolstrip. Then, click the Generate Code button to generate code.

The code generator these source and header files for foo andidentity in the folder codegen/lib/foo:

Partitioning of Local Functions

For each local function, the code generator produces code in the same C/C++ file as the calling function. This partitioning patten is not affected by inlining settings.

For example, suppose you define a function bar that calls a local function identity_local. Use thecoder.inline("never") to instruct the code generator not to inline identity_local.

function y = bar(u,v) %#codegen s = single(u); d = double(v); y = double(identity_local(s))+identity_local(d); end

function y = identity_local(u) coder.inline("never") y = u; end

At the command line, use the -config:lib option to specify a static library build and specify that the input arguments u andv are scalar doubles by using the -args option.

codegen -config:lib bar -args {0 0}

Alternatively, if you use the MATLAB Coder app, add bar as an entry-point function and specify that u and v are scalar doubles. Set the build type by clicking Build Type > Static Library (.lib) on theMATLAB Coder tab of the toolstrip. Then, click the Generate Code button to generate code.

The code generator produces source and header files for bar, but not identity_local, in the foldercodegen/lib/bar:

Generated Files and Locations

The types and locations of generated files depend on the build type that you specify. Each time the code generator generates the same type of build for the same entry point, it removes the files from the previous build. If you want to preserve files from a build, copy them to a different location before starting another build.

By default, the names and locations of the generated files depend on the names of the entry points and on the build type. This table shows the default names and locations of the generated files when you generate code for entry-point functionmyFun.

Types of Files Location File Name
Platform-specific MEX file Current folder myFun_mex
C/C++ source, header, and object files codegen/build type/myFun_build type_ can be: mex for MEX functionsexe for C/C++ executableslib for C/C++ librariesdll for C/C++ dynamic libraries Generated file names begin with the name of the entry-point function. For example: myFun.cmyFun.cppmyFun.hmyFun.cmyFun_data.c
Code generation report, if you chose to generate one codegen/build type/myFun report.mlxdata

To change the base name of the generated files, use one of these approaches:

To change the location of the generated files, use one of these approaches:

See Also

MATLAB Coder | codegen | coder.inline

Topics