CW2272 CW2272 - 1 month ago 12
C Question

Undefined Reference to sem_wait and pthread_create when compiling with g++

I am fairly new to C and do not understand why I am getting these errors when I try to compile my program using

g++ ./main.c
. 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.

/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


The code I am trying to compile is

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <pthread.h>

#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(&sharedBuffer.empty);
sem_wait(&sharedBuffer.mutex);
sharedBuffer.buf[sharedBuffer.in] = "X";
sharedBuffer.in = (sharedBuffer.in+1)%N;
printf("Producer ");
printf("%c", sharedBuffer.in);
printf("\n");
sem_post(&sharedBuffer.mutex);
sem_post(&sharedBuffer.full);


}

}

void *consumer(void *arg){

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

}

}


int main(void) {

sem_init(&sharedBuffer.mutex, 0, 0);
sem_init(&sharedBuffer.full, 0, 0);
sem_init(&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(&p1,NULL,producer,NULL);
pthread_create(&p2,NULL,producer,NULL);
pthread_create(&p3,NULL,producer,NULL);
pthread_create(&p4,NULL,producer,NULL);
// create four consumer threads
pthread_create(&c1,NULL,consumer,NULL);
pthread_create(&c2,NULL,consumer,NULL);
pthread_create(&c3,NULL,consumer,NULL);
pthread_create(&c4,NULL,consumer,NULL);
}

Answer

Add the -pthread param to pull in all the threading stuff for linking

g++ ./main.c -pthread
Comments