title: Undefined reference to sem_init and other such functions even though -lpthread and -ltr are used
body: <p>This is my code for reader writer problem in c in linux.
  I saw some same previous questions and the solution to them was using -lpthread or -ltr, I tried those two both individually and together but still I am getting the same error continuously. Please help. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;  
  #include&lt;pthread.h&gt;
  #include&lt;semaphore.h&gt;
  
  sem_t rmutex,wmutex,canwrite;
  int readcount=0;
  
  void *reader1(int i)
  {
  printf("Reader %d wants to read\n",i);
  sem_wait(&amp;rmutex);
  readcount++; 
  sem_post(&amp;rmutex); 
  printf("Reader %d is reading",i);
  
  sem_wait(&amp;rmutex);
  readcount--;
  
  if(readcount==0) 
  sem_post(&amp;canwrite);
  
  sem_post(&amp;rmutex);
  printf("Reader %d is exiting",i);
  
  }
  
  void *writer1(int i)
  {
  printf("Writer wants to write\n");
  sem_wait(&amp;canwrite);
  sem_wait(&amp;wmutex);
  printf("Writer is writing\n");
  sem_post(&amp;wmutex);
  printf("Writer is exiting\n");
  }
  
  
  
  int main()
  {
  pthread_t *reader[3],*writer;
  int i;
  sem_init(&amp;rmutex,0,1);
  sem_init(&amp;wmutex,0,1);
  sem_init(&amp;canwrite,0,1);
  
  
  pthread_create(&amp;writer,NULL,writer1,(void *)i);
  for(i=0;i&lt;3;i++)
  pthread_create(&amp;(reader[i]),NULL,reader1,(void *)i);
  
  sleep(10000);
  
  for(i=0;i&lt;3;i++)
  pthread_join(reader[i],NULL);
  
  pthread_join(writer[i],NULL);
  
  return 0;
  }
  </code></pre>
  
  <p>This is the error I am getting</p>
  
  <pre class="lang-none prettyprint-override"><code>vineet@vineet-TravelMate-P643-M:~/Work$ cc -lpthread rw.c
  rw.c: In function ‘main’:
  rw.c:49:1: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type     [enabled by default]
  /usr/include/pthread.h:225:12: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **’
   rw.c:49:1: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
  /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of    type ‘void * (*)(int)’
  rw.c:51:1: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type [enabled by default]
  /usr/include/pthread.h:225:12: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **’
  rw.c:51:1: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
  /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of  type ‘void * (*)(int)’
  rw.c:56:1: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast [enabled by default]
  /usr/include/pthread.h:242:12: note: expected ‘pthread_t’ but argument is of type  ‘pthread_t *’
  /tmp/ccYZOwcD.o: In function `reader1':
  rw.c:(.text+0x22): undefined reference to `sem_wait'
  rw.c:(.text+0x3b): undefined reference to `sem_post'
  rw.c:(.text+0x5b): undefined reference to `sem_wait'
  rw.c:(.text+0x7d): undefined reference to `sem_post'
  rw.c:(.text+0x89): undefined reference to `sem_post'
  /tmp/ccYZOwcD.o: In function `writer1':
  rw.c:(.text+0xbd): undefined reference to `sem_wait'
  rw.c:(.text+0xc9): undefined reference to `sem_wait'
  rw.c:(.text+0xe1): undefined reference to `sem_post'
  /tmp/ccYZOwcD.o: In function `main':
  rw.c:(.text+0x115): undefined reference to `sem_init'
  rw.c:(.text+0x131): undefined reference to `sem_init'
  rw.c:(.text+0x14d): undefined reference to `sem_init'
  rw.c:(.text+0x172): undefined reference to `pthread_create'
  rw.c:(.text+0x1aa): undefined reference to `pthread_create'
  rw.c:(.text+0x1e4): undefined reference to `pthread_join'
  rw.c:(.text+0x20f): undefined reference to `pthread_join'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>And it would be really helpful if you could also explain the warning.</p>