Main Content

Customize Template Makefiles

To configure or customize a template makefile (TMF), you should be familiar with how the make command works and how it processes a makefile (.mk file). You should also understand makefile build rules. For information on these topics, refer to the documentation provided with the make utility that you use.

Template Makefiles and Tokens

A template makefile contains tokens. The build process expands the tokens and creates makefiles:

  • –– Compiles and links code generated from model components.

  • –– Compiles generated shared utility code.

The makefiles ( use commands that are specific to your development computer.

Template Makefile Tokens

The make_rtw command (or a different command provided with some targets) directs the process of generating The make_rtw command processes the TMF specified on the Code Generation pane of the Configuration Parameters dialog box. make_rtw copies the TMF, line by line, expanding each token encountered. Template Makefile Tokens Expanded by make_rtw lists the tokens and their expansions.

These tokens are used in several ways by the expanded makefile:

  • To control the conditional behavior in the makefile. The conditionals are used to control the source file lists, library names, target to be built, and other build-related information.

  • To provide the macro definitions for compiling the files, for example, -DINTEGER_CODE=1.

Template Makefile Tokens Expanded by make_rtw

General purpose


Linker flags automatically added by blocks.


Alternate full pathname for the MATLAB® executable; value is different than value for MATLAB_BIN token when the full pathname contains spaces.


Alternate full pathname for the MATLAB installation; value is different than value for MATLAB_ROOT token when the full pathname contains spaces.


Options passed to make_rtw. This token is provided so that the contents of your file change when you change the build arguments, thus forcing an update of modules when your build options change.


True (1) when you select model configuration parameter Single output/update function, otherwise False (0). Used for the macro definition -DONESTEPFCN=1.


Compiler flags in a group other than 'OPTS', 'OPT_OPTS', and 'OPTIMIZATION_FLAGS'. The build process produces a warning if |>COMPILE_FLAGS_OTHER<| is absent and these conditions apply:

  • The build information contains a compiler flag that is not in 'OPTS', 'OPT_OPTS', or 'OPTIMIZATION_FLAGS'.

  • The compiler flag is not in the template makefile.


Computer type. See the MATLAB computer command.


Preprocessor macro definitions in a group other than 'OPTS', 'OPT_OPTS', 'OPTIMIZATION_FLAGS', and 'Custom'.

A preprocessor macro definition in 'OPTS', 'OPT_OPTS', 'OPTIMIZATION_FLAGS', or 'Custom' that has a corresponding variable/token pair in the template makefile is not expanded in DEFINES_OTHER. This behavior helps to avoid the provision of duplicate preprocessor macro definitions.

For example, some template makefiles contain these statements:

If the template makefile contains the variable/token pair NUMST = |>NUMST<| and NUMST is a preprocessor macro definition in buildInfo, then the build process does not expand it in DEFINES_OTHER.

The build process produces a warning if |>DEFINES_OTHER<| is absent and these conditions apply:

  • The build information contains a preprocessor macro definition that is not in 'OPTS', 'OPT_OPTS', 'OPTIMIZATION_FLAGS', or 'Custom'.

  • The preprocessor macro definition, for example, NUMST, is not in the template makefile.


Location of precompiled library file. The TargetPreCompLibLocation configuration parameter can override this setting. For examples, see Control Library Location and Naming During Build.


Library name. For examples, see Control Library Location and Naming During Build and Modify the Template Makefile for rtwmakecfg.


Library suffix. The TargetLibSuffix configuration parameter can override this setting. For examples, see Control Library Location and Naming During Build.

|>EXT_MODE<| (Not required for R2018a and later provided template makefile specifies TOOLCHAIN_NAME)

True (1) to enable generation of external mode support code, otherwise False (0).

|>EXTMODE_TRANSPORT<| (Not required for R2018a and later provided template makefile specifies TOOLCHAIN_NAME)

Index of transport mechanism (for example, tcpip, serial) for external mode.

|>EXTMODE_STATIC<| (Not required for R2018a and later provided template makefile specifies TOOLCHAIN_NAME)

True (1) if static memory allocation is selected for external mode. False (0) if dynamic memory allocation is selected.

|>EXTMODE_STATIC_SIZE<| (Not required for R2018a and later provided template makefile specifies TOOLCHAIN_NAME)

Size of static memory allocation buffer for external mode.


True (1) when model configuration parameter Create block is set to SIL, otherwise False (0). Used for control of the makefile target of the build.


True (1) when model configuration parameter Terminate function required is selected, otherwise False (0). Used for the macro definition -DTERMFCN==1.


True (1) when model configuration parameter Support floating-point numbers is cleared, otherwise False (0). INTEGER_CODE is a required macro definition when compiling the source code and is used when selecting precompiled libraries to link against.

|>MAKEFILE_NAME<| — The name of the makefile that was created from the TMF.


True (1) when model configuration parameter MAT-file logging is selected, otherwise False (0). MAT_FILE is a required macro definition when compiling the source code and also is used to include logging code in the build process.


Location of the MATLAB executable program.


Path to where MATLAB is installed.


RT_MALLOC or RT_STATIC. Indicates how memory is to be allocated.


MEX-file extension. See the MATLAB mexext command.


Additional generated source modules. For example, you can split a large model into two files, model.c and model1.c. In this case, this token expands to model1.c.


Object filenames (.obj) corresponding to additional generated source modules.


Name of the Simulink® block diagram currently being built.


True (1) if solver mode is multitasking, otherwise False (0).


Number of continuous states.


Number of sample times in the model.


The MATLAB release version.


List of S-function libraries available for linking.


Solver source filename, for example, ode3.c.


Solver object (.obj) filename, for example, ode3.obj.


Code generation folder at the start of the build.


c when model configuration parameter Language is set to C, cpp when Language is set to C++. Used in the makefile to control the extension on generated source files.


True (1) if sampling rates of the continuous task and the first discrete task are equal, otherwise False (0).

S-function and build information support


For examples of the tokens in this section, see Modify the Template Makefile for rtwmakecfg.


List of folder names to add to the include path. Additionally, the ADD_INCLUDES macro must be added to the INCLUDES line.


List of library names.


Library module names within |>START_EXPAND_LIBRARIES<| and |>START_PRECOMP_LIBRARIES<| library lists.


Makefile rules.


List of precompiled library names.

Model reference support


For examples of the tokens in this section, see Providing Model Referencing Support in the TMF.


Name of the library file generated for the current model.


List of models referenced by the top model.


List of referenced model libraries against which the top model links.


Name of a response file against which the top model links. This token is valid only for build environments that support linker response files. For an example of its use, see matlabroot/toolbox/coder/compile/tmf/grt_vcx64.tmf.


Type of target being built. Possible values are

  • NONE: Standalone model or top model referencing other models

  • RTW: Model reference target build

  • SIM: Model reference simulation target build


Relative path, from the location of the generated makefile, to the MATLAB working folder.

These tokens are expanded by substitution of parameter values known to the build process. For example, if the source model contains blocks with two different sample times, the TMF statement


expands to:


In addition to the above, make_rtw expands tokens from other sources:

  • Target-specific tokens defined in the target options of the Configuration Parameters dialog box

  • Structures in the rtwoptions section of the system target file. Structures in the rtwoptions structure array that contain the field makevariable are expanded.

The following example is extracted from matlabroot/rtw/c/grt/grt.tlc. The section starting with BEGIN_RTW_OPTIONS contains MATLAB code that sets up rtwoptions. The following directive causes the |>EXT_MODE<| token to be expanded to 1 (on) or 0 (off), depending on how you set the external mode options.

rtwoptions(2).makevariable = 'EXT_MODE'

Invoke the make Utility

make Command

After creating from your TMF, the build process invokes a make command. To invoke make, the build process issues this command.

makecommand -f

makecommand is defined by the MAKECMD macro in your system target file TMF (see Structure of the Template Makefile). You can specify additional options to make by using the model configuration parameter Make command. (See the sections Specify a Make Command and Template Makefiles and Make Options.)

For example, specifying OPT_OPTS=-O2 in the Make command field causes make_rtw to generate the following make command.

makecommand -f OPT_OPTS=-O2

A comment at the top of the TMF specifies the available make command options. If these options do not provide you with enough flexibility, you can configure your own TMF.

make Utility Versions

The make utility lets you control nearly every aspect of building your real-time program. There are several different versions of make available. The code generator provides the Free Software Foundation GNU® make for both UNIX®1 and PC platforms in platform-specific subfolders under


It is possible to use other versions of make with the code generator, although GNU Make is recommended. To be compatible with the code generator, verify that your version of make supports the following command format.

makecommand -f

Structure of the Template Makefile

A TMF has multiple sections, including the following:

  • Abstract — Describes what the makefile targets. Here is a representative abstract from the ERT TMFs in matlabroot/toolbox/coder/compile/tmf:

    # Abstract:
    #       Template makefile for building a Windows-based stand-alone embedded
    #       real-time version of Simulink model using generated C code and the
    #          Microsoft Visual C/C++ compiler for x64.
    #       Note that this template is automatically customized by the build 
    #       procedure to create "<model>.mk"
    #       The following defines can be used to modify the behavior of the
    #       build:
    #         OPT_OPTS       - Optimization option. See DEFAULT_OPT_OPTS in
    #                          vctools.mak for default.
    #         OPTS           - User specific options.
    #         CPP_OPTS       - C++ compiler options.
    #         USER_SRCS      - Additional user sources, such as files needed by
    #                          S-functions.
    #         USER_INCLUDES  - Additional include paths
    #                          (i.e. USER_INCLUDES="-Iwhere-ever -Iwhere-ever2")
    #       To enable debugging:
    #         set DEBUG_BUILD = 1, which will trigger OPTS=-Zi (may vary with
    #                               compiler version, see compiler doc) 
    #       This template makefile is designed to be used with a system target
    #       file that contains 'rtwgensettings.BuildDirSuffix' see ert.tlc
  • Macros read by make_rtw section — Defines macros that tell make_rtw how to process the TMF. Here is a representative Macros read by make_rtw section from the GRT TMFs in matlabroot/toolbox/coder/compile/tmf:

    #------------------------ Macros read by make_rtw -----------------------------
    # The following macros are read by the build procedure:
    #  MAKECMD         - This is the command used to invoke the make utility
    #  HOST            - What platform this template makefile is targeted for
    #                    (i.e. PC or UNIX)
    #  BUILD           - Invoke make from the build procedure (yes/no)?
    #  SYS_TARGET_FILE - Name of system target file.
    MAKECMD         = nmake
    HOST            = PC
    BUILD           = yes
    BUILD_SUCCESS	= ^#^#^# Created
    # Opt in to simplified format by specifying compatible Toolchain
        "Microsoft Visual C++ 2019 v16.0 | nmake (64-bit Windows)", \
        "Microsoft Visual C++ 2017 v15.0 | nmake (64-bit Windows)", \
        "Microsoft Visual C++ 2015 v14.0 | nmake (64-bit Windows)"]

    The macros in this section might include:

    • MAKECMD — Specifies the command used to invoke the make utility. For example, if MAKECMD = mymake, then the make command invoked is

      mymake -f

    • HOST — Specifies the platform targeted by this TMF. This can be PC, UNIX, computer_name (see the MATLAB computer command), or ANY.

    • BUILD — Instructs make_rtw whether or not it should invoke make from the build procedure. Specify yes or no.

    • SYS_TARGET_FILE — Specifies the name of the system target file or the value any. This is used for consistency checking by make_rtw to verify the system target file specified in the Target selection panel of the Code Generation pane of the Configuration Parameters dialog box. If you specify any, you can use the TMF with any system target file.

    • BUILD_SUCCESS — When the makefile creates a static library, it produces the BUILD_SUCCESS string as defined, for example, by this rule in ert_vcx64.tmf:

      	@cmd /C "echo ### Linking ..."
      	$(LD) $(LDFLAGS) $(LIBS) \
          @$(CMD_FILE) @$(MODELREF_LINK_RSPFILE) -dll -def:$(MODEL).def -out:$@
      	@cmd /C "echo $(BUILD_SUCCESS) dynamically linked library  $(PRODUCT)"

      When the build process finds the BUILD_SUCCESS string, it polls for the existence of the updated static library file. The build process does not link the library file to the executable file until the build process can confirm the presence of the library file, with an updated timestamp, on the file system.

      If the build process does not find BUILD_SUCCESS in the compilation log, then the build process assumes that the updated library archive is available on the file system. If required, the build process links the library to the executable file.

  • Tokens expanded by make_rtw section — Defines the tokens that make_rtw expands. Here is a brief excerpt from a representative Tokens expanded by make_rtw section from the ERT TMFs in matlabroot/toolbox/coder/compile/tmf/:

    #---------------------- Tokens expanded by make_rtw ----------------------------
    # The following tokens, when wrapped with "|>" and "<|" are expanded by the
    # build procedure.
    #  MODEL_NAME          - Name of the Simulink block diagram
    #  MODEL_MODULES       - Any additional generated source modules
    #  MAKEFILE_NAME       - Name of makefile created from template makefile <model>.mk
    #  MATLAB_ROOT         - Path to where MATLAB is installed.
    MODEL                   = |>MODEL_NAME<|
    MODULES                 = |>MODEL_MODULES<|
    PRODUCT                 = |>PRODUCT<|
    MAKEFILE                = |>MAKEFILE_NAME<|
    MATLAB_ROOT             = |>MATLAB_ROOT<|

    For more information about TMF tokens, see Template Makefile Tokens Expanded by make_rtw.

  • Subsequent sections vary based on compiler, host, and target. Some common sections include Model and reference models, External mode, Tool Specifications or Tool Definitions, Include Path, C Flags, Additional Libraries, and Source Files.

  • Rules section — Contains the make rules used in building an executable from the generated source code. The build rules are typically specific to your version of make. The Rules section might be followed by related sections such as Dependencies.

