Main Content

Understand and Control Partitioning of the Generated Code

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:

  • To generate one C/C++ file for each MATLAB function, set the parameter to Generate one file for each MATLAB file.

  • To generate a single C/C++ file for the MATLAB code, set the parameter to Generate all functions into a single file.

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 the identity 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 and v 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 the MATLAB Coder tab of the toolstrip. Then, click the Generate Code button to generate code.

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

  • foo.c

  • foo.h

  • identity.c

  • identity.h

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 the coder.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 and v 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 the MATLAB 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 folder codegen/lib/bar:

  • bar.c

  • bar.h

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 function myFun.

Types of FilesLocationFile Name
Platform-specific MEX fileCurrent foldermyFun_mex
C/C++ source, header, and object files

codegen/build type/myFun

build type can be:

  • mex for MEX functions

  • exe for C/C++ executables

  • lib for C/C++ libraries

  • dll for C/C++ dynamic libraries

Generated file names begin with the name of the entry-point function. For example:

  • myFun.c

  • myFun.cpp

  • myFun.h

  • myFun.c

  • myFun_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:

  • On the MATLAB Coder tab of the toolstrip, click Settings. Specify a base name in the Custom output file box.

  • At the command line, use the -o option with the codegen command.

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

  • On the MATLAB Coder tab of the toolstrip, click Settings > View all settings. In the Code Configuration dialog box, on the Output pane, in the Paths section, select the build folder.

  • At the command line, use the -d option with the codegen command.

See Also

| |

Topics