Global var

Discussions related to the firmware code development
Post Reply
JP7NJT
Posts: 10
Joined: Wed Oct 18, 2023 12:44 pm

Global var

Post by JP7NJT » Thu Dec 07, 2023 10:39 pm

Hello everyone,

I would like to use a global variable that would be visible in all the code, do you know in which file I should define my variable?

JP7NJT

VK3KYY
Posts: 7590
Joined: Sat Nov 16, 2019 3:25 am
Location: Melbourne, Australia

Re: Global var

Post by VK3KYY » Fri Dec 08, 2023 2:02 am

JP7NJT wrote:
Thu Dec 07, 2023 10:39 pm
Hello everyone,

I would like to use a global variable that would be visible in all the code, do you know in which file I should define my variable?

JP7NJT
The code is partially modular and you don't normally expect to have variables that every single function has access to.

Most variables are declared as static to individual files, to deliberately limit their scope.

This is normal / good programming practice, to limit variability scope.

If you need a variable that is visible to every function, you will probably need to making a new .c file and a new .h and then include that new .h in every file that needs access to your 'global' variable

JP7NJT
Posts: 10
Joined: Wed Oct 18, 2023 12:44 pm

Re: Global var

Post by JP7NJT » Mon Dec 11, 2023 10:27 pm

VK3KYY wrote:
Fri Dec 08, 2023 2:02 am
JP7NJT wrote:
Thu Dec 07, 2023 10:39 pm
Hello everyone,

I would like to use a global variable that would be visible in all the code, do you know in which file I should define my variable?

JP7NJT
The code is partially modular and you don't normally expect to have variables that every single function has access to.

Most variables are declared as static to individual files, to deliberately limit their scope.

This is normal / good programming practice, to limit variability scope.

If you need a variable that is visible to every function, you will probably need to making a new .c file and a new .h and then include that new .h in every file that needs access to your 'global' variable
Hello, thanks for you reply.
I tried to create a global variable in a .h file.
It works when I include this variable in a single.c file but in multiple file, there is errors :
application/include/globalvar.h:2: first defined here.
On the other hand, if I add the variable in static then I can put it in as many.c files as I want.

But I want a volatile variable, not a static variable. I see that there are these kinds of variables like for example uiDataGlobal.Scan. which is visible in all .c files. How do you create this kind of variable?

VK3KYY
Posts: 7590
Joined: Sat Nov 16, 2019 3:25 am
Location: Melbourne, Australia

Re: Global var

Post by VK3KYY » Mon Dec 11, 2023 10:46 pm

JP7NJT wrote:
Mon Dec 11, 2023 10:27 pm
VK3KYY wrote:
Fri Dec 08, 2023 2:02 am
JP7NJT wrote:
Thu Dec 07, 2023 10:39 pm
Hello everyone,

I would like to use a global variable that would be visible in all the code, do you know in which file I should define my variable?

JP7NJT
The code is partially modular and you don't normally expect to have variables that every single function has access to.

Most variables are declared as static to individual files, to deliberately limit their scope.

This is normal / good programming practice, to limit variability scope.

If you need a variable that is visible to every function, you will probably need to making a new .c file and a new .h and then include that new .h in every file that needs access to your 'global' variable
Hello, thanks for you reply.
I tried to create a global variable in a .h file.
It works when I include this variable in a single.c file but in multiple file, there is errors :
application/include/globalvar.h:2: first defined here.
On the other hand, if I add the variable in static then I can put it in as many.c files as I want.

But I want a volatile variable, not a static variable. I see that there are these kinds of variables like for example uiDataGlobal.Scan. which is visible in all .c files. How do you create this kind of variable?
This a C language programming question not specific to the firmware. You should post it to a C language programming forum.

Post Reply