Customize and Create Template Makefiles


This section describes the mechanics of setting up a custom template makefile (TMF) and incorporating it into the build process. It also discusses techniques for modifying a TMF and MATLAB file mechanisms associated with the TMF.

Before creating a custom TMF, you should read Folder and File Naming Conventions to understand the folder structure and MATLAB path requirements for custom targets.

Setting Up a Template Makefile

To customize or create a new TMF, copy an existing GRT or ERT TMF from matlabroot/toolbox/coder/compile/tmf.

Place the copy in the same folder as the associated system target file. Usually, this is the mytarget/mytarget folder within the target folder structure. Then, rename your TMF (for example, mytarget.tmf) and modify it.

To allow the build process to locate and select your TMF, you must provide information in the system target file file header (see System Target File Structure). For a target that implements a single TMF, the standard way to specify the TMF to be used in the build process is to use the TMF directive of the system target file file header.

TMF: mytarget.tmf

Using Macros and Pattern Matching Expressions in a Template Makefile

This section shows, through an example, how to use macros and file-pattern-matching expressions in a TMF to generate commands in

The make utility processes and generates a set of commands based upon dependency rules defined in After make generates the set of commands for building or rebuilding test, make executes them.

For example, to build a program called test, make must link the object files. However, if the object files don't exist or are out of date, make must compile the source code. Thus there is a dependency between source and object files.

