title: How to resolve Duplicate Symbol Error?
body: <p>I'm compiling 2 C++ files together. 4 if you include the header files. The problem is, I keep getting "Duplicate Symbol" errors when the linker tries to link the files together.</p>
  
  <p>Here are my files.</p>
  
  <hr>
  
  <p><strong>main.h</strong></p>
  
  <pre><code>int test2();
  </code></pre>
  
  <hr>
  
  <p><strong>main.cc</strong></p>
  
  <pre><code>#include "main.h"
  #include "test.h"
  
  int test2(int test) {
      return 0;
  }
  
  int main() {
  
      test2(test());
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>test.h</strong></p>
  
  <pre><code>int hello = 10;
  int test();
  </code></pre>
  
  <hr>
  
  <p><strong>test.cc</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "test.h"
  using namespace std;
  
  int test() {
    cout &lt;&lt; hello &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>I think I'm doing something simple wrong. Can someone please point out what I'm doing wrong.
  Here's how I'm compiling the files.</p>
  
  <pre><code>c++ main.cc test.cc -o main
  </code></pre>
  
  <p>Here's the error I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>duplicate symbol _hello in:
  /var/folders/nj/568_95bj4dg9v11l_mksv_2m0000gn/T/main-3becdd.o
  /var/folders/nj/568_95bj4dg9v11l_mksv_2m0000gn/T/test-e84473.o
  ld: 1 duplicate symbol for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>