title: What if redefine an inline function?
body: <p>I've spent days in a weird problem and finally discover that there were two <code>inline</code> function of the same signature in the project and they caused the problem. To simplify the situation here is an example: two cpp file:</p>
  
  <p>a.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  void b();
  
  inline void echo()
  {
      std::cout &lt;&lt; 0 &lt;&lt; std::endl;
  }
  
  int main()
  {
      echo();
      b();
      return 0;
  }
  </code></pre>
  
  <p>and b.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  inline void echo()
  {
      std::cout &lt;&lt; 1 &lt;&lt; std::endl;
  }
  
  void b()
  {
      echo();
  }
  </code></pre>
  
  <p>Please note that <code>inline</code> functions <code>echo</code> have the same signature but different implements. Compile and run</p>
  
  <pre><code>g++ a.cpp b.cpp -o a.out &amp;&amp; ./a.out
  </code></pre>
  
  <p>Or like this</p>
  
  <pre><code>g++ a.cpp -c
  g++ b.cpp -c
  g++ a.o b.o -o a.out
  ./a.out
  </code></pre>
  
  <p>It prints <code>0 0</code>. (I was using g++ 4.6.1 for that, and I tested with clang++ 2.9, same result)</p>
  
  <p>That won't happen if turning on optimization, like</p>
  
  <pre><code>g++ -O3 a.cpp b.cpp -o a.out &amp;&amp; ./a.out
  </code></pre>
  
  <p>It is <code>0 1</code> this time.</p>
  
  <p>My question is, no matter the result or how the compilation performs, there is no error or even warning about I have defined <code>inline</code> functions multiple times. What on earth happens to the compiler and linker in this kind of situation?</p>
  
  <p>EDIT:</p>
  
  <p>Take a look at the symbols in the object file</p>
  
  <pre><code>nm a.o b.o | c++filt
  </code></pre>
  
  <p>Both files have the record <code>echo()</code>. So I think the problem happens at the link time. Could it be said that the linker randomly picks one implementation and discard all other?</p>