title: Function prototype in header file doesn't match definition, how to catch this?
body: <p>(I found this question which is similar but not a duplicate:
  <a href="https://stackoverflow.com/questions/7148285/how-to-check-validity-of-header-file-in-c-programming-language">How to check validity of header file in C programming language</a> )</p>
  
  <p>I have a function implementation, and a non-matching prototype (same name, different types) which is in a header file. The header file is included by a C file that uses the function, but is not included in the file that defines the function. </p>
  
  <p>Here is a minimal test case :</p>
  
  <p><strong>header.h:</strong></p>
  
  <pre><code>void foo(int bar);
  </code></pre>
  
  <p><strong>File1.c:</strong></p>
  
  <pre><code>#include "header.h"
  int main (int argc, char * argv[])
  {
      int x = 1;
      foo(x);
      return 0;
  }
  </code></pre>
  
  <p><strong>File 2.c:</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  typedef struct {
      int x;
      int y;
  } t_struct;
  
  void foo (t_struct *p_bar)
  {
      printf("%x %x\n", p_bar-&gt;x, p_bar-&gt;y);
  }
  </code></pre>
  
  <p>I can compile this with VS 2010 with no errors or warnings, but unsurprisingly it segfaults when I run it.</p>
  
  <ul>
  <li>The compiler is fine with it (this I understand) </li>
  <li>The linker did not catch it (this I was slightly surprised by) </li>
  <li>The static analysis tool (Coverity) did not catch it (this I was very surprised by).</li>
  </ul>
  
  <p>How can I catch these kinds of errors?</p>
  
  <p>[Edit: I realise if I <code>#include "header.h"</code> in <code>file2.c</code> as well, the compiler will complain. But I have an enormous code base and it is not always possible or appropriate to guarantee that all headers where a function is prototyped are included in the implementation files.]</p>