Q:

Error: /tmp/ccOB6cIT.o: In function `main': example.c:(.text+0x4a)

I picked up the following code from a practice file
#include <stdio.h>
#include <pthread.h>
void *kidfunc(void *p) 
{
printf ("Kid ID is ---> %d\n", getpid( ));
}
int main(int count, char* argv[])
{
pthread_t kid ;
pthread_create (&kid, NULL, kidfunc, NULL) ;
printf ("Parent ID is ---> %d\n", getpid( )) ;
pthread_join (kid, NULL) ;
printf ("No more kid!\n") ;
return 0;
}

But when I compile it on my laptop (running Ubuntu 14.04) it generates the following error:
ahsan-15f8116@VirtualWorld:~$ gcc example.c
/tmp/ccOB6cIT.o: In function `main':
example.c:(.text+0x4a): undefined reference to `pthread_create'
example.c:(.text+0x71): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

This doesn't make any sense to me, because the header includes pthread.h, can you tell me what I'm doing wrong here? 

A:

Headers only declare functions, it is libraries that define them.
Usually you would need to add libraries to be linked explicitly on the gcc command line e.g. gcc -o prog prog.c -lm for the math library libm. However the pthread library is a special case - it is linked by a special compiler flag -pthread:
gcc -pthread example.c

From man gcc
-pthread
    Adds support for multithreading with the pthreads library.  This
    option sets flags for both the preprocessor and linker.

