Choosing Your Target

Types of Targets

Before compiling your M-file with Embedded MATLAB™ Coder, you must choose the appropriate target for code generation. Embedded MATLAB Coder provides reserved words that represent each possible target.

To Generate:Use Target Reserved Word:
C-MEX functionmex

(the default)

Embeddable C code and compile it to an executable

rtw:exe
Embeddable C code and compile it to a libraryrtw:lib
or
rtw

Specifying the Target to the Compiler

After choosing your target, invoke Embedded MATLAB Coder with the -T option and pass the appropriate reserved word as its argument (see -T Specify Target for emlc in the Real-Time Workshop® Reference). For example, suppose you have a primary function called MyFcn. The following table shows how to specify different targets when compiling MyFcn:

To Generate:Use This Command:
C-MEX functionemlc -T mex MyFcn
or
emlc MyFcn
(translates M-function to C-MEX function by default)
Embeddable C code and compile it to an executable
rtwcfg = emlcoder.RTWConfig;
rtwcfg.CustomSource = 'main.c';
rtwcfg.CustomInclude = 'c:\myfiles';
emlc -T rtw:exe -s rtwcfg MyFcn

Embeddable C code and compile it to a libraryemlc -T rtw:lib MyFcn
or
emlc -T rtw MyFcn

Relationship of Targets and Configuration Objects

There is a relationship between targets and configuration objects. For example, there is a Real-Time Workshop configuration object, emlcoder.RTWConfig, that allows you to define properties for the rtw:lib and rtw:exe targets, that is, for generating embeddable code as a C library or executable. The best practice is to match your configuration object with your target when you invoke Embedded MATLAB Coder, as in this example:

rtwcfg = emlcoder.RTWConfig;
emlc -T rtw:lib -s rtwcfg myMFcn

However, if you do not specify a target, but do provide a configuration object, Embedded MATLAB Coder determines the target from the configuration object. For example, consider these sample commands:

rtwcfg = emlcoder.RTWConfig;
emlc -s rtwcfg myMFcn

Normally, when you do not specify a target explicitly, Embedded MATLAB Coder assumes the default target, mex, and generates C-MEX code. However in this case, Embedded MATLAB Coder assumes the desired target is rtw:lib, based on the configuration object rtwcfg, and therefore, generates embeddable C code as a library instead of generating C-MEX code.

For more information about configuration objects, see Configuring Your Environment for Code Generation. For more information about invoking Embedded MATLAB Coder, see Compiling Your M-File.

Location of Generated Files

By default, Embedded MATLAB Coder generates files in output directories based on your target, as follows:

TargetDefault Output Directory
mexemcprj/mexfcn/function_name
rtw:exeemcprj/rtwexe/function_name
rtw:lib or rtwemcprj/rtwlib/function_name

Specifying main Functions for C Executables

When you choose rtw:exe as your target, you must provide a C file that contains the main function for generating your C executable. You can specify the C file by using the CustomSource and CustomInclude properties of the Real-Time Workshop configuration object (see Configuring Your Environment for Code Generation). The CustomInclude property indicates the location of C files specified by CustomSource.

To specify a main function for a C executable, follow these steps:

  1. Create a Real-Time Workshop configuration object, as in this command:

    rtwcfg = emlcoder.RTWConfig;
  2. Set the CustomSource property to the name of the C source file that contains the main function, as in this command:

    rtwcfg.CustomSource = 'main.c';
  3. Set the CustomInclude property to the location of main.c, as in this command:

    rtwcfg.CustomInclude = 'c:\myfiles';
  4. Generate the C executable using the appropriate command line options, as in this example:

    emlc -T rtw:exe -s rtwcfg myMFunction

    Embedded MATLAB Coder compiles and links main.c with the C code it generates from myMFunction.m.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS