title: multiple definition in g++?
body: <p>The code is as follows:</p>
  
  <p><strong>global.h</strong></p>
  
  <pre><code>#ifndef GLOBAL_H
  #define GLOBAL_H
  #include &lt;stdio.h&gt;
  int test;
  
  void test_fun(void);
  #endif
  </code></pre>
  
  <p><strong>global.c</strong></p>
  
  <pre><code>#include "global.h"
  
  void test_fun()
  {
      printf("%d\n", test);
  }
  </code></pre>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>#include "global.h"
  
  int main(void)
  {
      test_fun();
      test = 1;
      printf("%d\n", test);
  }
  </code></pre>
  
  <p><strong>Makefile using gcc compiler</strong></p>
  
  <pre><code>main: main.o global.o
      gcc -o main main.o global.o
  
  main.o: main.c global.h
      gcc -c main.c
  global.o: global.c global.h
      gcc -c global.c
  
  clean:
      rm -f global.o main.o main
  </code></pre>
  
  <p>This works well.</p>
  
  <p>However, when I change my code to C++, as follows:</p>
  
  <p><strong>global.h</strong></p>
  
  <pre><code>#ifndef GLOBAL_H
  #define GLOBAL_H
  #include &lt;iostream&gt;
  int test;
  
  void test_fun(void);
  #endif
  </code></pre>
  
  <p><strong>global.cpp</strong></p>
  
  <pre><code>#include "global.h"
  
  void test_fun()
  {
      cout &lt;&lt; test
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include "global.h"
  
  int main(void)
  {
      test_fun();
      test = 1;
      std::cout &lt;&lt; test;
  }
  </code></pre>
  
  <p><strong>Makefile using g++ compiler</strong></p>
  
  <pre><code>main: main.o global.o
      g++ -o main main.o global.o
  
  main.o: main.cpp global.h
      g++ main.cpp
  global.o: global.cpp global.h
      g++ global.cpp
  
  clean:
      rm -f global.o main.o main
  </code></pre>
  
  <p>The code above throws the output:</p>
  
  <pre><code>global.o:(.bss+0x0): multiple definition of `test'
  </code></pre>
  
  <p>What makes the different here?</p>