Undefined Reference Problems Using Semaphores

I play using Semaphores, but I continue to encounter Undefined help warnings, which leads to the fact that my code does not work. I pulled out the sample code from the text, but had problems with some of their syntax, so I went to the POSIX semaphore tutorial and changed the situation to my syntax, and as a result now I get these reference errors.

I can just ignore something, but I cannot find it.

Errors:

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 

What I have (may seem a little ugly due to the way I commented on this from my old method). I also know that my add method will not work, but I will get to this when I fix my syntax problems

 #include <stdio.h> #include <semaphore.h> #include <string.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <errno.h> #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(&mutex, 0, 1); //sem_init(&empty, 0, N); sem_init(&full, 0, 0); while(1) { sem_wait(&empty); sem_wait(&mutex); //printf("Empy: %d\n",empty); //printf("Mutex: %d\n",mutex); //printf("Both Downs Ran\n"); //buffer = buffer + 1; sem_post(&mutex); sem_post(&full); //printf("Producer produced: %d\n",buffer); } } void consumer(void) { while(1) { sem_wait(&full); sem_wait(&mutex); //item = buffer; sem_post(&mutex); sem_post(&empty); //printf("Consumer consumed: %d/n",item); } } void main() { } 
+8
c undefined-reference semaphore
source share
2 answers

If you are on a linux system, you will need to compile and link the -pthread flag to link the pthreads library.

 gcc -pthread Producers_Consumers.c 

As Paul Griffiths noted, you can also use -lrt , which is more portable and bundles the POSIX Realtime Extensions library

 gcc Producers_Consumers.c -lrt 

  • int main(void) not void main()
  • typedef int semaphore is wrong, sem_t should be considered as an opaque type, you will never use this typedef in your code.
  • The problem I foresee is that your consumer code uses semaphores before they are initialized in producer . You must initialize them in main
+17
source share

Received the same error in ubuntu qt. After adding

LIBS + = -lpthread -lrt

for the project.pro file all compiled.

+2
source share

All Articles