Undefined Reference to sem_wait and pthread_create when compiling with g++
<p>I am fairly new to C and do not understand why I am getting these errors when I try to compile my program using <code>g++ ./main.c</code>. I have tried looking at other resources and I am unable to find the answers I need. If there is a solution that you already know of, please post it here as well. </p>

<pre><code>/tmp/ccSGRAcp.o: In function `producer(void*)':
main.c:(.text+0x12): undefined reference to `sem_wait'
main.c:(.text+0x1c): undefined reference to `sem_wait'
main.c:(.text+0xa0): undefined reference to `sem_post'
main.c:(.text+0xaa): undefined reference to `sem_post'
/tmp/ccSGRAcp.o: In function `consumer(void*)':
main.c:(.text+0xc5): undefined reference to `sem_wait'
main.c:(.text+0xcf): undefined reference to `sem_wait'
main.c:(.text+0x153): undefined reference to `sem_post'
main.c:(.text+0x15d): undefined reference to `sem_post'
/tmp/ccSGRAcp.o: In function `main':
main.c:(.text+0x17e): undefined reference to `sem_init'
main.c:(.text+0x192): undefined reference to `sem_init'
main.c:(.text+0x1a6): undefined reference to `sem_init'
main.c:(.text+0x1c1): undefined reference to `pthread_create'
main.c:(.text+0x1dc): undefined reference to `pthread_create'
main.c:(.text+0x1f7): undefined reference to `pthread_create'
main.c:(.text+0x212): undefined reference to `pthread_create'
main.c:(.text+0x22d): undefined reference to `pthread_create'
/tmp/ccSGRAcp.o:main.c:(.text+0x248): more undefined references to `pthread_create' follow
collect2: error: ld returned 1 exit status
</code></pre>

<p>The code I am trying to compile is </p>

<pre><code>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/sem.h&gt;
#include &lt;semaphore.h&gt;
#include &lt;pthread.h&gt;

#define N 10000000

typedef struct
{
    char const* buf[N];
    char in;
    char out;

    sem_t mutex;
    sem_t full;
    sem_t empty;

} bufferItems;

bufferItems sharedBuffer;


void *producer(void *arg) {

    while(1) {
        sem_wait(&amp;sharedBuffer.empty);
        sem_wait(&amp;sharedBuffer.mutex);
        sharedBuffer.buf[sharedBuffer.in] = "X";
        sharedBuffer.in = (sharedBuffer.in+1)%N;
        printf("Producer ");
        printf("%c", sharedBuffer.in);
        printf("\n");
        sem_post(&amp;sharedBuffer.mutex);
        sem_post(&amp;sharedBuffer.full);


    }

}

void *consumer(void *arg){

    while(1){
        sem_wait(&amp;sharedBuffer.full);
        sem_wait(&amp;sharedBuffer.mutex);
        sharedBuffer.buf[sharedBuffer.out] = NULL;
        sharedBuffer.out = (sharedBuffer.out+1)%N;
        printf("Consumer ");
        printf("%c", sharedBuffer.out);
        printf("\n");
        sem_post(&amp;sharedBuffer.mutex);
        sem_post(&amp;sharedBuffer.empty);

    }

}


int main(void) {

    sem_init(&amp;sharedBuffer.mutex, 0, 0);
    sem_init(&amp;sharedBuffer.full, 0, 0);
    sem_init(&amp;sharedBuffer.empty, 0, N);

    pthread_t p1;
    pthread_t p2;
    pthread_t p3;
    pthread_t p4;

    pthread_t c1;
    pthread_t c2;
    pthread_t c3;
    pthread_t c4;

    // create four producer threads
    pthread_create(&amp;p1,NULL,producer,NULL);
    pthread_create(&amp;p2,NULL,producer,NULL);
    pthread_create(&amp;p3,NULL,producer,NULL);
    pthread_create(&amp;p4,NULL,producer,NULL);
    // create four consumer threads
    pthread_create(&amp;c1,NULL,consumer,NULL);
    pthread_create(&amp;c2,NULL,consumer,NULL);
    pthread_create(&amp;c3,NULL,consumer,NULL);
    pthread_create(&amp;c4,NULL,consumer,NULL);
}
</code></pre>