2
I’m trying to solve a problem with semáforos
, but when I try to use the function sem_init()
I get an error saying there’s an indefinite reference,
can tell me why?
I have the library of semáforos
included.
Code (C):
#include<semaphore.h>
#include<sys/sem.h>
int pos_escrita;
int pos_leitura;
int buffer[10]; //capacidade para 10
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t vazio;
sem_t cheio;
// IDs das threads
int id_produtores[100];
int id_consumidores[10];
void init()
{
sem_init(&vazio, 0, 10);
sem_init(&cheio, 0, 0);
pos_escrita = 0;
pos_leitura = 0;
}
int main()
{
int i;
init();
pthread_t threads[2]; // nº de threads
for(i=0; i<10;i++)
{
}
printf("\n");
return 0;
}
The error presented was:
/tmp/ccTcI1EU: In Function init': f.c:(.text+0x1e): Undefined Reference to sem_init' f.c:(. text+0x3a): Undefined Reference to sem_init' collect2: Ld returned 1 Exit status
Try to put
-lrt
or-lpthread
when compiling:gcc codigo.c -lpthread -o codigo
– Leonardo