6

I have ubuntu 11 installed in my system. I have a c program that uses the pthread library. I get the error Undefined reference to sem_wait() even if I have compiled with the flag -lpthread.

for example:

gcc -lpthread prog.c

The program works fine on other ubuntu installations.

GWW
  • 43,129
  • 11
  • 115
  • 108
Geo Paul
  • 1,777
  • 6
  • 25
  • 50

1 Answers1

12

Try:

gcc -pthread

instead of -lpthread. The difference is significant, I believe. The latter is linking against libpthread, the former is linking against libpthread and a bunch of other things, too!

sem_wait is part of librt, so you could just as well use gcc -lrt, but -pthread does this for you (and everything else as well!).

Gian
  • 13,735
  • 44
  • 51