Each version of make differs slightly in its features and how rules are defined. For example, consider a program called test that gets created from two sources, file1.c and file2.c. Using most versions of make, the dependency rules would be

test: file1.o file2.o
        cc -o test file1.o file2.o

file1.o: file1.c
        cc -c file1.c

file2.o: file2.c
        cc -c file2.c

In this example, a UNIX2 environment is assumed. In a PC environment the file extensions and compile and link commands are different.

In processing the first rule

test: file1.o file2.o

make sees that to build test, it needs to build file1.o and file2.o. To build file1.o, make processes the rule

file1.o: file1.c

If file1.o doesn't exist, or if file1.o is older than file1.c, make compiles file1.c.

The format of TMFs follows the above example. Our TMFs use additional features of make such as macros and file-pattern-matching expressions. In most versions of make, a macro is defined with

MACRO_NAME = value

References to macros are made with $(MACRO_NAME). When make sees this form of expression, it substitutes value for $(MACRO_NAME).

You can use pattern matching expressions to make the dependency rules more general. For example, using GNU3 Make, you could replace the two “file1.o: file1.c” and “file2.o: file2.c” rules with the single rule

%.o : %.c
        cc -c $<

Note that $< in the previous example is a special macro that equates to the dependency file (that is, file1.c or file2.c). Thus, using macros and the "%" pattern matching character, the previous example can be reduced to

