title: Undefined reference to pthread_create in Linux
body: <p>I picked up the following demo off the web from <a href="https://computing.llnl.gov/tutorials/pthreads/" rel="noreferrer">https://computing.llnl.gov/tutorials/pthreads/</a></p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #define NUM_THREADS     5
  
  void *PrintHello(void *threadid)
  {
     long tid;
     tid = (long)threadid;
     printf("Hello World! It's me, thread #%ld!\n", tid);
     pthread_exit(NULL);
  }
  
  int main (int argc, char *argv[])
  {
     pthread_t threads[NUM_THREADS];
     int rc;
     long t;
     for(t=0; t&lt;NUM_THREADS; t++){
        printf("In main: creating thread %ld\n", t);
        rc = pthread_create(&amp;threads[t], NULL, PrintHello, (void *)t);
        if (rc){
           printf("ERROR; return code from pthread_create() is %d\n", rc);
           exit(-1);
        }
     }
     pthread_exit(NULL);
  }
  </code></pre>
  
  <p>But when I compile it on my machine (running Ubuntu Linux 9.04) I get the following error:</p>
  
  <pre><code>corey@ubuntu:~/demo$ gcc -o term term.c
  term.c: In function ‘main’:
  term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
  /tmp/cc8BMzwx.o: In function `main':
  term.c:(.text+0x82): undefined reference to `pthread_create'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>This doesn't make any sense to me, because the header includes <code>pthread.h</code>, which should have the <code>pthread_create</code> function. Any ideas what's going wrong?</p>