user53592: I have a .h library file in which I want to declare a global variable that can be read in the program loop and in the library's .cpp file.
The question is, is it possible to declare a variable in the .h file and make it global?

user31079: You can declare the variable as a static variable inside an inline function, and then just call this function to get a reference to this variable. Example:
inline int& getMyInteger()
{
    static int x;
    return x;
}

This definition can be in a header file, included into multiple *.cpp files, and any call to getMyInteger will return reference to the same object. You don't need any extern, nor to actually put the variable in any compilation unit as you would need with a simple namespace-scope variable. This trick just uses the linker's ability to de-duplicate inline functions.
See also: static variables in an inlined function question on StackOverflow.

user37622: .h file is included by the compilation preprocessor as text into the .c or .cpp file. It is included as if you would replace the #include preprocessor instruction with the content of the .h file. (The Arduino .ino file is converted to cpp file before preprocessing.)
After the inclusion the cpp file is compiled to 'object' (.o file). If the cpp file contains a variable definition the variable is defined in this object file. If your .h file contained a variable definition, the it is also in this object file.
If two cpp files included the .h file with the definition of the variable, then the variable will be in two object files and the linker will exit with an error, because of two variables with the same name.
If only one cpp/ino files includes the variable definition, then it is OK. It is not a good practice, but sometimes a necessary compromise. (example of it)
To have one global variable made accessible by a .h file, the variable should be only declared in the .h file and be defined in one cpp file. Usually it is a pair of .h and .cpp file with same name. 
An example of global variable is Serial. It is a global object of type HardwareSerial declared in HardwareSerial.h as 
extern HardwareSerial Serial; 
and defined in HardwareSerial0.cpp as 
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
Do you see the difference? The declaration has the keyword extern and the definition has in this case parameters for the constructor of the object. The declaration with extern keyword makes object Serial visible in all cpp files which include HardwareSerial.h (or an include file which includes HardwareSerial.h like Arduino.h included by ino to cpp conversion).

53592: <p>I have a .h library file in which I want to declare a global variable that can be read in the program loop and in the library's .cpp file.</p>

<p>The question is, is it possible to declare a variable in the .h file and make it global?</p>
 31079: <p>You can declare the variable as a <code>static</code> variable inside an <code>inline</code> function, and then just call this function to get a reference to this variable. Example:</p>

<pre><code>inline int&amp; getMyInteger()
{
    static int x;
    return x;
}
</code></pre>

<p>This definition can be in a header file, included into multiple <code>*.cpp</code> files, and any call to <code>getMyInteger</code> will return reference to the same object. You don't need any <code>extern</code>, nor to actually put the variable in any compilation unit as you would need with a simple namespace-scope variable. This trick just uses the linker's ability to de-duplicate <code>inline</code> functions.</p>

<p>See also: <a href="https://stackoverflow.com/q/185624/673852">static variables in an inlined function</a> question on StackOverflow.</p>
 37622: <p>.h file is included by the compilation <em>preprocessor</em> as text into the .c or .cpp file. It is included as if you would replace the <code>#include</code> <em>preprocessor instruction</em> with the content of the .h file. (The Arduino .ino file is converted to cpp file before <em>preprocessing</em>.)</p>

<p>After the inclusion the cpp file is compiled to 'object' (.o file). If the cpp file contains a variable <em>definition</em> the variable is <em>defined</em> in this <em>object</em> file. If your .h file contained a variable <em>definition</em>, the it is also in this object file.</p>

<p>If two cpp files included the .h file with the <em>definition</em> of the variable, then the variable will be in two <em>object</em> files and the linker will exit with an error, because of two variables with the same name.
If only one cpp/ino files includes the variable <em>definition</em>, then it is OK. It is not a good practice, but sometimes a necessary compromise. (<a href="https://arduino.stackexchange.com/questions/58358/how-to-avoid-multiple-definition-of-blynk-error/58359#58359">example of it</a>)</p>

<p>To have one global variable made accessible by a .h file, the variable should be only <em>declared</em> in the .h file and be <em>defined</em> in one cpp file. Usually it is a pair of .h and .cpp file with same name. </p>

<p>An example of global variable is <code>Serial</code>. It is a global object of type <code>HardwareSerial</code> <em>declared</em> in HardwareSerial.h as </p>

<p><code>extern HardwareSerial Serial;</code> </p>

<p>and <em>defined</em> in HardwareSerial0.cpp as </p>

<p><code>HardwareSerial Serial(&amp;UBRR0H, &amp;UBRR0L, &amp;UCSR0A, &amp;UCSR0B, &amp;UCSR0C, &amp;UDR0);</code></p>

<p>Do you see the difference? The <em>declaration</em> has the keyword <code>extern</code> and the <em>definition</em> has in this case parameters for the constructor of the object. The <em>declaration</em> with extern keyword makes object Serial visible in all cpp files which include HardwareSerial.h (or an include file which includes HardwareSerial.h like Arduino.h included by ino to cpp conversion).</p>