SRCS = file1.c file2.c
OBJS = $(SRCS:.c=.o)

test: $(OBJS)
        cc -o $@ $(OBJS)

%.o : %.c
        cc -c $<

Note that the $@ macro above is another special macro that equates to the name of the current dependency target, in this case test.

This example generates the list of objects (OBJS) from the list of sources (SRCS) by using the text substitution feature for macro expansion. It replaces the source file extension (for example, .c) with the object file extension (.o). This example also generalized the build rule for the program, test, to use the special "$@" macro.

Customizing Generated Makefiles with rtwmakecfg

TMFs provide tokens that let you add the following items to generated makefiles:

  • Source folders

  • Include folders

  • Run-time library names

  • Run-time module objects

S-functions can add this information to the makefile by using an rtwmakecfg.m file function. This function is particularly useful when building a model that contains one or more of your S-Function blocks, such as device driver blocks.

To add information pertaining to an S-function to the makefile,

  1. Create the function rtwmakecfg in file rtwmakecfg.m. The code generator associates this file with your S-function based on its folder location.

  2. Modify your target's TMF such that it supports macro expansion for the information returned by rtwmakecfg functions.

After the TLC phase of the build process, when generating a makefile from the TMF, the build process searches for an rtwmakecfg.m file in the folder that contains the S-function component. If it finds the file, the build process calls the rtwmakecfg function. For more information, see Use rtwmakecfg.m API to Customize Generated Makefiles.

