From RTSC-Pedia
revision tip
LANDSCAPE orientationFAQ-081229-1
How can I insert my own C code into the configuration generated .c file
Any module can contribute C source code to the generated .c file that the configuration process generates. The code that is placed in this file will become part of the applications that use this configuration. Modules specify the C code they want to include via the @Template attribute. In the example below, we show all the files necessary to create a package named mypackage that contains a module named MyMod which generates some C statements that become part of any configuration that uses the mypackage.MyMod module.
| package.xdc |
| package mypackage {
module MyMod;
} |
| | MyMod.xdc |
| @Template("myCode.xdt")
module MyMod {
} |
| | package.bld |
| /* nothing needed here */ |
|
The C code to be generated is specified via a template file named "myCode.xdt". This template is expanded in the context of the configuration model and can refer to any modules loaded as part of the configuration process.
Although arbitrary JavaScript statements and values can be used within a template, in the example below, we simply specify a global string variable named myString initialized to a constant value. For a brief description of the syntax and some simple examples of template files see Methods for generating other files in the The XDCscript Language User's Guide.
| myCode.xdt |
| const char *myString = "hello from mypackage.MyMod"; |
To cause this template to be included in a configuration, the configuration script must "use" the mypackage.MyMod module.
| app.cfg |
| var MyMod = xdc.useModule("mypackage.MyMod"); |
Whenever mypackage.MyMod is used in a configuration, the string myString declared by myCode.xdt can be referenced from your application as illustrated below.
| app.c |
| #include <xdc/runtime/System.h>
extern char *myString;
int main(int argc, char *argv[]) {
System_printf("%s\n", myString);
return (0);
} |
| | output |
| hello from mypackage.MyMod |
|
Copyright © 2008 The Eclipse Foundation. All Rights Reserved