title: How to properly initialize global variables?
body: <p>I'm writing little student project and stuck with the problem that I have a few global variables and need to use it in a few source files, but I get the error <em>undefined reference to  variable_name</em>. Let's create three source files for example:</p>
  <p>tst1.h:</p>
  <pre><code>extern int global_a;
  void Init();
  </code></pre>
  <p>tst1.cpp:</p>
  <pre><code>#include &quot;tst1.h&quot;
  void Init(){
    global_a = 1;
  }
  </code></pre>
  <p>tst2.cpp:</p>
  <pre><code>#include &quot;tst1.h&quot;
  int main(){
    Init();
  }
  </code></pre>
  <p>When I compile and link, that's what I get:</p>
  <pre><code>$ g++ -c tst1.cpp 
  $ g++ -c tst2.cpp 
  $ g++ tst2.o tst1.o
  tst1.o: In function `Init()':
  tst1.cpp:(.text+0x6): undefined reference to `global_a'
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>If I remove the <em>extern</em> statement, then I get the other problem, let me show:</p>
  <pre><code>$ g++ -c tst1.cpp 
  $ g++ -c tst2.cpp 
  $ g++ tst2.o tst1.o
  tst1.o:(.bss+0x0): multiple definition of `global_a'
  tst2.o:(.bss+0x0): first defined here
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>I really need some variables to be global, for example my little project works with assembly code, and have a variables like <em>string rax = &quot;%rax %eax %ax %ah %al&quot;;</em> which should be referenced through different source files.</p>
  <p>So, how to properly initialize the global variables?</p>