title: Defining function in .h vs .c file
body: <p>I have the following program setup:</p>
  <pre><code>// main.c
  #include &quot;str.h&quot;
  ...
  trim(&quot;Hello&quot;);
  </code></pre>
  <pre><code>// str.h
  char* trim(char* str) {
      return str;
  }
  </code></pre>
  <pre><code>// str.c
  #include &quot;str.h&quot;
  [empty]
  </code></pre>
  <p>And compiled as: <code>$ gcc main.c str.c -o run &amp;&amp; ./run</code>, and I get the following error:</p>
  <pre><code>/tmp/ccEUb8mH.o: In function `trim':
  str.c:(.text+0x0): multiple definition of `trim'
  /tmp/ccCiyN5A.o:main.c:(.text+0x0): first defined here
  collect2: error: ld returned 1 exit status
  </code></pre>
  <p>However, as soon as I change <code>str.h/c</code> to the following declration/definition it works:</p>
  <pre><code>// str.h
  [empty]
  </code></pre>
  <pre><code>// str.c
  #include &quot;str.h&quot;
  char* trim(char* str) {
      return str;
  }
  </code></pre>
  <p>What's the reason the second version works but the first one does not?</p>
  <p>I suppose it has to do with that the following will work fine:</p>
  <pre><code>char * trim(char * str);
  char * trim(char * str);
  char * trim(char * str);
  char * trim(char * str);
  char * trim(char * str);
  </code></pre>
  <p>But with definitions it will not:</p>
  <pre><code>char * trim(char * str){return str};
  char * trim(char * str){return str};
  </code></pre>
  <blockquote>
  <p>error: redefinition of ‘trim’</p>
  </blockquote>