title: Symbol not found when static linking on MacOSX
body: <p>I am trying to create a static library and link it on MacOS X (several versions):
  File <code>foo.c</code>:</p>
  
  <pre><code>char foo[111];
  </code></pre>
  
  <p>File <code>bar.c</code>:</p>
  
  <pre><code>#include &lt;string.h&gt;
  
  extern char foo[];
  
  int bar(char *src) {
    strcpy(foo, src);
    return strlen(foo);
  }
  </code></pre>
  
  <p>Create a library:</p>
  
  <pre><code>$ cc -c foo.c bar.c
  $ ar r libfoobar.a foo.o bar.o
  ar: creating archive libfoobar.a
  $ ranlib libfoobar.a 
  $ nm libfoobar.a 
  
  libfoobar.a(foo.o):
  000000000000006f C _foo
  
  libfoobar.a(bar.o):
                   U ___strcpy_chk
  0000000000000000 T _bar
                   U _foo
                   U _strlen
  </code></pre>
  
  <p>Create a small test program:</p>
  
  <p>File <code>main.c</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int bar(char *);
  
  int main(void) {
    printf("foobarbar = %i\n", bar("123"));
    return 0;
  }
  </code></pre>
  
  <p>Compile and link:</p>
  
  <pre><code>$ cc -c main.c
  $ cc -o m main.o -L. -lfoobar
  Undefined symbols for architecture x86_64:
    "_foo", referenced from:
        _bar in libfoobar.a(bar.o)
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>Why is the symbol not found? It is defined in <code>foo.c</code>? Shouldn't at least <code>ranlib</code> create an index in the library that allows a random order of the files there?</p>
  
  <p>The same code works well under Linux (gcc), and also when the symbol in <code>foo.c</code> is not a char array, but an int.</p>