title: What's the order for gcc to link unresolved symbol in static library
body: <p>When debug the function symbols conflicts problem, I find a strange behavior of gcc i couldn't  understand, illustrate by the following sample code:</p>
  
  <p>main.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int  main()
  {
     b();
     a();
  }
  </code></pre>
  
  <p>a.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  void  a(void)
  {
     printf("func a in a\n");
  }
  </code></pre>
  
  <p>b.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void a()
  {
     printf("func a in b\n");
  }
  void b()
  {
    printf( "func b try to call a  \n");
    a();
  }
  </code></pre>
  
  <p>compile: </p>
  
  <pre><code>gcc -c a.c
  gcc -c b.c
  ar -cr liba.a a.o
  ar -cr libb.a b.o
  gcc main.c liba.a libb.a
  </code></pre>
  
  <p>execute:</p>
  
  <pre><code>./a.out
  func b try to call a  
  func a in b
  func a in b   
  </code></pre>
  
  <p>My question is : </p>
  
  <ul>
  <li>Why calling function <code>a</code> in <code>main</code> function is <code>a in b.c</code> instead of <code>a in a.c</code> ?</li>
  <li>After change the library order: <code>gcc main.c libb.a liba.a</code>, the result is the same. Why?</li>
  <li>Why the linker don't report symbol conflict in this situation?</li>
  </ul>