This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/TM4C1294NCPDT: global variables in ti rtos

Part Number: TM4C1294NCPDT


Tool/software: TI-RTOS

respected sir,

I am trying to interface proface HMI  to tm4c1294ncpdt by modifying uartecho_EK_TM4C1294XL_TI_TivaTM4C1294NCPDT (TI-RTOS) project. I have added two files named proface.h and proface.c into this project. I am trying to pass data from uartecho.c file to proface.c and vice versa. But there are some errors. I have included the proface.h file in both the remaining files.

1. In first scenario if i define the variables in  proface.c file , it is giving error for uartecho.c (undefined symbols) file.

2. In second scenario if i define the variables in uartecho.c  file , it is giving error for proface.c (undefined symbols) file.

3. In third scenario if i define the variables in proface.h  file , it is giving error as shown in attached snap.

So how do i define global variables in TI-RTOS??

regards,

digvijay

  • Hello Digvijay,

    When you need to use the same global variable across multiple files, only one file should have the standard global variable definition (which should be the file you want it to originate from) and all other files that has the variable should use the 'extern' keyword to declare the variable as available for that file.

    So in one of your files, you will want to define the variable such as:

    uint16_t g_ui16MyVarible;

    And then for all files which need to use it, you would declare that variable as available by using the 'extern' keyword:

    extern uint16_t g_ui16MyVarible;

    This should allow you to get your global variable properly defined across both your files.

    This same feature can be applied to functions as well, such as how our TivaWare examples share ISR functions from the main file with the startup_ccs.c file for the interrupt vector table. Though typically a header file to declare the functions is a more conventional use case to share functions across files, but it is possible to use extern when it makes sense such as with the startup_ccs.c file.

    Here is also a helpful thread from stackoverflow about this, in case you want to read further: https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files

  • Respected sir,

    But why i cant declare the variable in proface.h file and then include this file in the whichever file i want??  Defining each variable in every file (with and without extern) is not a good way of programming. What i want to do is put all the variables required for project into one  .h file and then include this file wherever i want to use those variables. But this is giving error as shown in above snap. So i do i achieve this objective??

    regards,

    digvijay

  • Hello Digvijay,

    I believe you can do what you are seeking through using extern within the header file. Reference the stackoverflow post I provided in my first posting, it has far more detail about the do's and dont's of what you are seeking than I can go into. From what it sounds like the extern feature in a .h file will give you what you desire as well. I just am used to method I described (seems that's a common TI approach?).