title: Why do C++ member functions defined in a class not produce duplicate symbols, whereas they do in C?
body: <p><strong>C Example</strong></p>
  
  <p>bb.c:</p>
  
  <pre><code>#include "bb.h"
  #include &lt;stdio.h&gt;
  
  void bb() {
      printf("aa()...\n");
      aa();
  }
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>#include "aa.h"
  #include "bb.h"
  
  int main(int argc, const char** argv) {
  
      aa();
      bb();
  
      return 0;
  }
  </code></pre>
  
  <p>aa.h:</p>
  
  <pre><code>#ifndef aa_h
  #define aa_h
  
  #include &lt;stdio.h&gt;
  
  void aa() {
      printf("aa()...\n");
  }
  
  #endif // aa_h
  </code></pre>
  
  <p>bb.h:</p>
  
  <pre><code>#ifndef bb_h
  #define bb_h
  
  #include "aa.h"
  
  void bb();
  
  #endif // bb_h
  </code></pre>
  
  <p><strong>C Result</strong></p>
  
  <p>Compiled with clang main.c bb.c:</p>
  
  <pre><code>duplicate symbol _aa in:
      /var/folders/f2/2w4c0_n519g8cd2k6xv66hc80000gn/T/main-OsFJVB.o
      /var/folders/f2/2w4c0_n519g8cd2k6xv66hc80000gn/T/bb-OkcMzn.o
  ld: 1 duplicate symbol for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p><strong>C++ Example</strong></p>
  
  <p>b.cpp:</p>
  
  <pre><code>#include "b.hpp"
  
  void b::do_something_else() {
      std::cout &lt;&lt; "b::do_something_else() being called..." &lt;&lt; std::endl;
      a a;
      a.doit();
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include "a.hpp"
  #include "b.hpp"
  
  int main() {
  
      a a;
      b b;
  
      a.doit();
      b.do_something_else();
  
      return 0;
  }
  </code></pre>
  
  <p>a.hpp:</p>
  
  <pre><code>#ifndef a_hpp
  #define a_hpp
  
  #include &lt;iostream&gt;
  
  class a{
  public:
  
      void doit() {
          std::cout &lt;&lt; "a::doit() being called..." &lt;&lt; std::endl;
      }
  
  };
  
  #endif // a_hpp
  </code></pre>
  
  <p>b.hpp:</p>
  
  <pre><code>#ifndef b_hpp
  #define b_hpp
  
  #include "a.hpp"
  
  #include &lt;iostream&gt;
  
  class b{
  public:
  
      void do_something_else();
  
  };
  
  #endif // b_hpp
  </code></pre>
  
  <p><strong>C++ Result</strong></p>
  
  <p>The above compiles fine with <code>clang++ main.cpp b.cpp</code> and the output to the program is:</p>
  
  <pre><code>a::doit() being called...
  b::do_something_else() being called...
  a::doit() being called...
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li><p>Why does the duplicate error <em>not</em> occur with the C++ version?</p></li>
  <li><p>Does the fact that the function <code>void a::doit()</code> is <em>defined</em> in the header file rather than a source file mean that the compiler will automatically inline the function?</p></li>
  </ol>