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.
The first thing you need to check is whether you have installed the gcc compiler or not. If you have not installed gcc compiler and trying to use gcc command then the error message is obvious as the functionality is not available on your computer.
If you get the bash: gcc: command not found error when using this command, the GCC tool is not installed correctly.
In the Command Prompt window type “gcc” and hit enter. If the output says something like “gcc: fatal error: no input files”, that is good, and you pass the test.
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!).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With