title: Why am I getting a gcc "undefined reference" error trying to create shared objects?
body: <p>Why am I getting an "undefined reference" error using gcc?</p>
  
  <p>I am trying to create a shared object (.so) that exports one function, "external()".  I then try to link against the .so but get "undefined reference 'external'".  What am I doing wrong here?</p>
  
  <p><strong>File: external.c</strong></p>
  
  <pre><code>int external() {
      return 5;
  }
  </code></pre>
  
  <p><strong>File: program.c</strong></p>
  
  <pre><code>int external();
  int main(char** argv, int* argc) {
      return external();
  }
  </code></pre>
  
  <p><strong>Commands:</strong></p>
  
  <pre><code>$ gcc -fPIC -c external.c
  $ gcc -shared -o libexternal.so external.o
  $ gcc -L. -lexternal -o program program.c
  /tmp/cc3MmhAE.o: In function `main':
  program.c:(.text+0x7): undefined reference to `external'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I can even run nm and see that the .so is defining 'external':</p>
  
  <p>Command:</p>
  
  <pre><code>$ nm libexternal.so | grep external
  0000040c T external
  </code></pre>
  
  <p>What am I missing here?</p>