title: Experimenting with global variables and functions in C
body: <p>I'm trying to understand how global variables and functions work in C. My program compiles and works fine with <code>gcc</code>, but does not compile with <code>g++</code>. I have the following files:</p>
  
  <p><strong>globals.h:</strong></p>
  
  <pre><code>int i;
  void fun();
  </code></pre>
  
  <p><strong>globals.c:</strong></p>
  
  <pre><code>#include "stdlib.h"
  #include "stdio.h"
  
  void fun()
  {
    printf("global function\n");
  }
  </code></pre>
  
  <p><strong>main.c:</strong></p>
  
  <pre><code>#include "stdlib.h"
  #include "stdio.h"
  #include "globals.h"
  
  void myfun();
  
  int main()
  {
  
    i=1;
  
    myfun();
  
    return 0;
  }
  </code></pre>
  
  <p>And finally, <strong>myfun.c</strong>:</p>
  
  <pre><code>#include "stdlib.h"
  #include "stdio.h"
  #include "globals.h"
  
  void myfun()
  {
    fun();
  }
  </code></pre>
  
  <p>I get the following error when compiling with g++:</p>
  
  <pre><code>/tmp/ccoZxBg9.o:(.bss+0x0): multiple definition of `i'
  /tmp/ccz8cPTA.o:(.bss+0x0): first defined here
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Any ideas why? I would prefer to compile with g++.</p>