Main Content

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 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

  1. Prepare your MATLAB code for code generation.

  2. Check for run-time issues.

  3. Make sure that example main generation is enabled.

  4. Generate C/C++ code for the entry-point functions.

  5. Copy the example main files from the examples subfolder to a different folder.

  6. Modify the example main files in the new folder to meet the requirements of your application.

  7. Deploy the example main and generated code for the platform that you want.

  8. 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

  1. On the Generate Code page, to open the Generate dialog box, click the Generate arrow .

  2. In the Generate dialog box, set the Build Type to one of the following:

    • Source Code

    • Static Library

    • Dynamic Library

    • Executable

  3. Click More Settings.

  4. On the All Settings tab, under Advanced, set Generate example main to one of the following:

    Set ToFor
    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 functionGenerating an example C/C++ main function and compiling it

Control Example Main Generation Using the Command-Line Interface

  1. Set the GenerateExampleMain property.

    Set ToFor
    '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';
    

  2. 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 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.

Related Topics