Supporting Continuous Time in Custom Targets

If you want your custom ERT-based target to support continuous time, you must update your template makefile (TMF) and the static main program module (for example, mytarget_main.c) for your target.

Template Makefile Modifications.  Add the NCSTATES token expansion after the NUMST token expansion, as follows:


In addition, add NCSTATES to the CPP_REQ_DEFINES macro, as in the following example:


Modifications to Main Program Module.  The main program module defines a static main function that manages task scheduling for the supported tasking modes of single- and multiple-rate models. NUMST (the number of sample times in the model) determines whether the main function calls multirate or single-rate code. However, when a model uses continuous time, do not rely on NUMST directly.

When the model has continuous time and the flag TID01EQ is true, both continuous time and the fastest discrete time are treated as one rate in generated code. The code associated with the fastest discrete rate is guarded by a major time step check. When the model has only two rates, and TID01EQ is true, the generated code has a single-rate call interface.

To support models that have continuous time, update the static main module to take TID01EQ into account, as follows:

  1. Before NUMST is referenced in the file, add the following code:

    #if defined(TID01EQ) && TID01EQ == 1 && NCSTATES == 0
    #define DISC_NUMST (NUMST - 1)
    #define DISC_NUMST NUMST
  2. Replace instances of NUMST in the file by DISC_NUMST.

Model Reference Considerations

See Support Model Referencing for important information on TMF modifications you may need to make to support the code generator model referencing features.


If you are using a TMF without the variable MODELREFS, the file might have been used with a previous release of Simulink software. If you want your TMF to support model referencing, add MODELREFS to the make file.

Timeout Error When Building Shared Utility Code with Custom Template Makefile

If the build process uses a custom template makefile to create a makefile for the shared utility code, the build process produces a timeout error when all of these conditions apply:

  • The configuration parameter GenCodeOnly is 'off'.

  • The BUILD_SUCCESS message is output during the make call to

  • The make call to does not update the final product (typically rtwshared.lib).

To avoid the error, update the custom template makefile so that the make call generates the BUILD_SUCCESS message only when the final product is generated (as identified by the make variable PRODUCT). Alternatively, if the build process does not use the generated makefile, disable makefile generation by setting the configuration parameter GenerateMakefile to 'off'.

Related Topics

1 UNIX is a registered trademark of The Open Group in the United States and other countries.

2 UNIX is a registered trademark of The Open Group in the United States and other countries.

3 GNU is a registered trademark of the Free Software Foundation.