| Real-Time Workshop® | ![]() |
| On this page… |
|---|
Specifying the Target to the Compiler |
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 function | mex (the default) | |
Embeddable C code and compile it to an executable
| rtw:exe | |
| Embeddable C code and compile it to a library | rtw:lib or rtw |
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 function | emlc -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 library | emlc -T rtw:lib MyFcn or emlc -T rtw MyFcn |
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.
By default, Embedded MATLAB Coder generates files in output directories based on your target, as follows:
| Target | Default Output Directory |
|---|---|
| mex | emcprj/mexfcn/function_name |
| rtw:exe | emcprj/rtwexe/function_name |
| rtw:lib or rtw | emcprj/rtwlib/function_name |
Note You can change the name and location of generated files by using the options -o Specify Output File Name and -d Specify Output Directory when you run Embedded MATLAB Coder. |
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:
Create a Real-Time Workshop configuration object, as in this command:
rtwcfg = emlcoder.RTWConfig;
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';
Set the CustomInclude property to the location of main.c, as in this command:
rtwcfg.CustomInclude = 'c:\myfiles';
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.
![]() | Specifying Properties of Primary Function Inputs | Compiling Your M-File | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |