title: Already defined in .obj even when using extern and #ifdef
body: <p>I have three files like this:</p>
  <p><strong>head.h</strong></p>
  <pre><code>#ifndef HEAD_
  #define HEAD_
  
  extern int f();
  
  #endif
  </code></pre>
  <p><strong>mycpp.cpp</strong></p>
  <pre class="lang-c prettyprint-override"><code>#include &quot;head.h&quot;
  
  int f() {
      return 5;
  }
  </code></pre>
  <p><strong>myMain.cpp</strong></p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;iostream&gt;
  #include &quot;head.h&quot;
  #include &quot;mycpp.cpp&quot;
  
  int main()
  {
      std::cout &lt;&lt; f() &lt;&lt; std::endl;
      system(&quot;Pause&quot;);
      return 0;
  }
  </code></pre>
  <p>Running this code produces a link error:</p>
  <blockquote>
  <p>LNK2005   &quot;int __cdecl f(void)&quot; (?f@@YAHXZ) already defined in mycpp.obj</p>
  </blockquote>
  <p>If I add <code>inline</code> to the function <code>f</code> in <code>mycpp.cpp</code> the error goes away.</p>
  <p>My question is how to get rid of this error without using the <code>inline</code> function?</p>
  <p>P.S. This is an assignment and I can only modify the <code>mycpp.cpp</code> file. So the <code>#include &quot;mycpp.cpp&quot;</code> has to be there.</p>
  <p>I know it is a bad idea to include <code>cpp</code> files, but I cannot change the <code>myMain</code> file. It is given this way. And I am not supposed to use <code>inline</code>.</p>