title: Undefined Reference issues using Semaphores
body: <p>I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX's semaphore tutorial and changed things around to their syntax and as a result am now getting these reference errors.</p>
  
  <p>I may simply be overlooking something, but I cannot find it.</p>
  
  <p>Errors:</p>
  
  <pre><code>Producers_Consumers.c:52: warning: return type of ‘main’ is not ‘int’
  /tmp/cceeOM6F.o: In function `producer':
  Producers_Consumers.c:(.text+0x1e): undefined reference to `sem_init'
  Producers_Consumers.c:(.text+0x3a): undefined reference to `sem_init'
  Producers_Consumers.c:(.text+0x46): undefined reference to `sem_wait'
  Producers_Consumers.c:(.text+0x52): undefined reference to `sem_wait'
  Producers_Consumers.c:(.text+0x5e): undefined reference to `sem_post'
  Producers_Consumers.c:(.text+0x6a): undefined reference to `sem_post'
  /tmp/cceeOM6F.o: In function `consumer':
  Producers_Consumers.c:(.text+0x7e): undefined reference to `sem_wait'
  Producers_Consumers.c:(.text+0x8a): undefined reference to `sem_wait'
  Producers_Consumers.c:(.text+0x96): undefined reference to `sem_post'
  Producers_Consumers.c:(.text+0xa2): undefined reference to `sem_post'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>What I have (It may look a bit ugly due to the way I commented things out from my old method) I also know my adding method won't work, but I'll get to that when I fix my syntax issues:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;errno.h&gt;
  
  #define N 10     //Number of slots in buffer
  typedef int semaphore;  //Semaphores ae a special kind of int
  sem_t mutex; //Controls access to critical region 1
  sem_t empty;  //Counts empty buffer slots N
  sem_t  full;  //Counts full buffer slots 0
  int count = 0; //What we're putting in 
  //int buffer[N];
  
  void producer(void) {
      sem_init(&amp;mutex, 0, 1);
      //sem_init(&amp;empty, 0, N);
      sem_init(&amp;full, 0, 0);
  
      while(1) { 
          sem_wait(&amp;empty);
          sem_wait(&amp;mutex);
          //printf("Empy: %d\n",empty);
          //printf("Mutex: %d\n",mutex);
          //printf("Both Downs Ran\n");
          //buffer = buffer + 1;
          sem_post(&amp;mutex);
          sem_post(&amp;full);
          //printf("Producer produced: %d\n",buffer);
      }
  }
  
  void consumer(void) {
      while(1) { 
          sem_wait(&amp;full);
          sem_wait(&amp;mutex);
          //item = buffer;
          sem_post(&amp;mutex);
          sem_post(&amp;empty);
          //printf("Consumer consumed: %d/n",item);
      }
  }
  
  void main() {
  
  }
  </code></pre>