undefined reference error - driving me crazy

I've been programming/building Arduino projects on and off for many years but never got stuck like this...

I am modifying some code which is not my own but have studied it extensively. There are many modules so I will try to summarize the (simple) change I am making to the code and summarize where all the related/supporting code lives, highlighting the lines which produce error messages. All of this code is cut/paste so there or no UPPER/lower case errors.

I am adding a new boolean (bool) flag, "scubaMode", and then test an established input to set or clear (true or false) the variable. In another part of the code, I look at this flag to set another variable. No matter where or how I define the variable "scubaMode" (bool, extern bool, static bool, uint_7, int) I either get an undefined in scope error or an undefined reference error. Certainly this can't be that hard, but I have spent four hours on it now and gotten nowhere with it. Any help is very much appreciated.

Here are the relevant snippets, each with a "where it is in the code" label. I have placed the verify/compile errors on the relevant lines of code in the comment field.

in alternator.h
extern bool tachMode;
extern bool scubaMode;

in system.cpp
#if defined (OSE_ALTERNATOR) ||
defined (OSE_GENERATOR)
#include "Alternator.h"

** in config.h**
#define OSE_ALTERNATOR

Later in system.cpp
#ifdef FEATURE_IN_SCUBA
if (feature_in(false)==true)
scubaMode = true //ERROR: sketch/System.cpp:332: undefined reference to scubaMode' else scubaMode = false; //ERROR: sketch/System.cpp:334: undefined reference to scubaMode'
#endif

** in alternator.cpp **
#include "Alternator.h"

**later in alternator.cpp **
if (tachMode) //NO ERROR HERE FOR tachMode
fieldPWMvalue = max(fieldPWMvalue, thresholdPWMvalue);

if(scubaMode) //ERROR: sketch/Alternator.cpp:1413: undefined reference to `scubaMode'
fieldPWMvalue = FIELD_PWM_SCUBA;

// END OF CODE SNIPPETS
Thank you!

Where did you write

bool ScubaMode;

?

I tried just using

bool scubaMode;

in config.h, alternator.h, and other header files and had no luck with that so I tried the extern reference.

Are you hinting that I also need to have

bool scubaMode;

in another header file, such as, in this case, whereever I might find

bool tachMode;

?

Thanks!

A global variable must be DECLARED in every file whose contained functions need access to it. This is typically done via an 'extern' declaration in an .h file that gets #include(d).

However, the variable must be DEFINED in one and only one file. That, of course, means that a .h file is absolutely the wrong place to do this as it will result in multiple definitions of the variable. Instead, it must be defined in one (and only one) .cpp or .c file.

If you don't know the difference, see:

Thanks for setting me straight (kindly). I realize now that my projects have been more consolidated and thus I never had to use the extern declaration for a variable. The code I am modifying has evolved over many years and is leverageable for multiple hardware platforms and functionalities so it is really huge and quite sprawling. Maybe I will consolidate it down to just the parts I really need.

thanks again!