title: Undefined reference to all 'sem' and 'pthread' functions upon compilation
body: <p>I'm using the gcc to compile this c program that just relies on 2 created thread, one to increment a counter, the second reads the counter and subtracts a random (0-9) value from the counter, and then displays the counter's value, using a Semaphore to access it. Still upon compilation I'm facing lots of ' Undefined reference to sem_init/sem_wait/sem_post/pthread_create/..etc' I don't know why although I associated them headers in my program.</p>
  
  <p>I'm using 'gcc -o prog prog.c'to compile my program.</p>
  
  <pre><code>#include&lt;semaphore.h&gt;
  #include&lt;pthread.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;unistd.h&gt;
  
  int counter=0;
  int run=1;
  sem_t mutex;
  
  void * increm();
  
  void * decrem();
  
  
  void main()
  {       sem_t mutex;
      sem_init(&amp;mutex,0,1);
      pthread_t t1,t2;
      pthread_create(&amp;t1,NULL,increm,NULL);
      pthread_create(&amp;t2,NULL,decrem,NULL);
  pthread_join(t1,NULL);
  pthread_join(t2,NULL);
  sem_destroy(&amp;mutex);
  }
  void * increm()
  {  while(run)
      {sem_wait(&amp;mutex);
              counter++;
              sem_post(&amp;mutex);
      }
      pthread_exit(NULL);
  }
  
   void * decrem()
  { int i=25;
  
      while(i&gt;0)
      {sem_wait(&amp;mutex);
              counter-=(rand()%10);
             printf("Counter value : %d \n",counter);
              i--;
              sem_post(&amp;mutex);
      }
      run=0;
      pthread_exit(NULL);
   }
  </code></pre>