Incorporate Generated Code Using an Example Main Function - MATLAB & Simulink (original) (raw)
Main Content
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 theexamples
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
andmain.h
.
Do not modify the files main.c
and main.h
in theexamples
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.
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
- On the Generate Code page, to open theGenerate 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
Control Example Main Generation Using the Command-Line Interface
- 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'; - Create a code configuration object for
'lib'
,'dll'
, or'exe'
. For example:
cfg = coder.config('lib'); % or dll or exe
Note
In Windows®, when selecting Generate and compile an example main function
or setting cfg.GenerateExampleMain
toGenerateCodeAndCompile
, 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
andmain.h
files present in the examples folder to meet your application's requirements.