title: error LNK2005: already defined - C++
body: <p><strong>Background</strong></p>
  
  <p>I have a project named <em>PersonLibrary</em> which has two files.</p>
  
  <ol>
  <li>Person.h</li>
  <li>Person.cpp</li>
  </ol>
  
  <p>This library produces a static library file. Another project is <em>TestProject</em> which uses the <em>PersonLibrary</em> (Added though project dependencies in VS008). Everything worked fine until I added a non-member function to <em>Person.h</em>. <em>Person.h</em> looks like</p>
  
  <pre><code>class Person
  {
  public:
      void SetName(const std::string name);
  
  private:
      std::string personName_;
  };
  
  void SetPersonName(Person&amp; person,const std::string name)
  {
      person.SetName(name);
  }
  </code></pre>
  
  <p><em>Person.cpp</em> defines <em>SetName</em> function. When I try to use <em>SetPersonName</em> from <em>TestProject</em>, I get <em>error LNK2005: already defined</em>. Here is how I used it</p>
  
  <pre><code>#include "../PersonLibrary/Person.h"
  int main(int argc, char* argv[])
  {
      Person person;
      SetPersonName(person, "Bill");
      return 0;
  }
  </code></pre>
  
  <p><strong>Workarounds tried</strong></p>
  
  <p>1 - I have removed the <em>Person.cpp</em> and defined the whole class in <em>Person.h</em>. Error gone and everything worked.</p>
  
  <p>2 - Changed the <em>SetPersonName</em> modifier to <em>static</em>. Like the below</p>
  
  <pre><code>static void SetPersonName(Person&amp; person,const std::string name)
  {
      person.SetName(name);
  }
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>Why the code shown first is not working as I expected?</li>
  <li>What difference <em>static</em> made here?</li>
  <li>What is the approapriate solution for this problem?</li>
  </ol>
  
  <p>Thanks</p>