3

I am compiling a program that contains mutex semaphores from the pthread library but when i compile using the -lpthread flag I am getting an undefined reference error.

gcc -lpthread prodcon.c
/tmp/ccESOlOn.o: In function `producer':
prodcon.c:(.text+0x2e): undefined reference to `pthead_mutex_lock'
prodcon.c:(.text+0xd6): undefined reference to `pthead_mutex_unlock'
collect2: ld returned 1 exit status

The syntax for the mutex lock is like so:

pthread_mutex_t mutex1;

is a global declaration so that it can be used by multiple threads. within the functions I am calling the mutex like so:

pthead_mutex_lock(&mutex1);
pthead_mutex_unlock(&mutex1);

But i am getting the compiler error, I also tried compiling with the -pthread flag

gcc -pthread prodcon.c
/tmp/cc6wiQPR.o: In function `producer':
prodcon.c:(.text+0x2e): undefined reference to `pthead_mutex_lock'
prodcon.c:(.text+0xd6): undefined reference to `pthead_mutex_unlock'
collect2: ld returned 1 exit status

I have searched for answers but am at a loss and would appreciate any help with figuring out why it has an undefined reference when I am linking in the library that contains the mutex locks.

alk
  • 69,737
  • 10
  • 105
  • 255
aastorms
  • 45
  • 1
  • 1
  • 4

1 Answers1

5

Order matters, so use:

gcc prodcon.c -lpthread 

Or better:

gcc -Wall -Wextra -pedantic -pthread prodcon.c -lpthread

Are you sure you use pthead_mutex_lock? Or is this a typo? Anyways it should be pthread_mutex_lock. Same for pthread_mutex_unlock.

alk
  • 69,737
  • 10
  • 105
  • 255
  • it was a typo, but compiling like that seems to have done it though I am sure I tried that I must have made an error somewhere. Thanks! – aastorms Mar 01 '16 at 06:48