[SOLVED] Glabal variables, new c files, etc

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] Glabal variables, new c files, etc

    Hello all

    After several weeks of using the Segger EM studio and nRF52DK I have finally got to see some results !

    Anyway, I have basic problems, I guess this is normal, as far as I have read about the global variables. For example, I have created the global variables within the main, where the access to the sensor is made (EG. Word Data1, Data2, Data3). This works just fine when I am within the range of main(), however, to keep it nice and neatly, I have started to move the code to separate files , within the Application folder, like creating a new file (*.c) ... But no luck, I am not sure how come I cannot use the globally declared variables from the main() in other functions outside the main file ? Am I missing how the global variables should be declared? Anyway, how to avoid this in anyway?

    Any ideas?

    Thanks and all the best!
  • Hi Mueta,

    To use global variables in different modules you have to extern the variables.
    The easiest way to do is, to declare the global variable in a header file.
    The following snippet shows you how to do that.
    Because it is a standard c questions, you will find also a answer on stackoverflow or similar platforms.

    In your main.c you have to define if it is the real main.c, you can do that like this at the top of your file, you also have to include the header file wich is interface for the modules.

    C Source Code

    1. #define MAIN_C
    2. #include "main.h"

    now you have to take a look at your header file wich is the interface for your modules:

    Source Code

    1. #ifndef MAIN_H
    2. #define MAIN_H
    3. #ifdef MAIN_C
    4. #define EXTERN
    5. #else
    6. #define EXTERN extern
    7. #endif
    8. EXTERN int MyGlobal;
    9. #endif
    Display All

    My second module is the app.c where i include also my header.
    Now i can just work with my global variable in multiple modules.
    In my example:

    (app.c)

    C Source Code

    1. #include "main.h"
    2. int foo(void) {
    3. MyGlobal++;
    4. return MyGlobal;
    5. }

    (main.h)

    Source Code

    1. #ifndef MAIN_H
    2. #define MAIN_H
    3. #ifdef MAIN_C
    4. #define EXTERN
    5. #else
    6. #define EXTERN extern
    7. #endif
    8. EXTERN int MyGlobal;
    9. int foo(void);
    10. #endif
    Display All

    (main.c)

    C Source Code

    1. /*********************************************************************
    2. * SEGGER Microcontroller GmbH *
    3. * The Embedded Experts *
    4. **********************************************************************
    5. -------------------------- END-OF-HEADER -----------------------------
    6. File : main.c
    7. Purpose : Generic application start
    8. */
    9. #define MAIN_C
    10. #include <stdio.h>
    11. #include <stdlib.h>
    12. #include "main.h"
    13. /*********************************************************************
    14. *
    15. * main()
    16. *
    17. * Function description
    18. * Application entry point.
    19. */
    20. int main(void) {
    21. int i;
    22. for (i = 0; i < 100; i++) {
    23. printf("Hello World %d!\n", foo());
    24. printf(MyGlobal);
    25. }
    26. do {
    27. i++;
    28. } while (1);
    29. }
    30. /*************************** End of file ****************************/
    Display All


    Here you can find also a good Explanation: riptutorial.com/c/example/3251/using-a-global-variable
    Regards,

    Lucas
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longerperiods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket
    Or you can contact us via e-mail.
  • Hello Lucas,
    thank you for your reply, I have tried what you are suggesting to do, but no luck. Would you please say more about the second step?
    Namely where I find the "header file" that you are talking about here:

    #ifndef MAIN_H
    #define MAIN_H

    #ifdef MAIN_C
    #define EXTERN
    #else
    #define EXTERN extern
    #endif

    EXTERN int MyGlobal;


    #endif

    Thank you and I am looking forward to your reply.

    Best
  • Hello,

    Please understand that this topic has nothing to do with SEGGER products and is thus out of our support scope.
    When using our products we assume that you have basic understanding of the C language. If not we recommend to visit one of the many excellent C coding references in the internet or check out some online courses.

    The thread will be closed now as it has nothing to do with SEGGER products.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.