Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc -lpthread not working

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.

like image 337
Geo Paul Avatar asked Nov 25 '11 04:11

Geo Paul


People also ask

Why GCC is not working?

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.

What does GCC command not found mean?

If you get the bash: gcc: command not found error when using this command, the GCC tool is not installed correctly.

How can I tell if GCC is working?

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.


1 Answers

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!).

like image 117
Gian Avatar answered Oct 06 '22 00:10

Gian