title: Global variables in header file
body: <p>I have a 2 modules (.c files) and one .h header file:</p>
  
  <p>file1.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "global.h"
  
  int main()
  {
      i = 100;
      printf("%d\n",i);
      foo();
      return 0;
  }
  </code></pre>
  
  <p>file2.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "global.h"
  
  void foo()
  {
      i = 10;
      printf("%d\n",i);
  }
  </code></pre>
  
  <p>global.h</p>
  
  <pre><code>int i;
  extern void foo()
  </code></pre>
  
  <p>When I do gcc file1.c file2.c everything works fine and I get the expected output. Now, when I initialize variable 'i' in the header file to say 0 and compile again I get a linker error:</p>
  
  <pre><code>/tmp/cc0oj7yA.o:(.bss+0x0): multiple definition of `i'
  /tmp/cckd7TTI.o:(.bss+0x0): first defined here
  </code></pre>
  
  <p>If I just compile file1.c (removing call to foo()) with the initialization in the header file i.e. gcc file1.c, everything works fine. What is going on?</p>