user60405: I have created a library Lib that doesn't have a class. I want this library to have a variable that can be accessed from outside. The way I would go about this is to just declare them in the header file.
However, if I do this like so:
int myVariable = 1;

with the .cpp-file containing only
#include "Lib.h"

I get this error message when compiling an empty sketch that includes this library:

multiple definition of `myVariable'

and for some weird reason

Multiple libraries were found for "Lib.h"

on top of that (although, this is something I have encountered before - when there is some error during the compilation, this error may come alongside it, even though there are no multiple libraries; it goes away when the other compilation errors are resolved aswell).
What is causing that error and how to resolve it?

user4143: If you want a "variable" that is used at compile time, for instance to set the size of an array, then a variable is not what you want.
Variables change. That's what "variable" means. If you are using it for something at compile time then it can't be variable, since it must never change.
Ergo, you want either a constant or a #define macro which is replaced with a literal.  Both of these can be done in the header, since they both by default have local TU scope:
const int myVariable = 123;

Or to use a macro:
#define SAMPLE_ARRAY_SIZE 123


user16780: In Lib.h you should declare an
extern int myVariable;

In Lib.cpp you can define it once
#include "Lib.h"
int myVariable=123;
...


60405: <p>I have created a library <code>Lib</code> that doesn't have a class. I want this library to have a variable that can be accessed from outside. The way I would go about this is to just declare them in the header file.</p>

<p>However, if I do this like so:</p>

<pre class="lang-cpp prettyprint-override"><code>int myVariable = 1;
</code></pre>

<p>with the <code>.cpp</code>-file containing only</p>

<pre class="lang-cpp prettyprint-override"><code>#include "Lib.h"
</code></pre>

<p>I get this error message when compiling an empty sketch that includes this library:</p>

<blockquote>
  <p>multiple definition of `myVariable'</p>
</blockquote>

<p>and for some weird reason</p>

<blockquote>
  <p>Multiple libraries were found for "Lib.h"</p>
</blockquote>

<p>on top of that (although, this is something I have encountered before - when there is some error during the compilation, this error may come alongside it, even though there are no multiple libraries; it goes away when the other compilation errors are resolved aswell).</p>

<p>What is causing that error and how to resolve it?</p>
 4143: <p>If you want a "variable" that is used at compile time, for instance to set the size of an array, then a variable is <em>not</em> what you want.</p>

<p>Variables change. That's what "variable" means. If you are using it for something at compile time then it can't be variable, since it must never change.</p>

<p>Ergo, you want either a <em>constant</em> or a <code>#define</code> macro which is replaced with a <em>literal</em>.  Both of these can be done in the header, since they both by default have local TU scope:</p>

<pre><code>const int myVariable = 123;
</code></pre>

<p>Or to use a macro:</p>

<pre><code>#define SAMPLE_ARRAY_SIZE 123
</code></pre>
 16780: <p>In Lib.h you should declare an</p>

<pre><code>extern int myVariable;
</code></pre>

<p>In Lib.cpp you can define it once</p>

<pre><code>#include "Lib.h"
int myVariable=123;
...
</code></pre>
