undefined reference?

I have the following peace of code for the producer_consumer problem:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

#define QUEUESIZE 5
#define QMAX 300000

void *producer (void *args);
void *consumer (void *args);

typedef struct {
int buf[QUEUESIZE];
long head, tail;
int full, empty;
pthread_mutex_t *mut;
pthread_cond_t *notFull, *notEmpty;
} queue;

queue *queueInit (void);
void queueDelete (queue *q);
void queueAdd (queue *q, int in);
void queueDel (queue *q, int *out);

int main()
{
queue *fifo;
pthread_t pro, con;
fifo = queueInit ();
if (fifo == NULL) {
fprintf(stderr, "main: Queue Init failed.\n");
exit(1);
}
pthread_create (&pro, NULL, producer, fifo);
pthread_create (&con, NULL, consumer, fifo);
pthread_join (pro,NULL);
pthread_join (con,NULL);
phthread_mutex_lock(fifo->mut);
phthread_mutex_unlock(fifo->mut);
queueDelete (fifo);
return 0;
}

void *producer (void *q)
{
queue *fifo;
int i;
fifo = (queue *)q;
for (i = 0; i<= QMAX; i++) {
while (fifo->full) {
//printf ("producer: queue FULL.\n");
continue;
}
queueAdd (fifo, i);
}
return (NULL);
}

void *consumer (void *q)
{
queue *fifo;
int i, d;
fifo = (queue *)q;
for (i = 0; i<= QMAX; i++) {
while (fifo->empty) {
//printf ("producer: queue Empty.\n");
continue;
}
queueDel (fifo, &d);
if (i == QMAX)
printf( "consumer:recieved %d.\n", d);
}
return (NULL);
}

queue *queueInit (void)
{
queue *q;
q = (queue *)malloc (sizeof (queue));
if (q == NULL) return (NULL);
q->empty = 1;
q->full = 0;
q->head = 0;
q->tail = 0;
q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
pthread_mutex_init (q->mut, NULL);
q->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
pthread_cond_init (q->notFull, NULL);
q->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
pthread_cond_init (q->notEmpty, NULL);
return (q);
}

void queueDelete (queue *q)
{
pthread_mutex_destroy (q->mut);
free (q->mut);
pthread_cond_destroy (q->notFull);
free (q->notFull);
pthread_cond_destroy (q->notEmpty);
free (q->notEmpty);
free (q);
}


void queueAdd (queue *q, int in)
{
q->buf[q->tail] = in;
q->tail++;
if (q->tail== QUEUESIZE)
q->tail= 0;
if (q->tail== q->head)
q->full= 1;
q->empty = 0;
return;
}

void queueDel (queue *q, int *out)
{
*out= q->buf[q->head];
q->head++;
if (q->head == QUEUESIZE)
q->head= 0;
if (q->head== q->tail)
q->empty= 1;
q->full = 0;
return;
}

I get this result:
bt Desktop # gcc -m32 -g -o consumer consumer.c
consumer.c:129:2: warning: no newline at end of file
/tmp/ccdRObpm.o: In function `main':
/root/Desktop/consumer.c:35: undefined reference to `pthread_create'
/root/Desktop/consumer.c:36: undefined reference to `pthread_create'
/root/Desktop/consumer.c:37: undefined reference to `pthread_join'
/root/Desktop/consumer.c:38: undefined reference to `pthread_join'
/root/Desktop/consumer.c:39: undefined reference to `phthread_mutex_lock'
/root/Desktop/consumer.c:40: undefined reference to `phthread_mutex_unlock'
collect2: ld returned 1 exit status

Par from the no new line,i have no idea how to fix the errors.any help is appreciated.
This is cplusplus.com, not c.com. A C++ programmer would create a Queue class. ;-)

However, the fix is to link with -lpthread.

I'd like to make one other recommendation if I might: don't program as root. You can really damage your system by running buggy code as root. Create a regular user account and use that.
Topic archived. No new replies allowed.