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 Package option
of 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.
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.
On the Generate Code page, to
open the Generate dialog box, click the Generate arrow
.
In the Generate dialog box, set the Build Type to one of the following:
Source Code
Static Library
Dynamic Library
Executable
Click More Settings.
On the All Settings tab, under Advanced, set Generate example main to one of the following:
Set To | For |
---|---|
Do not generate an example main function | Not generating an example C/C++ main function |
Generate, but do not compile, an example main
function (default) | Generating an example C/C++ main function but not compiling it |
Generate and compile an example main function | Generating an example C/C++ main function and compiling it |
Create a code configuration object for 'lib'
, 'dll'
,
or 'exe'
. For example:
cfg = coder.config('lib'); % or dll or exe
Set the GenerateExampleMain
property.
Set To | For |
---|---|
'DoNotGenerate' | Not generating an example C/C++ main function |
'GenerateCodeOnly' (default) | Generating an example C/C++ main function but not compiling it |
'GenerateCodeAndCompile' | Generating an example C/C++ main function and compiling it |
For example:
cfg.GenerateExampleMain = 'GenerateCodeOnly';