FAQ-081229-2

From RTSC-Pedia

Jump to: navigation, search
revision tip
—— LANDSCAPE orientation
[printable version]  [offline version]offline version generated on 18-Nov-2017 00:11 UTC

FAQ-081229-2

How do I define C language global variables within a configuration script

The special Program.global configuration parameter can be used to define simple global variables accessible from within C code.

In the example below, we define a global variable named myString and initializes it to "hello world".

app.cfg
 
Program.global.myString = "hello world";

The global variable myString defined by app.cfg above can be referenced from your application as illustrated below.

app.c
 
 
 
 
 
 
 
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>
 
int main(int argc, char *argv[]) {
    System_printf("%s\n", myString);
    return (0);
}
output
 
hello world

The xdc/cfg/global.h header file defines the type of all global variables declared in app.cfg. This has the advantage that the C compiler can catch discrepancies between what is defined by app.cfg and what is used in your C code. But, to make this possible you must pass an additional -D option on the compile line of you application sources; for more details see Portable Inclusion of Configuration Constants within the Integrating RTSC Modules topic.

Alternatively, if you are willing to give up on type safety, you can simply add an appropriate extern to your sources (and hope that app.cfg and your C code never gets out of sync).

app.c
 
 
 
 
 
 
 
#include <xdc/runtime/System.h>
extern char *myString;
 
int main(int argc, char *argv[]) {
    System_printf("%s\n", myString);
    return (0);
}
[printable version]  [offline version]offline version generated on 18-Nov-2017 00:11 UTC
Copyright © 2008 The Eclipse Foundation. All Rights Reserved
Personal tools
package reference