title: Why am I getting a 'multiple definition' error? how do i fix it?
body: <p>command given by terminal:</p>
  
  <blockquote>
    <p>g++ main.cpp test.cpp</p>
  </blockquote>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>/tmp/ccvgRjlI.o: In function `test2()': <br>test.cpp:(.text+0x0): multiple
    definition of `test2()'<br>
    /tmp/ccGvwiUE.o:main.cpp:(.text+0x0): first defined here<br>
    collect2: error: ld returned 1 exit status
    main.cpp</p>
  </blockquote>
  
  <p>Source code:</p>
  
  <pre><code>#include "test.hpp"
  int main(int argc, char *argv[])
  {
      test2();
      return 0;
  }
  </code></pre>
  
  <p>test.hpp</p>
  
  <pre><code>#ifndef _TEST_HPP_
  #define _TEST_HPP_
  
  #include &lt;iostream&gt;
  
  void test();
  void test2() { std::cerr &lt;&lt; "test2" &lt;&lt; std::endl; }
  
  #endif
  </code></pre>
  
  <p>test.cpp</p>
  
  <pre><code>#include "test.hpp"
  
  using std::cerr;
  using std::endl;
  
  void test() { cerr &lt;&lt; "test" &lt;&lt; endl; }
  </code></pre>
  
  <p>btw the following compiles fine:</p>
  
  <blockquote>
    <p>g++ main.cpp</p>
  </blockquote>