Incorporate Generated Code Using an Example Main Function
When you build an application that uses generated C/C++ code, you must provide a C/C++ main function that calls the generated code.
By default, for code generation of C/C++ source code, static libraries, dynamic libraries, and executables, MATLAB® Coder™ generates an example C/C++ main function. This function is a template that can help you incorporate generated C/C++ code into your application. The example main function declares and initializes data, including dynamically allocated data. It calls entry-point functions but does not use values that the entry point functions return.
MATLAB
Coder generates source and header files for the example main function in the
examples
subfolder of the build folder. For C code generation, it
generates the files main.c
and main.h
. For C++ code
generation, it generates the files main.cpp
and
main.h
.
Do not modify the files main.c
and main.h
in the
examples
subfolder. If you do, when you regenerate code, MATLAB
Coder does not regenerate the example main files. It warns you that it detects changes
to the generated files. Before using the example main function, copy the example main source
and header files to a location outside of the build folder. Modify the files in the new
location to meet the requirements of your application.
The packNGo
function and the Export > Package Generated
Code as a ZIP File button in the MATLAB
Coder app do not package the example main source and header files when you generate
the files using the default configuration settings. To package the example main files,
configure code generation to generate and compile the example main function, generate your
code, and then package the build files.
Workflow for Using an Example Main Function
Prepare your MATLAB code for code generation.
Check for run-time issues.
Make sure that example main generation is enabled.
Generate C/C++ code for the entry-point functions.
Copy the example main files from the
examples
subfolder to a different folder.Modify the example main files in the new folder to meet the requirements of your application.
Deploy the example main and generated code for the platform that you want.
Build the application.
For an example that shows how to generate an example main and use it to build an executable, see Use an Example C Main in an Application.
Control Example Main Generation Using the MATLAB Coder App
To instruct the code generator to generate standalone code, on the MATLAB Coder tab of the toolstrip, click Output Type and select Static Library (.lib), Dynamic Library (.dll), or Executable (.exe).
Click Settings to open the Code Generation Settings dialog box. This table shows how to set the Generate example main parameter for different code generator behaviors.
Setting | Code Generator Behavior |
---|---|
Do not generate an example main function
| Does not generate an example C/C++ main function. |
Generate, but do not compile, an example main
function | Generates an example C/C++ main function but does not compile it. This behavior is the default. |
Generate and compile an example main
function | Generates an example C/C++ main function and compiles it |
Control Example Main Generation Using the Command-Line Interface
In a standalone code configuration object, set the
GenerateExampleMain
property. This table shows how to set the
GenerateExampleMain
property for different code generator
behaviors.
Setting | Code Generator Behavior |
---|---|
'DoNotGenerate'
| Does not generate an example C/C++ main function. |
'GenerateCodeOnly' | Generates an example C/C++ main function but does not compile it. This behavior is the default. |
'GenerateCodeAndCompile' | Generates an example C/C++ main function and compiles it. |
For example, create a code configuration object for 'lib'
,
'dll'
, or 'exe'
. Then, instruct the code generator
to generate an example C/C++ main function but not to compile
it.
cfg.GenerateExampleMain = 'GenerateCodeOnly'; cfg = coder.config('lib'); % or dll or exe
Note
In Windows®, when selecting Generate and compile an example main
function
or setting cfg.GenerateExampleMain
to
GenerateCodeAndCompile
, a Windows executable batch file (.bat) is generated in the current working directory
for dll
and lib
code generation, instead of an
.exe
file. You can modify the main.c
and
main.h
files present in the examples folder to meet your
application's requirements.