Main Content

Structure of Generated Example C/C++ 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. To use the example main function, copy the example main source and header files to a location outside of the build folder, and then modify the files in the new location to meet the requirements of your application.

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.

Contents of the File main.c or main.cpp

For the example main source file main.c or main.cpp, MATLAB Coder generates the following sections:

By default, MATLAB Coder also generates comments in the example main source file that can help you modify the example main function to use in your application.

Include Files

This section includes the header files required to call code that is not in the example main source file. If you call external functions when you modify the example main source file, include any other required header files.

Function Declarations

This section declares the function prototypes for the argument initialization and entry-point functions that are defined in the example main source file. Modify the function prototypes to match modifications that you make in the function definitions. Declare new function prototypes for functions that you define in the example main source file.

Argument Initialization Functions

This section defines an initialization function for each data type that the entry-point functions use as an argument. The argument initialization function initializes the size of the argument to a default value and the values of the data to zero. The function then returns the initialized data. Change these size and data values to meet the requirements of your application.

For an argument with dimensions of size <dimSizes> and MATLAB C/C++ data type <baseType>, the example main source file defines an initialization function with the name argInit_<dimSizes>_<baseType>. For example, for a 5-by-5 array with data of MATLAB type double, the example main source file defines the argument initialization function argInit_5x5_real_T.

MATLAB Coder alters the name of the argument initialization functions as follows:

  • If any of the dimensions are variable-size, MATLAB Coder designates the size of these dimensions as d<maxSize>, where <maxSize> is the maximum size of that dimension. For example, for an array with data of MATLAB type double with a first dimension of static size 2 and a second dimension that can vary in size up to 10, the example main source file defines the argument initialization function argInit_2xd10_real_T.

  • If any of the dimensions are unbounded, MATLAB Coder designates the size of these dimensions as Unbounded.

  • If the return type of the initialization function is an emxArray, MATLAB Coder defines the function as returning a pointer to the emxArray.

  • If the length of the initialization function name exceeds the maximum number of characters set for function names in the configuration settings, MATLAB Coder prepends an identifier to the front of the function name. MATLAB Coder then truncates the function name to the maximum allowed number of characters for identifier length.

    Note

    By default, the maximum number of characters allowed for generated identifiers is 31. To specify the value set for the maximum identifier length using the MATLAB Coder app, select the Maximum identifier length value on the Code Appearance tab of the code generation settings. To specify the value set for the maximum identifier using the command-line interface, change the value of the MaxIdLength configuration object setting.

Entry-Point Functions

This section defines a function for each MATLAB entry-point function. For a MATLAB function foo.m, the example main source file defines an entry-point function main_foo. This function creates the variables and calls the data initialization functions that the C/C++ source function foo.c or foo.cpp requires. It calls this C/C++ source function but does not return the result. Modify main_foo so that it takes inputs and returns outputs as required by your application.

Main Function

This section defines a main function that does the following:

  • If your output language is C, it declares and names the variables argc and argv but casts them to void. If your output language is C++, the generated example main declares, but does not name, the variables argc and argv.

  • Calls each of the entry-point functions once.

  • Calls the terminate function foo_terminate, which is named for the first MATLAB entry-point function foo declared for code generation. Call the terminate function only once, even if you have multiple entry-point functions called in the function main.

  • Returns zero.

By default, the example main function does not call the initialize function foo_initialize. The code generator includes a call to the initialize function at the beginning of the generated C/C++ entry-point functions. The generated code also includes checks to make sure that the initialize function is called automatically only once, even when there are multiple entry-point functions.

You can choose to not include a call to the initialize function in the generated entry-point functions. To make this choice, do one of the following:

If you make this choice, the example main function includes a call to the initialize function foo_initialize.

See Use Generated Initialize and Terminate Functions.

Modify the function main, including the inputs and outputs of main and of the entry-point functions, to meet the requirements of your application.

Contents of the File main.h

For the example main header file main.h, MATLAB Coder generates the following:

By default, MATLAB Coder also generates comments in main.h that can help you modify the example main function to use in your application.

Include Guard

main.h uses an include guard to prevent the contents of the file from being included multiple times. The include guard contains the include files and function declarations within an #ifndef construct.

Include Files

main.h includes the header files required to call code that is not defined within it.

Function Declarations

main.h declares the function prototype for the main function that is defined in the example main source file main.c or main.cpp.

Related Examples

More About