Warning: 'buttonCount' initialized and declared 'extern'

Hi all,

i store my global variables in variables.h and declare them "extern". I find it's an easy way to have all of my global variables in one place while making them accessible to the rest of my sketch.

One issue i have with that is when i go to compile it gives a warning for each variable which has been declared "extern". I have to go through this list manually to see if there are any other errors in there which are actually relevant to me.

Is there a way for me to get the Arduino IDE to ignore compile warnings for variables declared as "extern" so i don't have to manually check the list every time i compile?

I'm using the latest version of the Arduino IDE v2.0.3.

Any help would be greatly appreciated.

Cheers

NM

Why do you declare them 'extern'? Declaring them 'extern' means that the compiler thinks they are defined elsewhere and not in the actual sketch. Where do you define them?
If you include 'variables.h' only in your sketch, there is no need to declare them extern. Without the 'extern' keyword the variables are declared and defined in place.
You need to declare them as 'extern' in the .h file if you include variables.h in several .cpp files. But then you have to define them in another place - usually in one of the .cpp files. You can initialze a variable only where it is defined, not where it is declared only.

You should not ignore warnings - mostly that are errors in reality - even if it compiles.

Why not put them in one place above setup() then ? :thinking:

I only put them in variables.h if i have a lot of variables. I don't like them being before setup if there is a lot of them.

If i don't declare them "extern" in the variables.h file then the sketch doesn't work. I got the idea from another post in this forum a while ago.

I'm not an expert at any of this, just bumbling my way through mostly.

This is how i usually declare them in variables.h

extern uint8_t currDayNumber = 0;

and they are then available in the rest of the sketch. If i don't do that then they aren't available globally. I don't know a lot about C and don't really understand what the differences is between the .h, .cpp files etc

Cheers

p.s. is there another way to have a separate file for global variables?

Put them in a XXXX.h file in the same directory as your sketch.

They will appear in a tab in the IDE.

Please show exactly how you do that. Where are your files located? How do you include the file in your sketch? Show an example.

You cannot initialize an external variable.


I create variables.h file which is in the same directory as the sketch. I include it using the line

#include "variables.h"

Cheers

Does this mean that the warnings i am getting is because i have initialised a "extern" variable?

Then you don’t need to add external at all.

I just updated a sketch i am working on by removing the "extern" declaration on all of the vars in the variables.h file and it compiled fine. It didn't in the past though. Not sue why that was, but it all seems to be good and it's also solved at least one other problem i had.

Thanks for your help MicroBahner and LarryD. I would have been stuck doing it wrong for ever if i hadn't asked for advice.

Cheers

NM :grinning:

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.